我的控制台应用程序出了点问题。我尝试使用条形码扫描仪从串行连接中检索数据。
问题在于: - 第一次阅读是完美的; - 除非我等待大约一分钟,否则第二次读取结果不正确。
以下是代码:
using System;
using System.IO.Ports;
class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM10");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.RequestToSend;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.WriteLine(indata);
}
}
这是2次读取相同条形码的结果,而没有等待第一次到第二次读取的足够时间:
Press any key to continue...
Data Received:
229000400718
Data Received:
2
Data Received:
2
Data Received:
9
Data Received:
0
Data Received:
0
Data Received:
0
Data Received:
4
Data Received:
0
Data Received:
0
Data Received:
7
Data Received:
1
Data Received:
8
有什么建议吗?
提前致谢!
答案 0 :(得分:1)
第二次读取的数据似乎是char的char,所以我会使用StringBuilder b并附加传入的数据,直到收到有效的条形码并包含在b中(即检查数据库的有效性)
我建议这样的事情:
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
StringBuilder b = new StringBuilder();
while(!IsValidBarcode(b.ToString()))
{
b.Append(sp.ReadExisting());
}
Console.WriteLine("Data Received:");
Console.WriteLine(b.ToString());
}
private static Boolean IsValidBarcode(String s)
{
if (String.IsNullOrEmpty(s)) return false;
// (1) Query a database for expected barcodes ...
// (2) Check s for Start-Stop-Characters ...
// (3) Query the device for completed barcode ...
throw new NotImplementedException();
}
或者
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
StringBuilder b = new StringBuilder();
Thread.Sleep(1000); // 1 second delay for testing purposes
b.Append(sp.ReadExisting());
Console.WriteLine("Data Received:");
Console.WriteLine(b.ToString());
}
答案 1 :(得分:0)
您需要有一个结束字符才能知道邮件何时完成。大多数人使用\r\n (13,10)
并从串口读取到行尾(即\ r \ n)。
您正在为每个角色获得一个事件并且阅读速度太快。
答案 2 :(得分:0)
您需要某种计时器来检查正在读取的多个字符是否是单个读取的一部分,或者它们是否是2个不同的扫描。第二次扫描的事件触发之间的时间可能非常短,只需将一个计时器设置为在事件触发后启动,如果它在下一个事件之前到期,那么它就是条形码的结束,如果没有将读取附加到上一个(使用某种缓冲区)。
答案 3 :(得分:0)
Luca Pillin,我刚刚将thread.sleep包含了3秒,它解决了问题
using System;
using System.Threading;
using System.IO.Ports;
class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM2");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.RequestToSend;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
//
string indata = string.Empty;
SerialPort sp = (SerialPort)sender;
**Thread.Sleep(3000);**
indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.WriteLine(indata);
}
}