.NET SerialPort DataReceived problem
Posted on 2006-06-29
I am using 2.0 and the SerialPort class to make and CSD data call and then parse the incoming data via DataReceived.
I am also using Portmon to analyse the actual data that is incoming through the port. So I can make sure I am getting everything correct.
I have been experiencing a few problems with picking up the data from the remote modem.
Randomly when I make a datacall every other packet sent from the remote modem, which can be seen in full via Portmon, is sent through as null via the DataReceived event. The event is being called but the data not included. The alternate packet is fine! - i.e data seen in portmon is sent via DataReceived.
To make the matter more harded to pin point, Somtimes when I connect EVERY packet is sent and rec totally fine, and there is no problem. This is what confuses me, why it works one time then not the other!!
Does anyone have a clue why this could be??
At first I thought it could be a result of signal strength on the remote modem, but the correct information is coming through ok as its shown in portmon.
Then i thought, and still do, is that the problem must be with my code somewhere.
I have tried reading from the port in bytes, char, readexisting, readline all with the same result.
I have also tried different encodings.
Hopefully I'm missing something somewhere.
Any help would be great., thanks in advance.
My current code;
public bool OpenPort()
mPort.PortName = "COM1";
mPort.Handshake = Handshake.RequestToSendXOnXOff;
mPort.ReadTimeout = 50000;
mPort.Encoding = System.Text.Encoding.ASCII;
mPort.WriteTimeout = 50000;
mPort.Parity = Parity.None;
mPort.BaudRate = 9600;
mPort.StopBits = StopBits.One;
mPort.DataBits = 8;
mPort.DtrEnable = true;
// add relevant exceptions here
AddToLogFile("Can not open port");
// this is hooked up elsewhere
mPort.DataReceived += new SerialDataReceivedEventHandler(mPort_DataRecDownloadLog);
public void mPort_DataRecDownloadLog(object sender, EventArgs e)
int bnread = mPort.BytesToRead;
char a_char = new char[bnread];
int nread = 0;
nread = mPort.Read(a_char, 0, bnread);
string str = new string(a_char, 0, nread);