So, I am trying to send a character from my python program to Arduino program via the COM5 serial port. However, It wont let me run both at the same time because it says that the port is busy.
This is my python code:
import serial
import time
try:
arduino = serial.Serial()
arduino.port = 'COM5'
time.sleep(2)
except Exception as e:
print(e)
arduino.open()
arduino.write(b'a')
time.sleep(2)
arduino.close()
This is my Arduino code which is the exact same of Robin2's serial basics code.
char receivedChar;
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvOneChar();
showNewData();
}
void recvOneChar() {
if (Serial.available() > 0) {
receivedChar = Serial.read();
newData = true;
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChar);
newData = false;
}
}
Can anyone tell me what I am doing wrong? Thanks.