Hello all, frustrated Arduino noob here.
I'm trying to communicate with an Arduino using pyserial, and they don't seem to be communicating. I'm attempting to isolate the issue by reducing the problem down to just trying to make the LED turn on and off, but I'm still finding no luck after hours of googling and fiddling. My Python code is as follows:
import serial, struct, time
ser = serial.Serial(port='/dev/ttyACM0')
time.sleep(3)
a = 1
ser.write(struct.pack(">B", a))
and the Arduino code is
int ledPin = 13;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
while(!Serial);
}
void loop() {
if(Serial.available()){
int input = Serial.read();
Serial.println(input);
if(input == 49 || input == 1){
digitalWrite(stepPin, HIGH);
}
if(input == 0 || input == 48){
digitalWrite(stepPin, LOW);
}
}
}
The perplexing thing is that, if I open the serial port in a Python terminal, then open the Serial Monitor in the Arduino software, everything works fine. The light turns on and off with a 1 or a zero using the code above in the Python terminal...but if I don't open the Serial Monitor, or I exit the serial monitor then close and reopen the port, nothing at all...I've been trying every variation of struct.unpack or encode() in case it's how the input is being parsed, but it doesn't look like Serial.available() is ever evaluating as nonzero...I feel like I've been searching for hours and have tried everything I could find or think of, so I'd be really grateful if anyone could provide some insight as to what is happening here.