资源简介

C#上位机电表数据采集

资源截图

代码片段和文件信息

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.Runtime.InteropServices;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        [DllImport(“kernel32“)]            //记忆串口号
        private static extern long WritePrivateProfileString(String section String key String val String filePath);
        [DllImport(“kernel32“)]
        private static extern long GetPrivateProfileString(String section String keyString def StringBuilder retVal int size String filePath);
        string FileName = System.AppDomain.CurrentDomain.baseDirectory + “data.ini“;

        StringBuilder temp = new StringBuilder(16);
        StringBuilder temp1 = new StringBuilder(16);
        string CurrentPortName;           //记忆串口号
        string CurrentBaudrate;

       // byte[] sendToData = new byte[] { 0x000x01 0x02 0x03 };   //发送的数据
        //byte dianbiaoCOM=0;
        byte[] buffer0 = new byte[7] { 0x00 0xff 0x01 0x13 0x14 0x0D 0x0A };
        byte[] buffer1 = new byte[7] { 0x00 0x01 0x01 0x13 0x14 0x0D0x0A};
        byte[] buffer2 = new byte[7] { 0x00 0x02 0x01 0x13 0x14 0x0D 0x0A };
        byte[] buffer3 = new byte[7] { 0x00 0x03 0x01 0x13 0x14 0x0D 0x0A };

        string dateTime = DateTime.Now.ToString(“yyyyMMdd“);
     

        public Form1()
        {
            InitializeComponent();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_FormClosing);
        }

        private void port_DataReceived(object sender SerialDataReceivedEventArgs e)//接收
        {
            string str;
            if (!checkBox2.Checked)//字符
            {
                try
                {
                    str = serialPort1.ReadExisting();
                    textBox1.AppendText(str);
                }
                catch (Exception) { }
            }
            else
            {
                byte dat;
                dat = (byte)serialPort1.ReadByte();
                str = Convert.ToString(dat 16).ToUpper();
                textBox1.AppendText((str.Length == 1) ? (“0X0“ + str) : (“0X“ + str));
            }
    if (checkBox3.Checked)
    {
        textBox1.AppendText(Environment.NewLine);
    }
 }

//if (dianbiaoCOM == 1)
//{
//    textBox1.AppendText(“电表0:“);
//}
//if (dianbiaoCOM == 2)
//{
//    textBox1.AppendText(“电表1:“);

//}
//if (dianbiaoCOM == 3)
//{
//    textBox1.AppendText(“电表2:“);
//}
//if (dianbiaoCOM == 4)
//{
//    textBox1.AppendText(“电表3:“);
//}
//private void SendDataToSerialPort(SerialPort MyPort byte adrs2

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-02-11 19:39  采集电表1\
     目录           0  2020-02-11 19:39  采集电表1\WindowsFormsApplication2\
     文件        1041  2019-03-03 16:01  采集电表1\WindowsFormsApplication2.sln
     文件       44544  2019-04-03 13:11  采集电表1\WindowsFormsApplication2.v12.suo
     目录           0  2020-02-11 19:39  采集电表1\WindowsFormsApplication2\bin\
     目录           0  2020-02-11 19:39  采集电表1\WindowsFormsApplication2\bin\Debug\
     文件          46  2019-04-03 13:11  采集电表1\WindowsFormsApplication2\bin\Debug\data.ini
     文件           0  2019-04-02 18:26  采集电表1\WindowsFormsApplication2\bin\Debug\Vista20190402.txt
     文件          20  2019-04-03 01:37  采集电表1\WindowsFormsApplication2\bin\Debug\Vista20190403.txt
     文件       17920  2019-04-03 12:49  采集电表1\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe
     文件       32256  2019-04-03 12:49  采集电表1\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.pdb
     文件       24216  2019-04-03 15:22  采集电表1\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe
     文件         490  2018-04-12 07:35  采集电表1\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.manifest
     文件        9489  2019-04-03 12:49  采集电表1\WindowsFormsApplication2\Form1.cs
     文件       15684  2019-04-03 11:50  采集电表1\WindowsFormsApplication2\Form1.Designer.cs
     文件        6384  2019-04-03 11:50  采集电表1\WindowsFormsApplication2\Form1.resx
     目录           0  2020-02-11 19:39  采集电表1\WindowsFormsApplication2\obj\
     目录           0  2020-02-11 19:39  采集电表1\WindowsFormsApplication2\obj\Debug\
     文件        1453  2019-03-03 17:11  采集电表1\WindowsFormsApplication2\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6899  2019-04-02 19:28  采集电表1\WindowsFormsApplication2\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件        3030  2019-04-03 15:22  采集电表1\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csproj.FileListAbsolute.txt
     文件         977  2019-04-03 11:50  采集电表1\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csproj.GenerateResource.Cache
     文件        2211  2019-03-03 16:48  采集电表1\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csprojResolveAssemblyReference.cache
     文件       17920  2019-04-03 12:49  采集电表1\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.exe
     文件         180  2019-04-03 11:50  采集电表1\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.Form1.resources
     文件       32256  2019-04-03 12:49  采集电表1\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.pdb
     文件         180  2019-03-03 16:48  采集电表1\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.Properties.Resources.resources
     文件         505  2019-03-23 22:26  采集电表1\WindowsFormsApplication2\Program.cs
     目录           0  2020-02-11 19:39  采集电表1\WindowsFormsApplication2\Properties\
     文件        1372  2019-03-03 16:01  采集电表1\WindowsFormsApplication2\Properties\AssemblyInfo.cs
     文件        2900  2019-03-03 16:01  采集电表1\WindowsFormsApplication2\Properties\Resources.Designer.cs
............此处省略4个文件信息

评论

共有 条评论