1、C# 串口通信 读二维码 基恩士N-R2控制器 SR-710读码器 功能Demo;2、依据实际选择COM端口号, 依实际修改读码指令和停止读码指令;3、读码指令和停止读码指令可在 基恩士的软件AutoID Network Navigator中设置和测试。1、文件 2、实例UI: 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_FormClosing(object sender, FormClosingEventArgs e)
{
if (myCOM == null)
return;
if (myCOM.IsOpen)
{
myCOM.Close();
myCOM.Dispose();
}
}
private void btnClosePort_Click(object sender, EventArgs e)
{
if (myCOM == null)
return;
if (myCOM.IsOpen)
{
myCOM.Close();
comHadOpened = false;
myCOM.Dispose();
}
}
private void btnTriggerOFF_Click(object sender, EventArgs e)
{
TriggerOFF();
}
private void TriggerOFF()
{
if (comHadOpened == false)
return;
try
{
myCOM.WriteLine(this.txtOFFCommand.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 FrmSerialPortMain_Load(object sender, EventArgs e)
{
string[] ArryPort = System.IO.Ports.SerialPort.GetPortNames();
cbSerialPortList.Items.Clear();
for (int i = 0; i < ArryPort.Length; i )
{
cbSerialPortList.Items.Add(ArryPort[i]);
}
cbSerialPortList.SelectedIndex = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
TriggerOn();
}
private void btnStartTimer_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void btnStopTimer_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
}
}
评论