# Pyserial to Arduino problem

**URL:** <https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020>\
**Category:** Uncategorized\
**Created:** [18 December 2020 01:15 UTC](https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020 "2020-12-18T01:15:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason136169](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jason136169](https://forum.core-electronics.com.au/u/Jason136169)\
**Post date:** [18 December 2020 01:15 UTC](https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020/1 "2020-12-18T01:15:20Z")

</div>

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/](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.

---

<div class="post-metadata">

**Author:** ![Jason136169](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jason136169](https://forum.core-electronics.com.au/u/Jason136169)\
**Post date:** [18 December 2020 19:58 UTC](https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020/2 "2020-12-18T19:58:30Z")

</div>

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

Can anyone help please.  
Thank you.  
Jason

---

<div class="post-metadata">

**Author:** ![Jason136169](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jason136169](https://forum.core-electronics.com.au/u/Jason136169)\
**Post date:** [19 December 2020 03:02 UTC](https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020/3 "2020-12-19T03:02:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jason136169](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jason136169](https://forum.core-electronics.com.au/u/Jason136169)\
**Post date:** [19 December 2020 21:48 UTC](https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020/4 "2020-12-19T21:48:19Z")

</div>

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

```auto
>>> 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

---

<div class="post-metadata">

**Author:** ![Mitchell](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/mitchell/32/3564_2.png) [@Mitchell](https://forum.core-electronics.com.au/u/Mitchell)\
**Post date:** [23 December 2020 01:32 UTC](https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020/5 "2020-12-23T01:32:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warner170535](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@warner170535](https://forum.core-electronics.com.au/u/warner170535)\
**Post date:** [27 July 2021 06:44 UTC](https://forum.core-electronics.com.au/t/pyserial-to-arduino-problem/9020/6 "2021-07-27T06:44:36Z")

</div>

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](http://net-informations.com/python/intro/2vs3.htm)
