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;
}