0

I am working on a C# application. My application has to do serial communication with a hardware device. The device is connected with my system on "COM4" com port. Code:

serialPort = new SerialPort("COM4", 2400, Parity.Odd, 8, StopBits.One);
serialPort.WriteTimeout = 5000;
serialPort.ReadTimeout = 5000;
serialPort.Open();
serialPort.Handshake = Handshake.None;
serialPort.DtrEnable = true;
serialPort.RtsEnable = true;

After that i do a write operation, this works fine. Code:

private void WriteMessage(byte[] busMsg)
{
    BinaryWriter writer = new BinaryWriter(serialPort.BaseStream);
    writer.Write(busMsg);
    writer.Flush();
}

After write, when i do ReadByte operation, i get the timeout exception. Code:

private byte ReadAEBusMessageResponse()
{
    BinaryReader reader = new BinaryReader(serialPort.BaseStream);
    return reader.ReadByte();
}

I read somewhere on google that BaseStream might cause issue, so i tried the below code for reading but, still no luck and i am still getting the timeout exception.

private byte ReadAEBusMessageResponse()
{
    SerialPort currentPort = serialPort;
    return Convert.ToByte(currentPort.ReadByte());
}

I do get the response when i try communication with Hercules, so i assume there is no issue with response from device. What am i doing wrong with serial communication ? any help would be much appreciated.

9
  • "After write, when i do ReadByte operation" What if not to write and just read, would result be different? Commented Apr 8, 2020 at 8:07
  • let me check, but i have to do write, as it is a command which i send to the device and device respond according to that command Commented Apr 8, 2020 at 8:09
  • @LouisGo still getting timeout exception which is pretty obvious Commented Apr 8, 2020 at 8:15
  • It's possible that the device did not really get the message from system, thus there is no reply ( byte ) to read. Check SerialPort.BytesToRead after your write might help. It would help a lot if your device has ability to show received message. Commented Apr 8, 2020 at 8:23
  • getting 0 in "SerialPort.BytesToRead". The device is a real hardware device, so can not show its received message. @LouisGo Commented Apr 8, 2020 at 8:43

1 Answer 1

1

I have only found the async event to work. I have created my own byte receive buffer and then I handle the event:

static byte[] RXbuffer = new byte[512]; //"static" so it can be used all over

public void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            Byte_Size = SerialPort.BytesToRead;

            SerialPort.Read(RXbuffer, 0, Byte_Size);

            /* Return to UI thread */
            this.Invoke(new EventHandler(DisplayText));                       
        }

the DisplayText is a method to print the buffer in a textbox.

private void DisplayText(object sender, EventArgs e)
        {
            int i = 0;
            byte[] Received_Bytes = new byte[Byte_Size];

            while (i < Byte_Size)
            {             
                Received_Bytes[i] = RXbuffer[i];
                i++;
            }
                

            TB_Info.AppendText("\r\nBytes Received: ");
            TB_Info.AppendText(Byte_Size.ToString());

            TB_Info.AppendText("\r\n--> ");
            TB_Info.AppendText(BitConverter.ToString(Received_Bytes));
            TB_Info.AppendText("\r\n");

           //Array.Copy(RXbuffer,Full_RX_Buff,)
        }

You can then read/manipulate the values later.

***Don't forget that in the SerialPort object that put on your form, you have to go into the properties/events dialog and assign the function that handles the receivedata event!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.