I am a very old Arduino, Python, and C++ newbie. If I don't have the correct Forum syntax, please advise.
Two programs:
Python 3.6 running on Windows 10 Ver 1803 64 bit and
Arduino IDE Ver 1.8.5 (windows store 1.8.10.0) running on Arduino Mega 2560 rev 3
Python 3.6 program:
import serial
ser = serial.Serial('COM6', 9600, timeout=0, parity=serial.PARITY_EVEN, rtscts=1)
s = 5
r = 5
ser.write(s)
print (s)
r = ser.read()
print(r)
Arduino IDE (c++) program running on Arduino Mega 2560 Rev 3
int s = 1; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
if (Serial.available() > 0) {
s = Serial.read(); // read the incoming data
}
if (Serial.available() > 0) {
Serial.write(s); //echo the data received
}
}
Both programs run with no errors but when Python sends a 5 the Arduino returns a b’‘
Here is the screen when Python script executes:
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
RESTART: C:\Users\Bill\AppData\Local\Programs\Python\Python36-32\odell serial test 5.py
5
b''
why do you call Serial.available() twice on each loop
call it once for each character received and printed, e.g.
void loop() {
if (Serial.available() > 0) {
s = Serial.read(); // read the incoming data
//}
// if (Serial.available() > 0) {
Serial.write(s); //echo the data received
}
}
if you are not sure if it is Python or the Arduino giving problems test your code Arduino code with the Serial Monitor first
First, when dealing with serial communications between Python and Arduino, I recommend sticking with character strings for sending numerical data, before tackling binary transmission. I would recommend Robin2's tutorial on Serial communications for very strong methods of receiving data on the Arduino side. Serial Input Basics - updated - Introductory Tutorials - Arduino Forum
With Python3, there are string issues relating to unicode an ascii characters. There may be several methods to deal with this, but I choose to use encode() and decode() which convert to UTF-8 which works for Arduino.
Your Python code for opening the Serial port was not correct. You also should have a short delay after opening the port to allow for the Arduino to reset.
There were issues with the Arduino code which others have pointed out.
Try this pair of Arduino and Python codes
char s; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
if (Serial.available() > 0) {
s = Serial.read(); // read the incoming character
Serial.print(s); //echo back received character
}
}
import serial
import time
#ser = serial.Serial('COM6', 9600, timeout=0, parity=serial.PARITY_EVEN, rtscts=1)
ser = serial.Serial('COM6', 9600)
print ("Opening Serial Port")
time.sleep(2); #two seconds for arduino to reset and port to open
s = "5"
r = "0"
print ("Sending " + s)
ser.write(s.encode()) #default encoding UTF-8 for Arudino bytes
print("Echo back from Aduino")
r = ser.read()
print (r.decode()) #eliminate the 'b' bytes indicator default UTF-8
Now for my next problem: How do I do this for integers?
Please explain what you want to do? Send a character string representing a number larger than a byte (e.g."1234"), or send the raw binary data of the number 1234?
I am a very old Arduino, Python, and C++ newbie.
I recommend that you stick with sending and receiving character strings, and using atoi() (and atof() for floats) to convert the strings to "numbers" rather than diving into sending/receiving raw binary data. It can get pretty complicated on the Python side.
Then you can have Python send a string representing an integer value like "1234", and you will use atoi (ascii to integer) to convert it to a numerical value for use in the arduino program.