Dear Ron,
Thanks to your Serial Communication script, which was post in FAQs, I finally communicate HMI with another device using COM port. Sending bytes from HMI works flawless, but I encounter some problems when try to read data in serial buffer.
I try using
Code:
Read(byte[] byte, int index, int count)
But it's very slow to get data from buffer
And, after some googling, I found this comment:
Quote:
Read is one of the most commonly misunderstood methods. Because the help files do not describe the real behaviour, it it is logical to assume that the method:
BytesReceived = YourCOMPort.Read(buffer, offset, count)
is a blocking method, which does not return before "count" number of bytes are received. It is not!. If there are bytes available on the serial port, Read returns up to "count" bytes, but will not block (wait) for the remaining bytes. If there are no bytes available on the serial port, Read will block until at least one byte is available on the port, up until the ReadTimeout milliseconds have elapsed, at which time a TimeoutException will be thrown. Unless you check the actual number of received bytes, you may believe that all bytes in your buffer are valid. This may not be the case!
PS! SerialPort in .Net 3.5 is so full of errors that it may be regarded as useless. Use .Net 2.0 or a third party DLL.
So, instead, I try Readline() method to read data with LF end code, but it won't get better.
Did I make some mistakes when try using Read() or Readline()?
And moreover, I couldn't use DataReceived event, it just raise event 1 time only.
Thanks,
Phong Duong