1

Following up with Serial Port Communication solution I implemented the below design. My code uses com8 to communicate with a Serial Port Utility Application that is listening on com9 within the same machine, then sending back (manually I type a message and press a button)

In my main I do this:

MyClass MyObj = new MyClass();
var message = MyObj.SendThenRecieveDataViaSerialPort("Test");

And then in my class I have this:

private static SerialPort MainSerialPort { get; set; } = new SerialPort();
private static string _ReceivedMessage;
private Thread readThread = new Thread(() => ReadSerialPort(ref _ReceivedMessage));

public string SendThenRecieveDataViaSerialPort(string _Message)
{
    MainSerialPort = new SerialPort("com8", 9600);
    MainSerialPort.ReadTimeout = 5000;
    MainSerialPort.WriteTimeout = 5000;
    MainSerialPort.Open();
    readThread.Start(); // 1

    try
    { // 2
        MainSerialPort.WriteLine(_Message); // 3
        readThread.Join(); // 6 - Console pops and waits
    }
    catch (TimeoutException ex)
    {
        Console.WriteLine("Exception in SendThenreceive");
    }

    return _ReceivedMessage;
}

private static void ReadSerialPort(ref string _message)
{
    try
    { // 4
        _message= MainSerialPort.ReadLine(); // 5 
    }
    catch (TimeoutException ex)
    {
        // 7 - when time outs
    }
}

However, it is throwing an error at step 7 saying:

{"The operation has timed out."}

InnerException: null

Could you tell me where I am going wrong? Please and thanks.

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • Didn't we already go through this? You *must* set the Handshake property, it is not optional. If you use Handshake.None then you *must* set the DtrEnable and RtsEnable properties to true. The utility you use was only made to talk to a device, not your program. Local loopback to get data sent by the PC back to the PC requires a null modem cable that connects com8 to com9. Your thread is broken, it can only ever read once. Don't use a thread. – Hans Passant Sep 06 '16 at 13:28
  • Hey Mr Hans \o/ Once again :). I am still struggling yup :( I have a hardware cable from com8 to com9 ! And I couldn't reproduce the problem from last time so I went with this multi-threading. I am trying `MyPort.Handshake = SetPortHandshake(MyPort.Handshake);` as [MSDN](https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readline(v=vs.110).aspx) now !! – Khalil Khalaf Sep 06 '16 at 13:35

1 Answers1

5

ReadLine waits until it sees the SerialPort.NewLine string. If this doesn't arrive within SerialPort.ReadTimeout the TimeoutException is thrown. So don't miss to send the NewLine!

Here is an alternative version without NewLine.

byte[] data = new byte[1024];
int bytesRead = MainSerialPort.Read(data, 0, data.Length);
_message = Encoding.ASCII.GetString(data, 0, bytesRead);
JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • Hi Jeff. I had 5 seconds to type `back` and press a button. I am pretty sure did not miss to send a NewLine. – Khalil Khalaf Sep 06 '16 at 13:30
  • Try Read instead of ReadLine. – JeffRSon Sep 06 '16 at 13:32
  • 1
    Read doesn't take 0 arguments – Khalil Khalaf Sep 06 '16 at 13:36
  • 1
    Sure enough you must use the correct method parameters. It's a test whether data arrive at all. You could try ReadExisting as well, but it doesn't block until you're able to type your answer. And - again - compare what you send with the actual value of NewLine. Often CR and LF may be confused. – JeffRSon Sep 06 '16 at 13:41
  • It worked. Now both your Read() with a string `back` works, and ReadLine() with a string `back\n` works as well. Thanks ! – Khalil Khalaf Sep 06 '16 at 13:57