Adam Čuba
Adam Čuba

Reputation: 21

C# serial port communication returning different values in Pascal

i'm programming serial port communication with hardware by our supplier. It's trivial, but I cannot get the right data from the port. I need to write to serial port 2 bytes of address, then receive 1 byte confirmation. HW supplier has testing software, which works exactly like that, but when I write the same bytes, then what I get from it is different.

How should it look like:

Write: 0x00 0x01
Read: 0xAA

How I get results:

Write: 0x00 0x01
Read: 0x3F 0x3F 0xAA - sometimes just 0x3F one time

I'm using SerialPort class in C#. Testing software from HW supplier is written is Pascal(synaser library) - I do not have source code. It seems like C# is doing something more behind, but I cannot find the issue. For data analysis I used Free Device Monitoring Studio to analyze exact data on the bus.

        private bool SendAddress()
        {
            int confirmation;
            byte[] address = { (byte)((Address >> 8) & 0xFFu), Address };
            var confirmed = false;
            var port = new SerialPort(Port, 19200, Parity.Mark, 8);

            port.WriteTimeout = 200;
            port.ReadTimeout = 200;

            StatusReaderService.PortLoggers[Port].Debug(Name + " Opening port for address.");
            port.Open();
            StatusReaderService.PortLoggers[Port].Debug(Name + " Port opened.");
            StatusReaderService.PortLoggers[Port].Debug(Name + " Discarding buffers.");
            port.DiscardOutBuffer();
            port.DiscardInBuffer();
            StatusReaderService.PortLoggers[Port].Debug(Name + " Buffers discarded.");
            StatusReaderService.PortLoggers[Port].Debug(Name + " Writing address: " + BitConverter.ToString(address));
            //port.Write(address, 0, address.Length);
            port.BaseStream.WriteByte(0x00);
            port.BaseStream.WriteByte(0x01);
            StatusReaderService.PortLoggers[Port].Debug(Name + " Address written.");

            try
            {
                for (int i = 0; i < 3; i++)
                {
                    StatusReaderService.PortLoggers[Port].Debug(Name + " Waiting for confirmation.");
                    confirmation = port.ReadByte();
                    StatusReaderService.PortLoggers[Port].Debug(Name + " Confirmation value received: " + BitConverter.ToString(BitConverter.GetBytes(confirmation), 0, 1));

                    if (confirmation == 0xAA)
                    {
                        StatusReaderService.PortLoggers[Port].Debug(Name + " Confirmed.");
                        confirmed = true;
                        break;
                    }
                    else
                    {
                        StatusReaderService.PortLoggers[Port].Debug(Name + " Invalid confirmation value.");
                    }
                }
            } catch(TimeoutException)
            {
                port.Close();
                throw;
            }

            StatusReaderService.PortLoggers[Port].Debug(Name + " Closing port.");
            port.Close();
            Console.ReadKey();

            if (confirmed == false)
            {
                StatusReaderService.PortLoggers[Port].Debug(Name + " Confirmation not received.");
                return confirmed;
            }

            return confirmed;
        }

I'm looping through the result, but thats not the way we want to do it. Didn't someone experience similar problem?

Upvotes: 0

Views: 114

Answers (1)

Adam Čuba
Adam Čuba

Reputation: 21

Thanks everyone for help. kunif revealed the problem.

HW supplier told me that it's receiving address with parity set to 1, but then sends confirmation without parity. It's suppliers problem as it should return data with same parity. So SerialPort class replaced errors with it's defined 0x3F value. In suppliers SW, where Pascal is used, there is no parity check.

Didn't notice that since C# didn't throw any exception and it's my first real experience with serial port.

One more time thanks all :)

Upvotes: 2

Related Questions