1

I'm writing two applications, which are supposed to communicate to each other. One is written in C++ for Arduino (Teensy, more specifically), second is a desktop app written in C#.

Usually the communication works correctly. However, sometimes I have trouble receiving information from Teensy. In those situations the log says:

00:00:00.00    | Serial port service      | Trying to open port COM4...
00:00:00.00    | Serial port service      | Closing port COM4...
00:00:00.00    | Serial port service      | Opening port COM4...
00:00:00.00    | Serial port service      | Opened port COM4...
00:00:00.00    | Serial port service      | Bytes to read: 0

The code behind this log looks like following (serialPort is a System.IO.Ports.SerialPort):

public void OpenPort(string port)
{
    logService.AddLog(SerialPortLogSource, $"Trying to open port {port}...");

    ClosePort();

    serialPort.PortName = port;

    try
    {
        logService.AddLog(SerialPortLogSource, $"Opening port {port}...");
        serialPort.Open();
        if (serialPort.IsOpen)
            logService.AddLog(SerialPortLogSource, $"Opened port {port}...");
        else
            logService.AddLog(SerialPortLogSource, $"Open call exited, but port is not opened");

        opened = true;
        this.port = port;

        configurationService.Configuration.LastUsedPort = port;
        configurationService.Save();
        logService.AddLog(SerialPortLogSource, $"Bytes to read: {serialPort.BytesToRead}");

        DataReceived?.Invoke(this, EventArgs.Empty);
    }
    catch
    {
        logService.AddLog(SerialPortLogSource, $"Failed to open port {port}...");

        opened = false;
        this.port = null;
    }
}

When I afterwards initiate communication from the device, I get nothing. However, what's interesting, if I only open the serial port monitor in Arduino IDE, I receive some information, which I was supposed to get in my application and moreover from this point on, communication with my application starts working. This is a scenario, which is reproducible every single time.

The whole project is available on GitLab, more specifically, this is MacroKeyboard.Satellite.

What may cause such weird behavior? What may happen in Arduino IDE's serial port monitor, what unclogs the serial port?

Spook
  • 25,318
  • 18
  • 90
  • 167
  • From a debug perspective have you tried opening a port outside of this method and just checking the input buffer at a regular interval? – KyleKW Mar 29 '22 at 15:12
  • I think that the problem may lie elsewhere. From the behavior of the device it seems like that if I don't open the Serial Monitor from Arduino, device doesn't pass `while (!Serial) ;` part. It looks like some magic must happen for the virtual USB Serial port to initialize properly. However, I have no idea, where to search for answers in this regard. – Spook Mar 29 '22 at 15:16
  • I would suggest posting the sketch as well. This may be more of an electronics stack exchange question. I have done similar to what you have shown here, and have not seen the same behavior. – KyleKW Mar 29 '22 at 15:21
  • 1
    Note that "COM" is sometimes used for "Component Object Model". So I would consider changing the title to something like "Serial COM port" to avoid possible ambiguities. You might also want to read some [criticism of system.IO.Ports.SerialPort](https://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport) – JonasH Mar 29 '22 at 15:22
  • The following may be helpful: https://stackoverflow.com/questions/70441938/get-scale-weight-over-serial-from-mettler-toledo-rice-lake-scale/70614758#70614758 and https://stackoverflow.com/questions/66666331/serialport-doesnt-actually-open-the-virtualcom-port/66666984#66666984 – Tu deschizi eu inchid Mar 29 '22 at 15:35

0 Answers0