1

So I'm trying to read data from a COM port that is connected to my laptop through USB. I know that the connection is successful and that the port is opened when calling on the Connect() method, however when I send a command to the port, it sends to the port, but once ReadLine() is called, it just hangs out in the terminal doing nothing. I've done some research on this issue and I see that many people have issues with SerialPort, but I can't seem to solve my own instance of the problem.

An example of a command I might send to the machine I'm working with is something as simple as "son" where I send the string to the port and the shaker device should simply turn on and start shaking. So I should send this command to the port and receive something back like "ok" to show that it is running correctly, but right now nothing happens once the code reaches the ReadLine().

Can someone please help me walk through where I might be going wrong in trying to read data from this port? Any help would be much appreciated.

   private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {

        string data = Port.ReadExisting();

    }

    public void Connect()
    {
        //MessageBox.Show(Port.IsOpen.ToString());
        Port = new SerialPort(Comport, 9600, Parity.None, 8, StopBits.One);
        Port.DtrEnable = true;
        
        Port.DataReceived += SerialPortDataReceived;
        Port.Open();
        MessageBox.Show(Port.IsOpen.ToString());
        Port.NewLine = Environment.NewLine;
    }

    public string SendCommand(string command)
    {
        
        Port.Write(command + "\r");
       
        string result = Port.ReadLine().Trim();
        
        if (result == "e")
        {
            string errorList = GetErrorList();
            throw new InvalidOperationException("BioShake error: " + errorList);
        }
        return result;
    }
Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29
Zayum
  • 79
  • 7
  • ReadLine() waits for Environment.NewLine, which is \r\n on a windows machine. Better read on yourself. – Oliver Jul 11 '22 at 20:06
  • 2
    Best way of debugging is to use Putty to make connection and type commands manually until you get it working. You may be using the wrong return character and should try "\n" instead of "\r". If typing a return in Putty doesn't return anything than the issue is not your c# application. – jdweng Jul 11 '22 at 20:31
  • The following may be helpful: https://stackoverflow.com/a/65971845/10024425. Here are some in VB.NET that may also be helpful: https://stackoverflow.com/a/70614758/10024425 , https://stackoverflow.com/a/67410160/10024425 , https://stackoverflow.com/a/69946343/10024425 – Tu deschizi eu inchid Jul 11 '22 at 20:49
  • Why do you have `string result = Port.ReadLine().Trim();` in `SendCommand` when you're using `SerialPortDataReceived`? Depending on the data that's sent in SerialPortDataReceived you may need to use [ReadLine](https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.readline?view=netframework-4.8) instead of [ReadExisting](https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.readexisting?view=netframework-4.8). Also see [ReceivedBytesThreshold](https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.receivedbytesthreshold?view=netframework-4.8) – Tu deschizi eu inchid Jul 11 '22 at 21:00
  • 1
    @Oliver: That's not correct, it uses the `NewLine` property on the SerialPort instance, not from the `Environment` class. However `SerialPort.ReadLine` is still a horrible function. – Ben Voigt Jul 11 '22 at 22:54
  • Oddly enough replacing `command + "\r"` with `command + Environment.NewLine` as well as doing away with the `SerialPortDataReceived` method fixed my issue, unless I did something completely unrelated that fixed it without me noticing. @BenVoigt – Zayum Jul 12 '22 at 16:02
  • @Zayum: Pedantically you should be using `Port.NewLine`, although that will be equal to `Environment.NewLine` unless you change it. Or use `WriteLine(command)` which will send `NewLine` automatically. Indeed you are correct that receiving two different ways at the same time `ReadLine` in `SendCommand` and `ReadExisting` inside the `DataReceived` handler was causing problems. The `DataReceived` event has many problems, not least of which is that .NET calls your handler on a different thread each time. – Ben Voigt Jul 12 '22 at 16:29

0 Answers0