System.IO.Ports.SerialPort problem to read binary data

P

pavel.orehov

Hi,

I am using .Net SerialPort class to read binary data form COM port. The

problem is that i am getting some bytes of data corrupted. Some of
bytes are changed by ParityError byte (default=63). Strange that i
don't get an error event.


Here are the configuration of the port:


portConf.PortName = "COM1";
portConf.BaudRate = 4800;
portConf.DataBits = 8;
portConf.Parity = Parity.None;
portConf.StopBits = StopBits.One;


Besides i have the other application on C++ on the same computer that
is working and reading correct data.


Any ideas ?


Thanks,
 
P

pavel.orehov

I also tried to change Encoding to UTF8 and now I see zeros on that
bytes where before were parity error byte.
 
D

Dick Grier

Hi,

How are you reading data? You should be using something like:

Dim Count As Integer = SerialPort.BytesToRead

Dim Buffer(0 To Count - 1) As Byte

SerialPort.Read(Buffer, 0, Count)

(Syntax similar for C#)

(ReadExisting works only for ASCII, text data.)

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
P

pavel.orehov

It was my mistake.
First, I used ReadExisting and should be used Read that returns byte[].
Second, I used TextWriter after to write this data to the file.
After i switched to BinaryWriter and changed to Read, everything works
fine.
Thanks for help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top