How to send serial data from a Python script to Arduino on Windows - nothing works

I cannot send serial data correctly from a Python script to Arduino Uno. I use 9600 baud and Arduino correctly resets, but it does not read the char that I am sending with a Python script. I am calling time.sleep()to ensure that the reset on the Arduino does not interfere, and I am using Windows 7. I must clarify by saying that my desktop launches a python script and connects via USB to the Serial of my Arduino Uno. Then I have the RX and TX pins (pins 0 and 1) of my Uno connected to my Mega Serial1 (pins 18 and 19). Then I use the Serial Monitor in the Arduino IDE on my laptop (which uses Mega regular Serial) to look at what Uno sees. Here is the code for 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");
  }
}

Uno:

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

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

, , 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

, , Doom3 Arduino , Arduino . ++, ++, .

+3
5

. , arduino IDE - . auto-reset.

Arduino , . .

, ++ , .

+2

, IDE Arudino python. (, ). python script, IDE arduino ardiuno, , msg, ( ). arduino , .

, IDE Arduinos. ( Arduinos.) . Arduino SoftwareSerial , d0/d1, FTDI, IDE.

python script, / .

+1

, , Arduino. , , , .

0

:

. , Serial Monitor - Arduino. , , . Arduino , pyserial - Python Arduino.



:

1) Python ser.write() ser.read()! , .


2) Arduino_Master_Delta (python) Arduino, .

Arduino_Master_Delta

:

import Arduino_Master_Delta as amd
writeSerial(23, msg=' Whatever you want to send to the serial monitor')
# Data will be encoded into bytes and hence use the Serial.readString() in Arduino code
# and then convert the string to your desired data type!

, !

, Python. .

0

pyserial. pyserial Windows, , : Python pyserial. COM- . , Arduino.

The received message will be printed in a while loop using the readline () function. A 100 millisecond delay is also used here, as in the Arduino sketch. Note that the pyserial readline () function requires a timeout when opening the serial port (pyserial: PySerial ReadLine documentation).

  import serial
    s = serial.Serial('/dev/tty.usbmodem1411', 9600, timeout=5)
    s.write('2')
0
source

All Articles