• 大小: 118KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C#
  • 标签: C#  电能表  DLT645  485模块  

资源简介

本程序采用C#实现能够读取电能表电表数据,电能表需具有485模块,通过485转232串口线和232转usb串口线与电脑相链接,安装相应的驱动程序,即可在电脑上读出电能表的数据。对嵌入式程序的设计,串口通讯等方面的编程具有重要意义。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;
using System.IO;
using System.Text.Regularexpressions;
using System.Reflection;
using System.Threading;

namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        int count = 0;//发送的次数
        int sendcount = 0;
        int getcount = 0;
      
        public Form1()
        {
            InitializeComponent();
        }
       //串口界面初始化
        private void Form1_Load(object sender EventArgs e)
        {
            //如果有配置 则读取配置文件
            if (File.Exists(Application.StartupPath + @“\SerConfig.wjs“))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + @“\SerConfig.wjs“ Encoding.UTF8))
                {
                    comboBox1.SelectedItem = sr.ReadLine();
                    comboBox2.SelectedItem = sr.ReadLine();
                    comboBox3.SelectedItem = sr.ReadLine();
                    comboBox4.SelectedItem = sr.ReadLine();
                    comboBox5.SelectedItem = sr.ReadLine();
                    textBox4.Text = sr.ReadLine();
                }
            }
            else 
            {
                //初始化
                comboBox1.SelectedIndex = 4;
                comboBox2.SelectedIndex = 5;
                comboBox3.SelectedIndex = 0;
                comboBox4.SelectedIndex = 2;
                comboBox5.SelectedIndex = 0;
            }
            Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
          //  this.Text = this.Text + “V“ + version.Major + “.“ + version.Minor;

            //扫描串口函数
            SearchAndAddSerialToComboBox(serialPort1comboBox1);
        }

        //扫描端口函数
        private void SearchAndAddSerialToComboBox(SerialPort MyPort ComboBox MyBox)
        {
            String Buffer;
            MyBox.Items.Clear();//清空列表
            for (int i = 1; i <= 10; i++)//测试每一个串口
            {
                try
                {
                    Buffer = “COM“ + i.ToString();
                    MyPort.PortName = Buffer;//对端口赋值
                    MyPort.Open();
                    MyBox.Items.Add(Buffer);//添加到下拉列表里
                    MyPort.Close();
                }
                catch (Exception)//不能打开运行这里进入下个i
                {

                }
            }
        }

        //设置串口名函数(当改变选中项时发生)
        private void comboBox1_SelectedIndexChanged(object sender EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                CloseSet();
                serialPort1.PortName = comboBox1.Text;
                OpenSet();
            }
            else 
            {
                serialPort1.PortName = comboBox1.Text;
     

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件         77  2016-04-12 10:09  读电表(自编)\WindowsFormsApplication21\app.config

     文件         70  2016-04-02 22:18  读电表(自编)\WindowsFormsApplication21\bin\Debug\SerConfig.wjs

     文件      27648  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.dll

     文件         77  2016-04-12 10:09  读电表(自编)\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.dll.config

     文件      81408  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.pdb

     文件      11600  2016-04-12 10:05  读电表(自编)\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.vshost.exe

     文件        490  2010-03-17 22:39  读电表(自编)\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.vshost.exe.manifest

     文件      16950  2016-01-16 12:27  读电表(自编)\WindowsFormsApplication21\Form1.cs

     文件      31254  2016-01-06 17:22  读电表(自编)\WindowsFormsApplication21\Form1.Designer.cs

     文件       6612  2016-01-06 17:22  读电表(自编)\WindowsFormsApplication21\Form1.resx

     文件        645  2016-01-02 22:04  读电表(自编)\WindowsFormsApplication21\Form2.cs

     文件       6847  2016-01-02 22:04  读电表(自编)\WindowsFormsApplication21\Form2.Designer.cs

     文件       5817  2016-01-02 22:04  读电表(自编)\WindowsFormsApplication21\Form2.resx

     文件       4440  2015-12-26 13:44  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6753  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       2834  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\GenerateResource.read.1.tlog

     文件       7886  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\GenerateResource.write.1.tlog

     文件       7897  2015-12-30 09:06  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\ResolveAssemblyReference.cache

     文件       7702  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\WindowsFormsApplication21.csproj.FileListAbsolute.txt

     文件      27648  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\WindowsFormsApplication21.dll

     文件        180  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\WindowsFormsApplication21.Form1.resources

     文件        180  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\WindowsFormsApplication21.Form2.resources

     文件      81408  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\WindowsFormsApplication21.pdb

     文件        180  2016-04-12 10:11  读电表(自编)\WindowsFormsApplication21\obj\x86\Debug\WindowsFormsApplication21.Properties.Resources.resources

     文件        506  2015-12-24 10:11  读电表(自编)\WindowsFormsApplication21\Program.cs

     文件       1398  2016-01-02 10:12  读电表(自编)\WindowsFormsApplication21\Properties\AssemblyInfo.cs

     文件       2898  2015-12-24 10:11  读电表(自编)\WindowsFormsApplication21\Properties\Resources.Designer.cs

     文件       5612  2015-12-24 10:11  读电表(自编)\WindowsFormsApplication21\Properties\Resources.resx

     文件       1108  2015-12-24 10:11  读电表(自编)\WindowsFormsApplication21\Properties\Settings.Designer.cs

     文件        249  2015-12-24 10:11  读电表(自编)\WindowsFormsApplication21\Properties\Settings.settings

............此处省略17个文件信息

评论

共有 条评论