Wednesday, November 27, 2013

SERIAL PORT BUFFERING C#.NET

SERIAL PORT BUFFERING C#.NET
ШАБЛОНЫ САЙТОВ ОНЛАЙН-МАГАЗИНЫ
ФАЙЛОВЫЕ МЕНЕДЖЕРЫ JOOMLA
In this article Serial Port buffering is explained. How to receive the serial port data without any loss if incoming data stream flow into seperate triggers is realized using c#.

Serial data flow through RS-232/RS-485/RS-422 has nothing to do with ‘packets’. It’s just a stream of bytes in and out. There is no guarantee that data arrives together. Data may come seperate triggers

Communication using Serial Port often used on monitoring the incoming data coming from electronic device that is designed on the bench, Commercial Off The Shelf device that we purchase for the use of as a part of our project, or electronic test instrument communication interface.

Since the bytes may come to computers Serial Port Communication Hardware(Serial Port of the Computer) in at any time, buffering the incoming data is important for not loosing any byte. For example, you may send a command out to your device, and the response back to the PC could trigger a single DataReceived event with all the 15 bytes of response data in the receive buffer. Or more likely, it could be any number of separate triggers of the DataReceived (up to the number of bytes received), like 4 triggers, first with 2 bytes, then 15 bytes, then 1 byte, then 12 bytes.

In the above case, someone do not know when will the complete response is completed. At this point buffering the data plays an important role on not loosing the incoming data. This is known as Serial Port Buffering.
Istead of looking a complete response in a single DataReceived Event to be finished by the sender one may follow the following rule to catch the data

1. buffer the incoming data
2. then scan your buffer to find complete data
3. remove the used data from the buffer

public void serialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Use either the binary OR the string technique (but not both)
// Buffer and process binary data
while (com.BytesToRead > 0)
bBuffer.Add((byte)com.ReadByte());
ProcessBuffer(bBuffer);

// Buffer string data
sBuffer += com.ReadExisting();
ProcessBuffer(sBuffer);
}

private void ProcessBuffer(string sBuffer)
{
// Look in the string for useful information
// then remove the useful data from the buffer
}

private void ProcessBuffer(List<byte> bBuffer)

{
// Look in the byte array for useful information
// then remove the useful data from the buffer
}
The above code samples catches all the data coming out of the device.

To make clear the buffering, follow the example case below:

Let us assume that we have connected the serial port of the PC to the external harware to communicate.We send a command byte [] cmd = new byte[]{command CRC} and expect a response as byte[]{messageLength b1 b2 b3 b4 b5 b6 b7 b8 b9 CRC}(here a messageLength is 11). And assume also that the response arrives to the PCs serial port into 5 seperate triggers as ;

fisrt trigger:messageLength b1 b2
second trigger:b3 b4
third trigger:b5 b6
fourth trigger:b7 b8
fifth trigger:b9 CRC

Here are the stpes to be followed to communicate the serial port device
1.Send the command message to the serial port device, serialport.send(cmd)
2.wait for the response using following code

public void seriport_DataAvailable(object sender, EventArgs e)
{
while (((SerialPort)sender).BytesToRead > 0)
{
serialPortReceivedData = new byte[((SerialPort)sender).BytesToRead];
((SerialPort)sender).Read(serialPortReceivedData, 0, serialPortReceivedData.Length);
bBuffer.AddRange(serialPortReceivedData);
if (bBuffer[0] == bBuffer.Count)
{
//send the response to the listeners
bBuffer.Clear();
break;
}
}
}

http://www.miltest.com/articles/application-notes/59-serial-port-buffering-csharp.html

No comments: