简体   繁体   中英

Python, Raspberry, and Arduino Communication

I have been working with my Raspberry, Arduino and applying some Python to make them communicate. I also installed pyserial. If I use IDLE and try the following I get my expected results.

  >>> import serial
  >>> ser = serial.Serial('/dev/ttyUSB0',115200)
  >>> ser.write('!x:1\n')
  8
  >>> ser.readline()
  'X:1\r\n'

If I write a python scrip and run it I don't get anything back. Here is my python script.

  import serial
  ser = serial.Serial('/dev/ttyUSB0', 115200)
  ser.write('!x:1\n')
  ser.readline()

I am new to Python so looking for some help to understand they this works in IDLE but not as a python script run from terminal.

Thanks.

Did you intend to have the string in the third line of your script start with an exclamation point? If not, that's your problem.

The solution for this really came down to a timing issue. The ser = serial.Serial('/dev/ttyUSB0', 115200) Needs to make the connection before sending any commands and the code I posted was just taking place too quick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM