读取 ftdi 以显示字符串
Reading ftdi to show string
我用这段代码通过ftdi usb读取RFID芯片SN reader。
我想稍后在 SQL 查询中使用 RFID SN (0DBFFC21)。
我的问题是我多次将 SN 分成两行...
我应该如何延迟或什么的,这样我每次都能得到完整的字符串?
#region Namespace Inclusions
using System;
using System.IO.Ports;
using System.Windows.Forms;
#endregion
namespace SerialPortExample
{
class SerialPortProgram
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM1",
9600, Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
// Instatiate this class
new SerialPortProgram();
}
private SerialPortProgram()
{
Console.WriteLine("Incoming Data:");
// Attach a method to be called when there
// is data waiting in the port's buffer
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);
// Begin communications
port.Open();
// Enter an application loop to keep this thread alive
Application.Run();
}
private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine(port.ReadExisting());
}
}
}
正确的方法可能会有所不同,但这里有一种简单易读的已知大小数据:
string _buffer;
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
_buffer += port.ReadExisting(); // read into buffer
if (_buffer.Length > 7) // wait until at least 8 characters are received
{
Console.WriteLine(_buffer.Substring(0, 8)); // display
_buffer = _buffer.Substring(8, _buffer.Length - 8); // remove from buffer
}
}
如果数据包之间的延迟足够大,则以下代码可能就足够了:
_buffer += port.ReadExisting();
if (_buffer.Length >= 8)
{
Console.WriteLine(_buffer);
_buffer = null;
}
我用这段代码通过ftdi usb读取RFID芯片SN reader。 我想稍后在 SQL 查询中使用 RFID SN (0DBFFC21)。
我的问题是我多次将 SN 分成两行... 我应该如何延迟或什么的,这样我每次都能得到完整的字符串?
#region Namespace Inclusions
using System;
using System.IO.Ports;
using System.Windows.Forms;
#endregion
namespace SerialPortExample
{
class SerialPortProgram
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM1",
9600, Parity.None, 8, StopBits.One);
[STAThread]
static void Main(string[] args)
{
// Instatiate this class
new SerialPortProgram();
}
private SerialPortProgram()
{
Console.WriteLine("Incoming Data:");
// Attach a method to be called when there
// is data waiting in the port's buffer
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);
// Begin communications
port.Open();
// Enter an application loop to keep this thread alive
Application.Run();
}
private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine(port.ReadExisting());
}
}
}
正确的方法可能会有所不同,但这里有一种简单易读的已知大小数据:
string _buffer;
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
_buffer += port.ReadExisting(); // read into buffer
if (_buffer.Length > 7) // wait until at least 8 characters are received
{
Console.WriteLine(_buffer.Substring(0, 8)); // display
_buffer = _buffer.Substring(8, _buffer.Length - 8); // remove from buffer
}
}
如果数据包之间的延迟足够大,则以下代码可能就足够了:
_buffer += port.ReadExisting();
if (_buffer.Length >= 8)
{
Console.WriteLine(_buffer);
_buffer = null;
}