I'm attempting to read data sent by an Arduino program over the serial port. To make it simple, I've uploaded a program to the Arduino so that it just sends the value of an integer every fifth of a second, and then increments it. I know it works because I can see the output in a serial monitor and it's not really the issue at hand, but here's the code anyway:
#include <SPI.h>
int counter = 1;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
Serial.println(counter);
counter++;
delay(200);
}
The real problem is reading it in with Mathematica. I'm positive the port is COM8, because that's the one selected in the Arduino IDE, and if I try to open the port in Mathematica while the serial monitor in the Arduino IDE is open, it won't open.
Here's my Mathematica code:
ReadSerialPort[] := Module[{serial, dat},
serial = DeviceOpen["Serial", {"COM8", "BaudRate" -> 9600}];
(*dat=DeviceReadBuffer[serial,10];*)
dat = DeviceRead[serial];
DeviceClose[serial];
Print@dat;
]
As you can see, I tried both DeviceRead
and DeviceReadBuffer
(separately), with the same result: $TimedOut
.
I found this Wolfram community post from about two years ago, where apparently there was a bug and the solution is to add "\.\" in from of the COM... but I tried that and it gave me a "doesn't recognize escape character C" error.
What else can I try?
edit: I've also tried doing Serial.write() in the Arduino program, and doing the serial at 115200 baud, but neither fixed the problem.