[英]C# - readin from serial port buffer
I am trying to read data from an RS-232 port. 我正在尝试从RS-232端口读取数据。 Does anyone have an example of how I get the data from the port/buffer and make sure that I have all the data as it can be multiline data. 有没有人举过一个例子,说明如何从端口/缓冲区获取数据,并确保我拥有所有数据,因为它可以是多行数据。
Do I simply read it as follows ? 我是否简单阅读如下?
string Rxstring = port.ReadLine();
Console.WriteLine(Rxstring);
Try this: 尝试这个:
using System.IO.Ports;
...
private SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
Console.WriteLine(port.ReadExisting());
Details can be found at Coad's Code . 有关详细信息,请参见Coad's Code 。
Q: how to get the date from the port/buffer, or input data from your connected device. 问:如何从端口/缓冲区获取日期,或如何从连接的设备输入数据。 AND make sure that you have all the data. 并且确保您拥有所有数据。
A: i have worked extensively with .net serial port class drivers where i was tasked to create reliable, robust code. 答:我曾与.net串行端口类驱动程序进行过广泛的合作,我曾受命创建可靠,强大的代码。 this means that a connected device under test has to run and NOT fail over a LONG period of time. 这意味着所连接的被测设备必须运行,并且不会在很长一段时间内发生故障。 Serial port can AND does lose data! 串口可以并且确实会丢失数据! don't forget that. 别忘了
//from top of the head;
using System.Port.IO;
using System.Port;
private class mywindowsForm: Form
{
StringBuilder sbReceived = new StringBuilder();
string Received = string.Empty;
int byteCOUNT = 0;
System.Windows.Timers.Timer serialTimer;
//Default constructor
myWindowsForm()
{
//assume that you clicked and dragged serial port in
serialPort1 = new SerialPort();//create new serial port instance
serialPort1.Baud = 9600;
serialPort1.DataReceived+=<Tab><Enter>
//serial port timer
serialTimer = new System.Windows.Timers.Timer(500);//set to 500ms time delay
serialTimer.Elapsed+=<TAB><ENTER>
}
void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//serial port has detected input data
//however, we only want to get serial data so,
if(e.EventType !=SerialData.Chars) return;
//good design practice dictates that we have an event handler that we invoke
this.BeginInvoke(new eventhandler(AddReceive));//beginInvoke is designed to deal with asynchronous processes like serial port data.
}
private void AddReceive(object s, EventArg e)
{
byteCOUNT=serialPort1.BytesToRead;//count the number of bytes in RX buffer
if(byteCOUNT > 0)
{
string ST = serialPort1.ReadTo("\n");//lets get one line at a time
sbReceived.Append(ST);//add whatever has been RX'd to our container.
serialPort1.Interval =100;
serialPort1.Start();//to be sure we have all data, check to see for stragglers.
}
}
void serialTimer(object Sender, TimerElapsedEventArgs e)
{
serialTimer.Stop();
this.BeginInvoke(new EventHandler(ReadData));
}
void ReadData(object Sender, EventArgs e)
{
//parse output for required data and output to terminal display (build one using rich text box)
Received = sbReceived.ToString();
//and if we have ANY MORE incoming data left over in serial buffer
if(Received.Length > 0)
{
//your data
}
}
}
this should be plenty to get you started. 这应该足以让您入门。 this is result of years of creating customized terminal emulators in c#. 这是多年来在c#中创建自定义终端模拟器的结果。 there are other things that can be done, particularly if you have large amount of i/o data you need to set up handshaking with device. 还有其他事情可以做,特别是如果您有大量I / O数据,则需要与设备建立握手。 you have to let the device handle at a rate that the device is happy with. 您必须让设备以设备满意的速度处理。 in cases where larger data has to be transferred consider setting up a simple packet passing protocol and command semaphore construct - or use a protocol as defined that the controller / device is designed to work with. 在必须传输较大数据的情况下,请考虑建立一个简单的数据包通过协议和命令信号量结构-或使用所定义的控制器/设备可以使用的协议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.