/// <summary>
/// 串口初始化
/// </summary>
private void OpenSerialPort()
{
//串口号、波特率、停止位 有好多,选择你适合你电脑的
//串口号
serialPort1.PortName = "COM1";
//波特率
serialPort1.BaudRate = "9600";
//停止位
serialPort1.StopBits = StopBits.One;
//打开串口
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
}
/// <summary>
/// 扫描
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)//内置方法
{
//获得条码信息
string SerialNum = "";
//延时确保扫描完全---这个很重要,扫描时间太短会导致扫描信息不全
System.Threading.Thread.Sleep(100);
int count = serialPort1.BytesToRead;
byte[] data = new byte[count];
serialPort1.Read(data, 0, count);
foreach (byte item in data)
{
SerialNum += Convert.ToChar(item);
}
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate
{
//显示信息
label2.Text = code;
}));
}
}