• 大小: 64KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: C#
  • 标签: 串口通信  c#  CRC  

资源简介

(菜鸟级)串口通信+CRC校验,之前很久写的VS2010版本。实践无毛病。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Linq;

using System.IO.Ports;
using System.Drawing;
using System.Diagnostics;

using System.ComponentModel;

namespace 济南电柜
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region 串口配置
        
        public SerialPort spPower = new SerialPort();
        byte[] fx = new byte[8];
       
        public void port_powerinit()
        {

           spPower.DataReceived += new SerialDataReceivedEventHandler(spPower_DataReceived);

        }
        public void startPower()
        {
            if (!spPower.IsOpen)
            {
                string portname = comboBox1.Text; int baudrate = 9600; int databits = 8; Parity parity = Parity.None; StopBits stopbits = StopBits.Two;
                spPower.PortName = portname;
                spPower.BaudRate = baudrate;
                spPower.DataBits = databits;
                spPower.Parity = parity;
                spPower.StopBits = stopbits;
                spPower.DtrEnable = true;//若不定义 串口监听不能使
                spPower.RtsEnable = true;//若不定义 串口监听不能使
                //spPower.ReadTimeout = 1000;
                try
                {
                    spPower.Open();
                    toolStripStatusLabel1.Text = “串口启动成功“;
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                }

            }
        }
        #endregion
        #region 数据解析
        private void spPower_DataReceived(object sender SerialDataReceivedEventArgs e)
        {


           // Thread.Sleep(500);///  星形接法,回去后改
            int n = spPower.BytesToRead;
            byte[] S = new byte[n];
            spPower.Read(S 0 n);

            if (S[2] ==0x02)
            {
                textBox3.Text = “ “;
                label9.Text = Convert.ToString((S[3] * 256 + S[4]) * 0.1)+“ V“;
                for (int c = 0; c < n; c++)
                {
                    textBox3.Text += “ “ + S[c].ToString(“x“);
                }
            }
            else if (S[2]<9)
            {
                textBox3.Text = “ “;
                for (int c = 0; c < n; c++)
                {
                    textBox3.Text += “ “ + S[c].ToString(“x“);
                }
            }

            else  if (S[2]>9)
            {
             int st = S[3]*256+S[4];//机器状态
                if (st == 1)
                {
                    label22.Text = “已启动“;
                    
                }
                else if (st==0)
                {
                    label22.Text = “已待机“;
                }
                label24.Te

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-10 15:16  济南电柜\
     目录           0  2018-05-10 15:16  济南电柜\济南电柜\
     文件         878  2017-04-06 17:06  济南电柜\济南电柜.sln
     文件       23040  2018-04-02 17:27  济南电柜\济南电柜.suo
     目录           0  2018-05-10 15:16  济南电柜\济南电柜\bin\
     目录           0  2018-05-10 15:16  济南电柜\济南电柜\bin\Debug\
     文件       20992  2017-04-07 12:02  济南电柜\济南电柜\bin\Debug\济南电柜.exe
     文件       36352  2017-04-07 12:02  济南电柜\济南电柜\bin\Debug\济南电柜.pdb
     文件       11600  2018-04-02 17:18  济南电柜\济南电柜\bin\Debug\济南电柜.vshost.exe
     文件         490  2010-03-17 22:39  济南电柜\济南电柜\bin\Debug\济南电柜.vshost.exe.manifest
     目录           0  2017-04-06 23:23  济南电柜\济南电柜\bin\Release\
     文件       11115  2017-04-07 12:02  济南电柜\济南电柜\Form1.cs
     文件       27661  2017-04-07 12:02  济南电柜\济南电柜\Form1.Designer.cs
     文件        6017  2017-04-07 12:02  济南电柜\济南电柜\Form1.resx
     目录           0  2018-05-10 15:16  济南电柜\济南电柜\obj\
     目录           0  2018-05-10 15:16  济南电柜\济南电柜\obj\x86\
     目录           0  2018-05-10 15:16  济南电柜\济南电柜\obj\x86\Debug\
     文件        2867  2018-04-02 17:18  济南电柜\济南电柜\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6275  2018-04-02 17:18  济南电柜\济南电柜\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         534  2017-04-07 12:02  济南电柜\济南电柜\obj\x86\Debug\GenerateResource.read.1.tlog
     文件        1242  2017-04-07 12:02  济南电柜\济南电柜\obj\x86\Debug\GenerateResource.write.1.tlog
     文件        8031  2017-04-07 00:18  济南电柜\济南电柜\obj\x86\Debug\ResolveAssemblyReference.cache
     目录           0  2017-04-06 17:06  济南电柜\济南电柜\obj\x86\Debug\TempPE\
     文件        2098  2018-04-02 17:18  济南电柜\济南电柜\obj\x86\Debug\济南电柜.csproj.FileListAbsolute.txt
     文件       20992  2017-04-07 12:02  济南电柜\济南电柜\obj\x86\Debug\济南电柜.exe
     文件         180  2017-04-07 12:02  济南电柜\济南电柜\obj\x86\Debug\济南电柜.Form1.resources
     文件       36352  2017-04-07 12:02  济南电柜\济南电柜\obj\x86\Debug\济南电柜.pdb
     文件         180  2017-04-06 23:30  济南电柜\济南电柜\obj\x86\Debug\济南电柜.Properties.Resources.resources
     文件         493  2017-04-06 17:06  济南电柜\济南电柜\Program.cs
     目录           0  2018-05-10 15:16  济南电柜\济南电柜\Properties\
     文件        1374  2017-04-06 17:06  济南电柜\济南电柜\Properties\AssemblyInfo.cs
............此处省略5个文件信息

评论

共有 条评论