Hello, I am Having trouble with Pyserial and Arduino. Started reading about Python/Pyserial to arduino, followed the links passed on from forum post DE2120 Scanner, and installed Python3 no problem. Pyserial was not as easy, after many different attempts I installed using sudo easy in the install line which worked.
Following the link https://www.instructables.com/Interface-Python-and-Arduino-with-pySerial/
I copied code for Arduino uploaded, no problem, but when I use the python script I get errors:
… print ser.readline() # Read the newest output from the Arduino
File “”, line 4
print ser.readline() # Read the newest output from the Arduino
^
SyntaxError: invalid syntax
sleep(.1) # Delay for one tenth of a second
File “”, line 1
sleep(.1) # Delay for one tenth of a second
IndentationError: unexpected indent
if counter == 255:
File “”, line 1
if counter == 255:
IndentationError: unexpected indent
counter = 32
Here is the whole thing:
import serial
ser = serial.Serial(‘/dev/tty.usbmodem14101’, 115200) # Establish the connection on a specific port
counter = 32 # Below 32 everything in ASCII is gibberish
while True:
… counter +=1
… ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
… print ser.readline() # Read the newest output from the Arduino
File “”, line 4
print ser.readline() # Read the newest output from the Arduino
^
SyntaxError: invalid syntax
sleep(.1) # Delay for one tenth of a second
File “”, line 1
sleep(.1) # Delay for one tenth of a second
IndentationError: unexpected indent
if counter == 255
File “”, line 1
if counter == 255
IndentationError: unexpected indent
counter = 32
I don’t know Python, but after changing, print ser.readline() to print (ser.readline) and if counter == 255: to if counter == 255 I have this:
Last login: Fri Dec 18 09:33:50 on ttys000
@Jasons-MacBook-Pro ~ % python3
Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
from time import sleep
import serial
ser = serial.Serial(‘/dev/tty.usbmodem14101’, 115200) # Establish the connection on a specific port
counter = 32 # Below 32 everything in ASCII is gibberish
while True:
… counter +=1
… ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
… print (ser.readline) # Read the newest output from the Arduino
… sleep(.1) # Delay for one tenth of a second
… if counter == 255
File “”, line 6
if counter == 255
^
SyntaxError: invalid syntax
counter = 32
Arduino compiled fine.
Code for Arduino :
void setup() {
Serial.begin(115200); // set the baud rate
Serial.println(“Ready”); // print “Ready” once
}
void loop() {
char inByte = ’ ';
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}
delay(100); // delay for 1/10 of a second
}
I feel like there is either, a minor error in Python code, Pyserial has had install issues, something has changed from Py 2.7 to Py 3.9, or my system isn’t correct because I see : @Jasons-MacBook-Pro ~ % (I deleted my full Name) instead of : @Jasons-MacBook-Pro ~ $, the default setting is /bin/zsh.