Hello all! I'm working on a project that involves an Arduino (Uno currently, Mega being used to debug) connected via USB to my PC and a program written in C# (.Net 4.5.2) connected via serial to control it. I'm using the standard SerialPort object. The complication is it's an ASPx page which lives, executes, and dies with each connection. I have JS on the page making async calls back to the server. Each call causes the code to live, execute, die. Each Execute instantiates the serial port, opens it (dtr is off, so no reset on the arduino), sends data (a packet of bytes), and dies. Data packets are {command byte, payload size byte, [payload bytes]}. The Arduino is supposed to .read() the command and size bytes, then:
byte payload[numBytes];
Serial.readBytes((char*)payload, numBytes);
read the payload in. Example packet: {1, 3, 243, 15, 75}
The problem is, sometimes it will do this just fine. It will pass exactly as expected and the arduino will get it and perform as expected. Sometimes however, it seems .NET on the SerialPort.Open() command will send bytes as the arduino will report that it received command 240 (only have commands 1-7 or so right now) with 0 additional bytes. Example bad packet {240}. It's ALWAYS 240 from what I've seen and it seems to go through upon subsequent .Open() commands sent to the arduino. Has anyone else seen this or anything like it? Any known issues with opening ports repeatedly to the arduino? Do I need to close/re-open on the arduino end as well?
Thanks!