views:

317

answers:

2

I made an application in C# which sends 11 byte of data to a serial port using:

            port = new SerialPort("COM1");
            port.BaudRate = 9600;
            port.DataBits = 8;
            port.Parity = Parity.None;
            port.StopBits = StopBits.One;
            port.ReadTimeout = 1000;
            port.WriteTimeout = 1000;
            port.Open();

            byte[] buffer = new byte[11];
            buffer[0] = 0;
            buffer[1] = 0;
            buffer[2] = 0;
            buffer[3] = 3;
            buffer[4] = 2;
            buffer[5] = 4;
            buffer[6] = 1;
            buffer[7] = 20;
            buffer[8] = 50;
            buffer[9] = 0;
            buffer[10] = 120;
            port.Write(buffer, 0, 11);

Then I wrote another application to test the previous one. I would like to check if the 11 bytes were correctly sent. In this application I use:

        using (SerialPort port = new SerialPort("COM1"))
        {
            // configure serial port
            port.BaudRate = 9600;
            port.DataBits = 8;
            port.Parity = Parity.None;
            port.StopBits = StopBits.One;
            port.Open();

            for (; ; )
            {
                byte[] b = new byte[11];
                port.Read(b, 0, 11);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 11; ++i)
                {
                    sb.Append(b[i]);
                    sb.Append(" ");
                }
                Console.WriteLine(sb.ToString());
            }
        }

to receive bytes. The problem is that, after sending something like this:

0 0 0 3 2 4 1 20 50 0 120

I receive:

0 0 0 0 0 0 0 0 0 0 0
0 0 3 2 1 4 20 50 0 120 0

Why does it happen? What kind of error is there in my code? Thank you

+3  A: 

You're not checking the result from port.Read(). It returns the number of bytes read, not the number of bytes requested. The output loop then needs to use this result as the upper limit.

Although you've setup timeouts on the sending side, you'll also need them on the reader too.

devstuff
I solved my problem. There are two ways. The first is what you described. The second is: register an handle to the event DataReceived and call Read with BytesToRead bytes.
Maurizio Reginelli
+1  A: 

To check what happens on a serial connection on my pc i use two tools:

  • com0com: A emulator that creates two serial ports on a pc which can communicate with each other. The only drawback i encountered, is that you should set for both a COM port below 10.

  • Free Serial Port Monitor: It sniffs on a com port and can show every byte that runs over the line. The only drawback i encountered, is that it sometimes has problems to disconnect and will only start again after a restart of the whole pc. So if you start a session with it, be sure you don't disconnect or accidentally close the application before you close the connection within your testing application.

Oliver
Very helpful tools, thank you very much.
Maurizio Reginelli