資源簡介
(菜鳥級)串口通信+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?????????????????{
????????????????????textBox3.Text?+=?“?“?+?S[c].ToString(“x“);
????????????????}
????????????}
????????????else?if?(S[2]<9)
????????????{
????????????????textBox3.Text?=?“?“;
????????????????for?(int?c?=?0;?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個文件信息
- 上一篇:人臉識別源碼C#
- 下一篇:zotero使用的中國引文樣式.csl文件
評論
共有 條評論