资源简介

实现串口的实时读取数据,定时发送数据,实时保存excel文件,数据处理,modbus协议

资源截图

代码片段和文件信息


using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using System.Configuration;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;


namespace kaifa1
{
    public partial class Form1 : Form
    {
        private SerialPort myserial = new SerialPort();
        private static System.Timers.Timer timeout;
        private int byteNums;
        private byte[] data;
        List list = new List();
        public Form1()
        {

            InitializeComponent();
            timeout = new System.Timers.Timer();
            timeout.Interval = 30;
            timeout.AutoReset = false;
            timeout.Enabled = false;
            data = new byte[1500];
            byteNums = 0;
            this.init();
        }

        public void init()
        {
            timeout.Elapsed += new System.Timers.ElapsedEventHandler(OverTime);
            myserial.DataReceived += new SerialDataReceivedEventHandler(portDataReceived);
        }
        private void portDataReceived(object sender SerialDataReceivedEventArgs e)
        {
            timeout.Stop();
            timeout.Start();

        }

        private void OverTime(object sender EventArgs e)
        {
            byteNums = myserial.BytesToRead;
            if (byteNums == 0)
            {
                timeout.Start();
            }
            else
            {
                try
                {
                    if (myserial.BytesToRead > 0)
                    {
                        myserial.Read(data 0 myserial.BytesToRead);
                    }
                }
                catch
                {
                }
                switch (data[1])
                {
                    case 0x04:
                        if (list.Count == 0)
                        {
                            list.Add(data[3]);
                            list.Add(data[5]);
                            return;
                        }
                        else
                        {
                            for (int i = 3; i < byteNums; i++)
                            {
                                list.Add(data[i]);
                            }
                            //  byte[] buff = list.ToArray();
                            this.BeginInvoke(new EventHandler(show03));
                        }
                        break;
                    case 0x03:
                        if (list.Count == 0)
                        {
                            for (int i = 0; i < 6; i++)
                            {
                                list.Add(data[i]);
                            }
                            return;
                        }
                        else
                        {
                            for (int i = 2; i 

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

    ..A..H.     49152  2019-06-24 17:27  kaifa1\.vs\kaifa1\v14\.suo

     文件        252  2019-05-22 15:08  kaifa1\kaifa1\App.config

     文件     200704  2011-01-03 14:16  kaifa1\kaifa1\bin\Debug\ICSharpCode.SharpZipLib.dll

     文件      34304  2019-06-18 14:54  kaifa1\kaifa1\bin\Debug\kaifa1.exe

     文件        252  2019-05-22 15:08  kaifa1\kaifa1\bin\Debug\kaifa1.exe.config

     文件      42496  2019-06-18 14:54  kaifa1\kaifa1\bin\Debug\kaifa1.pdb

     文件      22688  2019-06-24 17:27  kaifa1\kaifa1\bin\Debug\kaifa1.vshost.exe

     文件        252  2019-05-22 15:08  kaifa1\kaifa1\bin\Debug\kaifa1.vshost.exe.Config

     文件        490  2015-11-13 10:24  kaifa1\kaifa1\bin\Debug\kaifa1.vshost.exe.manifest

     文件    1624064  2018-12-17 18:54  kaifa1\kaifa1\bin\Debug\NPOI.dll

     文件     530432  2018-12-17 18:54  kaifa1\kaifa1\bin\Debug\NPOI.OOxml.dll

     文件      89600  2018-12-17 18:54  kaifa1\kaifa1\bin\Debug\NPOI.Openxml4Net.dll

     文件    2078720  2018-12-17 18:54  kaifa1\kaifa1\bin\Debug\NPOI.OpenxmlFormats.dll

     文件      24224  2019-06-03 15:10  kaifa1\kaifa1\Form1.cs

     文件      53195  2019-05-23 15:10  kaifa1\kaifa1\Form1.Designer.cs

     文件       5817  2019-05-22 15:34  kaifa1\kaifa1\Form1.resx

     文件       5239  2019-05-22 17:09  kaifa1\kaifa1\kaifa1.csproj

     文件       1452  2019-05-15 14:10  kaifa1\kaifa1\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7613  2019-05-23 14:58  kaifa1\kaifa1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1141  2019-06-24 17:27  kaifa1\kaifa1\obj\Debug\kaifa1.csproj.FileListAbsolute.txt

     文件       1012  2019-05-22 15:37  kaifa1\kaifa1\obj\Debug\kaifa1.csproj.GenerateResource.Cache

     文件      31794  2019-05-21 13:55  kaifa1\kaifa1\obj\Debug\kaifa1.csprojResolveAssemblyReference.cache

     文件      34304  2019-06-18 14:54  kaifa1\kaifa1\obj\Debug\kaifa1.exe

     文件        180  2019-05-28 16:23  kaifa1\kaifa1\obj\Debug\kaifa1.Form1.resources

     文件      42496  2019-06-18 14:54  kaifa1\kaifa1\obj\Debug\kaifa1.pdb

     文件        180  2019-05-28 16:23  kaifa1\kaifa1\obj\Debug\kaifa1.Properties.Resources.resources

     文件        204  2019-05-20 15:16  kaifa1\kaifa1\packages.config

     文件        487  2019-05-15 13:56  kaifa1\kaifa1\Program.cs

     文件       1320  2019-05-15 13:56  kaifa1\kaifa1\Properties\AssemblyInfo.cs

     文件       2825  2019-05-15 13:56  kaifa1\kaifa1\Properties\Resources.Designer.cs

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

评论

共有 条评论