简体   繁体   中英

Arduino to RPi serial communication

I am trying to send data from an arduino to an RPi and than to a database. But when i run my Python code:

import serial
import time
import MySQLdb as mdb

arduino = serial.Serial("/dev/ttyACM0")
arduino.baudrate=9600

data=arduino.readLine()
time.sleep()
data = arduino.readLine()
blah blah blah...

IT GIVES ME AN ERROR

AttributeError: 'Serial' object has no attribute 'readLine'

How can I fix this?

Serial doesn't implement readLine . Try read instead. You probably need to open it first also.

https://pythonhosted.org/pyserial/pyserial_api.html#classes

The correct function is:

arduino.readline()

(only lowercase letters in "readline")

Iterations of readings is better to put in loop eg.:

while True:
    data=arduino.readLine()
    time.sleep(1)

for x in range(0, 100):
    data=arduino.readLine()
    time.sleep(1)      

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