How to send serial data from Python script to Arduino on Windows - Nothing Works


Question

I can't correctly send data over serial from a Python script to an Arduino Uno. I'm using 9600 baud, and the Arduino correctly resets, but it does not read the char I'm sending from the Python script. I call time.sleep() to ensure the reset on the Arduino does not interfere, and I'm using Windows 7. I should clarify by saying that my desktop is running the python script and connected via USB to the Serial of my Arduino Uno. I then have the RX & TX pins (pins 0 & 1) of my Uno connected to my Mega's Serial1 (pins 18 & 19). I then use the Serial Monitor in the Arduino IDE on my laptop (which uses the Mega's regular Serial) to peek at what the Uno is seeing. Here is the code for the Mega:

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
  Serial.println("Master Ready");
}

void loop() {
  if(Serial1.available() > 0) {
    char inByte = Serial1.read();
    Serial.write(inByte);
    Serial.write("\n");
  }
}

Here is the code for the Uno:

void setup() {
  Serial.begin(9600);
  Serial.println("Slave Ready");
}

void loop() {
 if(Serial.available() > 0) {
   char inByte = Serial.read();
   Serial.write(inByte);
   }
}

And finally, here is the python script:

import sys
import serial
import time

ser = serial.Serial("COM23",9600)
n = int(sys.argv[1])
print n

time.sleep(10)
print ser
print n == 41
if (n == 70):
    ser.write(chr(97))
    print 'a'
elif n == 41:
    ser.write('ggggggg')
    print 'b'
elif n == 42:
    ser.write('hello world')
    print 'c'
elif n == 25:
    ser.write(chr(100))
elif n == 26:
    ser.write(chr(101))
elif n == 22:
    ser.write(chr(102))
elif n == 10:
    ser.write(chr(103))
elif n == 4:
    ser.write(chr(104))
elif n == 14:
    ser.write(chr(105))
elif n == 7:
    ser.write(chr(106))
elif n == 11:
    ser.write(chr(105))
elif n == 5:
    ser.write(chr(106))
elif n == 17:
    ser.write(chr(107))

# head - a - 70
# right bicep - b - 41
# right forearm - c - 42
# left bicep - d - 25
# left forearm - e - 26
# chest - f - 22
# right thigh - g - 10
# left thigh - h - 4
# right shin - i - 11 - 14
# left shin - j - 5 - 7
# waist - k - 17

In case it helps, I'm essentially trying to write the hit locations in Doom3 to an Arduino over serial so that the Arduino can turn a motor on in the proper location on your body. The game code is in C++, and I first tried using a serial library for C++, but that didn't work either.

1
3
12/17/2013 2:37:03 PM

I just came across something similar. The key here is that it worked when communicating with the arduino from the IDE and failed when using some other serial program. You have been bit by the auto-reset feature.

Check out this link on the Arduino Playground for a detailed description of what is going on and what to do about it. The site gives several solutions depending on you skill and comfort level.

I expect your C++ code will work once you implement one of the work arounds from this site.

2
6/7/2012 5:52:53 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon