Pyserial to Arduino problem

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.

Also the Tx led on Arduino is flashing but not receiving in Py.

Can anyone help please.
Thank you.
Jason

Have been trying different code, I now have something that does something, but not what it is supposed to do. Here is new code:
from time import sleep
import serial
ser = serial.Serial(’/dev/tty.usbmodem14101’, 115200)
counter = 32
while True:
counter +=1
ser.write(str.encode(chr(counter)))
print (ser.readline)
sleep(.1)
counter == 255
counter = 32

and this is readout:
False
1
<built-in method readline of Serial object at 0x10775bf70>
^CTraceback (most recent call last):
File “”, line 5, in

Any help would be great,
Jason

Ok, Here is Python3 code that will give the correct printout:

>>> from time import sleep
>>> import serial
>>> ser = serial.Serial('/dev/tty.usbmodem14101', 115200)
>>> counter = 47 
>>> while True:
...      counter += 1
...      chr(counter)
...      if counter == 57:
...          counter = 47      
...          ser.write(str.encode(chr(counter)))
...          print(str(ser.readline))                 
...          sleep(5)
... 
'0'
'1'
'2'
'3'
'4'
'5'
'6'
'7'
'8'
'9'
1
<built-in method readline of Serial object at 0x1070deb50>

As you can see this is a little different to the original dowloaded from the instuctables link.
I hope this can help anyone with similar issues, (and doesn’t know Py3).
And now back to my actual project.

Jason

2 Likes

good to hear you got it sorted. If you any more questions please feel free to post them up

Its because in Python 3, print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement. But if you write this in a program and someone using Python 2.x tries to run it, they will get an error. To avoid this, it is a good practice to import print function:

from __future__ import print_function

Now your code works on both python2 and python3

4 Likes