I have a C# dll in which I want to read my Arduino's serial port. For this test, I'm just sending out "1 1". I can't get C# to read this, I always get a timeout error. I tried various baud rates, various combinations of port.ReadLine, port.ReadByte (in C#) and Serial.print and Serial.println (in Arduino). The Arduino is a Teensy USB CDC (if that matters). The port settings in the Device Manager seem OK, as in a serial port monitor, I can see the Arduino output (seemingly) fine:
C# code.
int interval = 25;
using (SerialPort port = new SerialPort("COM9", 57600, Parity.None, 8, StopBits.One)) {
port.ReadTimeout = 7000;
port.NewLine = "\r";
try {
string output = null;
port.Open();
while (output == null && loopCnt < (timeOut / interval)) {
output = port.ReadLine();
Thread.Sleep(interval);
loopCnt++;
}
MessageBox.Show("output: [" + output+"]");
Arduino code:
void setup() {
Serial.begin(57600);
}
void loop() {
Serial.print("1 1");
Serial.print('\r');
delay(5000);
}