C# reading data from Serial port

Question -

I have a device (a GPS type of device) that constantly pushes communication through a standard serial connection (COM1).

What I need to be able to do is:

Programatically:
1) Open the serial port
2) Listen for a given period of time (say 2 seconds) to the data that is being received via the serial port.
3) populate a multi-line text box with the data that was received from the serial port.
4) close teh serial port

Then I will build additional code surrounding handling the data in the textbox.


Answer -

I set up my serialport like this

sp.BaudRate = 115200;
sp.PortName = "COM53";
sp.DataBits = 8;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Parity = System.IO.Ports.Parity.None;
sp.ReadTimeout = 0;
sp.Open();
sp.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(sp_ DataReceived);


and then I have this

void sp_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{

string Mystring = sp.ReadExisting();
byte testbyte = 254;
// Gather all the bytes until 102 is reached
foreach (byte c in Mystring)
{
if(pixelcount<102) pixelcount ="="" testbyte =" {0}" oldx =" 0;" pixelcount =" 0;">

1 comments:

Thank you very much