資源簡介
1、C# 串口通信 讀二維碼 基恩士N-R2控制器 SR-710讀碼器 功能Demo;
2、依據實際選擇COM端口號, 依實際修改讀碼指令和停止讀碼指令;
3、讀碼指令和停止讀碼指令可在 基恩士的軟件AutoID Network Navigator中設置和測試。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading;
using?System.Windows.Forms;
namespace?SerialPortDemo
{
????public?partial?class?FrmSerialPortMain?:?Form
????{
????????public?FrmSerialPortMain()
????????{
????????????InitializeComponent();
????????????Control.CheckForIllegalCrossThreadCalls?=?false;
????????}
????????System.IO.Ports.SerialPort??myCOM;
????????bool?comHadOpened?=?false;
????????string?LasterCode?=?““;
????????private?void?btnOpenPort_Click(object?sender?EventArgs?e)
????????{
????????????if?(comHadOpened?==?false)
????????????{
????????????????try
????????????????{
????????????????????myCOM?=?new?System.IO.Ports.SerialPort();
????????????????????myCOM.PortName?=?“COM3“;
????????????????????myCOM.BaudRate?=?115200;??//?波特率
????????????????????myCOM.DataBits?=?8;???//?數據位
????????????????????myCOM.Parity?=?System.IO.Ports.Parity.None;??//?校驗位
????????????????????myCOM.StopBits?=?System.IO.Ports.StopBits.One;??//停止位
????????????????????myCOM.NewLine?=?“\r\n“;
????????????????????myCOM.RtsEnable?=?true;?//啟用請求發送信號
????????????????????myCOM.DtrEnable?=?true;?//啟用控制終端就續信號
????????????????????myCOM.ReadTimeout?=?3000;
????????????????????myCOM.ReceivedBytesThreshold?=?1;
????????????????????myCOM.DataReceived?+=?new?System.IO.Ports.SerialDataReceivedEventHandler(this.myCom_DataReceived);
????????????????????myCOM.Open();
????????????????????comHadOpened?=?true;
????????????????}
????????????????catch?(Exception?ex)
????????????????{
????????????????????MessageBox.Show(ex.Message);
????????????????}
????????????}
????????}
????????private?void?btnTriggerOn_Click(object?sender?EventArgs?e)
????????{
????????????TriggerOn();
????????}
??????
????????private?void?TriggerOn()
????????{
????????????if?(comHadOpened?==?false)
????????????????return;
????????????try
????????????{
????????????????myCOM.WriteLine(this.txtONCommand.Text);
????????????}
????????????catch?(Exception?ex)
????????????{
???????????????listBox1.Items.Add(“[“?+?DateTime.Now.ToString(“MM-dd?HH:mm:ss“)?+?“]??“?+?ex.Message);
????????????????listBox1.SelectedIndex?=?listBox1.Items.Count?-?1;
????????????}
????????}
????????private?void?myCom_DataReceived(object?sender?System.IO.Ports.SerialDataReceivedEventArgs?e)
????????{
????????????Thread.Sleep(100);
????????????byte[]?ReDatas?=?new?byte[myCOM.BytesToRead];
????????????myCOM.Read(ReDatas?0?ReDatas.Length);//讀取數據
????????????string?recdata?=?new?ASCIIEncoding().GetString(ReDatas);
????????????if?(LasterCode!=recdata)
????????????{
????????????????listBox1.Items.Add(“[“?+?DateTime.Now.ToString(“MM-dd?HH:mm:ss“)?+?“]??“?+?recdata);
????????????????listBox1.SelectedIndex?=?listBox1.Items.Count?-?1;
????????????????LasterCode?=?recdata;
????????????}
????????}
????????private?void?FrmSerialPortMain_F
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3645??2021-07-07?17:01??SerialPortDemo.csproj
?????文件?????201216??2021-07-09?08:52??bin\Debug\SerialPortDemo.exe
?????文件??????40448??2021-07-09?08:52??bin\Debug\SerialPortDemo.pdb
?????文件????????208??2021-07-05?15:43??obj\Debug\.NETfr
?????文件???????2477??2021-07-22?11:11??obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7069??2021-07-07?17:01??obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????11??2021-07-09?08:52??obj\Debug\SerialPortDemo.csproj.AssemblyReference.cache
?????文件?????????42??2021-07-07?17:13??obj\Debug\SerialPortDemo.csproj.CoreCompileInputs.cache
?????文件???????2104??2021-07-07?17:13??obj\Debug\SerialPortDemo.csproj.FileListAbsolute.txt
?????文件???????1082??2021-07-08?09:42??obj\Debug\SerialPortDemo.csproj.GenerateResource.cache
?????文件?????201216??2021-07-09?08:52??obj\Debug\SerialPortDemo.exe
?????文件??????93420??2021-07-06?12:56??obj\Debug\SerialPortDemo.Form1.resources
?????文件??????93420??2021-07-08?09:42??obj\Debug\SerialPortDemo.FrmSerialPortMain.resources
?????文件??????40448??2021-07-09?08:52??obj\Debug\SerialPortDemo.pdb
?????文件????????180??2021-07-07?17:13??obj\Debug\SerialPortDemo.Properties.Resources.resources
?????文件???????1320??2021-07-05?15:43??Properties\AssemblyInfo.cs
?????文件???????2860??2021-07-05?15:43??Properties\Resources.Designer.cs
?????文件???????5612??2021-07-05?15:43??Properties\Resources.resx
?????文件???????1099??2021-07-05?15:43??Properties\Settings.Designer.cs
?????文件????????249??2021-07-05?15:43??Properties\Settings.settings
?????文件???????5025??2021-07-09?08:52??FrmSerialPortMain.cs
?????文件??????12754??2021-07-08?09:42??FrmSerialPortMain.Designer.cs
?????文件?????145595??2021-07-08?09:42??FrmSerialPortMain.resx
?????文件??????92854??2021-07-06?12:52??o11.ico
?????文件????????507??2021-07-07?17:01??Program.cs
?????目錄??????????0??2021-06-29?17:15??obj\Debug\TempPE
?????目錄??????????0??2021-06-29?17:23??bin\Debug
?????目錄??????????0??2021-07-07?17:01??bin\Release
?????目錄??????????0??2021-07-22?11:11??obj\Debug
?????目錄??????????0??2021-07-07?17:01??bin
............此處省略5個文件信息
評論
共有 條評論