I'm using the DataReceived from the V.2 .NET SerialPort class. I'm communicating with a microprocessor board and i can write to it fine. I'm having trouble reading from it. It sends a single byte at a time without an EndCharacter. I can see the byte being sent on my oscope and with an older Turbo Basic program it does read the byte. My problem is that my DataReceived event isn't being thrown. I have my ReceivedBytesThreshold set to 1 and i still can't see anything. Here is my code just see if the event can be thrown:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceivedMsgBox(
"here") End SubHere is my code to try to read anything that is received that doesn't use the DataReceived Event.
Private Function serialReceive() As String Dim tempstr As String = "" SerialPort1.Open() Try Label1.Text = "HI " + SerialPort1.BytesToRead.ToStringtempstr = SerialPort1.ReadExisting
Label4.Text = "tempstr " + tempstr Catch ex As ExceptionMsgBox(ex.Message)
FinallySerialPort1.Close()
End Try Return tempstr End FunctionI call this after every time i send a byte. I hope someone can help me. thanks!

SerialPort Class Help DataReceived
betolea
See the thread http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1379397&SiteID=1 "How does SerialPort handle DataReceived " and especially the answer from nobugz and my last answer.
PaulCzy
aaarrr
In my application I also don't have a dataReceived event thrown every time when some data is sent from my microcontroller to the pc.
When the microcontroller is sending only 1 byte, the pc is able to receive it correctly. When the microcontroller is sending a bytestream (i.e. 7 bytes at a time), inaccuracy errors in the reception occur. On the oscilloscoop I do see the transmission of all the bytes, so the problem likely is in the invocation of the event.
Decreasing the baudrate or increasing the serialport.ReceivedBytesThreshold is no solution.
Reasons for the failure I'm thinking of are the following.
I'm not using CTS, RTS nor DTR. Does anyone know whether this is the reason for the failure
Are too many events thrown
Best regards
Tom_MTB
Hi,
I.m no expert on VB.Net but a man who is wrote a program called serial port chat.
The receive section explains the use of Delegate to receive data. Apparantly the receive event runs under its own thread and can upset the main thread.
The article appears on the DEVX.COM website. Type in SerialPortChat to the search page.
(I cannot get the link to paste here)
The article is very explicit and clarified the use of SerialPort1 function enough for me to use it as a stand alone VB application to talk to two PCs, then to a PIC micro as well.
It only handles ascii characters, so binary is a little difficult unless converted to text strings first.
J Garcia ARB
See my previous post.
DEVX.Com website, SerialPortChat.
This works fine with ASCII chars, and if I can get it to work, anyone can.
Regards
Eias Nabhan
I am not sure about why the DataReceived event handler is not being called. Including all of the code that accesses SerialPort1 would help. Some things to keep in mind the DataReceived event handler will not be called if you call Close() on the SerialPort or if you no longer have any references to the SerialPort like after the following method terminates:
public sub Write
Dim mySerialPort as new SerialPort("COM1")
mySerialPort.WriteLine("Hello World")
mySerialPort.DataReceived = AddressOf MySerialPortDataReceived
end sub
The problem with you serialReceive method is that it will not read any data that is received before you call SerialPort1.Open(). So if you do something like the following you will potentially loose data:
SerialPort1.Open()
SerialPort1.WriteLine("Hello World")
SerialPort1.Close()
serialReceive()
You should not be opening and closing the SerialPort every time you read or write to it. In general you should open it once and close it once.
Ryan Byington [MS]
rswipe
I was not able to repro this problem. I wrote the following repro using a null modem cable between COM1 and COM2 and did not run into the problem that you hit. Is it possible that the device is sending the extra characters
public class Repro
{
static SerialPort com1;
public static void Main()
{
SerialPort com2;
com1 = new SerialPort("COM1");
com2 = new SerialPort("COM2");
com1.Open();
com2.Open();
com1.DataReceived += COM1DataReceivedHandler;
com2.WriteLine("Hello");
com2.WriteLine("World");
System.Threading.Thread.Sleep(5000);
com1.Close();
com2.Close();
}
private static void COM1DataReceivedHandler(Object sender, SerialDataReceivedEventArgs e)
{
while(0 < com1.BytesToRead) {
Console.WriteLine(com1.ReadByte());
}
}
}
amirmira
Hi, I'm using the ir as a serial COM, I can transmit data, but i can receive anything.
The event DataReceived is not generated and the property BytesToRead always is zero.
Thanks,
Juan Carlos
Manotas
TKTOCK
The state of the CTS, RTS, or DTR pins should not affect the DataReceived event handler. I am not sure if this address your problem but the DataReceived event handler is not guaranteed to get called for every ReceivedBytesThreshold number of bytes received.
For example if your device sends 3 packets with a length of 7 bytes one right after another and ReceivedBytesThreshold is set to 7 the DataReceived event handler will likely only be called once. In your DataReceived event handler you will have to expect that multiple packets might have been called.
Ryan Byington [MS]
d_Sun
Doing some small tests make me think that the DataReceivedEvent is only thrown when a next byte is received through the serial port.About your program screenshot: you send via COM2
char('H') char('e') char('l') char('l') char('o') char(13) char(10) char('W') char('o') char('r') char('l') char('d') char('\r') char(13) char(10)
right (as the end sequence in Windows is '\r' '\n') Are you sure you receive all of these characters
FYI, here are my serialport properties:
- BaudRate: 57600
- Databits: 8
- DiscardNull: false
- DtrEnable: false
- GenerateMember: True
- Handshake: None
- Modifiers: private
- Parity: None
- ParityReplace: 63
- PortName: COM1
- ReadBufferSize: 50
- ReadTimeout:-1
- ReceivedBytesTreshold: 1
- RtsEnable: false
- Stopbits: One
- WriteBufferSize: 50
- WriteTimeout: -1
My best regards,Koen
PremSamuel
I agree to you, Ryan, about the ReceivedBytesTreshold. Can you tell me whether using the property this->serialPort1->BytesToRead can help getting al the packets that has been sent
I noticed that only with the last received byte errors in the DataReceived event occur. I solved this in a not-so-clean way by adding an additional trailerbyte.
But now another problem happened: when reading a byte from the serialport using the function this->serialPort1->ReadByte() which happens to be character 10 (line feed), automatically a character 13 (carriage return) is added. The problem of adding additional bytes when reading from the serialport is mentioned in msdn, but only when reading form a bytestream and a charstream (which I'm not doing). Changing the property this->serialPort1->Newline to a value different of line feed is no help. Is there a clean way to overcome this problem. Now I solve my problem by checking whether a character 13 follows the character 10.
An extract of the problem code is below (VC++):
System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
DataReceivedCallback^ dr;
dr = gcnew DataReceivedCallback(this,&casestudy106051117::Form1::replyhandling);
try{
while (this->serialPort1->BytesToRead > 0)
this->Invoke(dr,gcnew cli::array<System::UInt16^>(1){gcnew System::UInt16(this->serialPort1->ReadByte())});
}catch(InvalidOperationException^ e){e;
}catch(System::IO::IOException^ e){e;
}catch(TimeoutException^ e){e;
}
delete dr;
}
mvarblow
Have microcontroller and want to send data to PC...
Anyone has had succes with this