Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 to Arduino
#1
Hi. My first post on here.
 I am trying to send - receive Python3 <> Arduino.
I had this working with Python2.7 now would like to convert to Pyhon3.
python2.7 code that worked.
    ser.write('<')
    ser.write(RorD)
    ser.write('>')
python3 code
    ser.write(bytes(b'<'))
    ser.write(b'RorD')
    ser.write(bytes(b'>'))
I think the Arduino is now receiving b'<'
RorB is a int.

Arduino snip

char startMarker = '<';
char endMarker = '>';

How can I send just '<'
Thanks for any help.
Hope I used the python tag right.
Reply
#2
Are you sending it over the net?
http://pythoncentral.io/encoding-and-dec...ython-3-x/
Recommended Tutorials:
Reply
#3
(Oct-24-2016, 05:39 PM)metulburr Wrote: Are you sending it over the net?
http://pythoncentral.io/encoding-and-dec...ython-3-x/

Thanks for the link, that could help a lot.
The Arduino will be plugged in to an EEEPC (USB) to control a rotatory table on my lathe.
Reply
#4
From what I gather from the pySerial docs (I presume that's what your using), in Python 3, you have to convert to 'bytes' type.

So this is the correct format

ser.write(b'RorD')
Are you receiving an error? If so, is it coming from Python or the Arduino?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
There is problem if you do this b'RorD' is now not a int.
Convert to Python 3.x Unicode string it will just be string 'RorD'
>>> s = b'RorD'
>>> type(s)
<class 'bytes'>
>>> s = s.decode('utf-8')
>>> type(s)
<class 'str'>
>>> s
'RorD'
When sending data to Arduino, they have to be converted to bytes. This can be done by prefixing the string with b:

So can look at RorD as integer.
>>> RorD = 100
>>> n = (RorD).to_bytes(2, byteorder='big')
>>> n
b'\x00d'
>>> # Back to int in Python 3
>>> int.from_bytes(n, byteorder='big')
100
for < is okay to use b'<'.
Back to string in Python 3 use decode(utf-8).
>>> s = b'<'
>>> s
b'<'
>>> type(s)
<class 'bytes'>
>>> s = s.decode('utf-8')
>>> s
'<'
>>> type(s)
<class 'str'>
Reply
#6
Thanks snippsat, metulburr and sparkz_alot.
You have been a great help. I can now see how to send from Python3.
Many thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,876 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  show and storage real temperature data Arduino in Python3 linkxxx 0 1,832 Aug-21-2019, 04:07 PM
Last Post: linkxxx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020