Question

I'm currently workng on a IMU Razor 9DOF
I can read data from it by reading /dev/ttyUSB0. (default mode is YPR = angles of the IMU)
I can also change the data i'm receiving by sending #ot or #osrt to the IMU via the Arduino's Serial Monitor.

So I'm trying to write a code in Python3 to do both reading and changing mode via the same interface (and also using python3 to process the data).
Here are my issues:
1. I am using pyserial to read data from the port. Here is my code:

    from serial import *
    usb = Serial('/dev/ttyUSB0', 57600)
    while True:
       usb.readline()

    print()

When I try to run this code via python console it runs perfectly, but when I try doing it from the script (python3 mycode.py) it doesn't work.
I don't understand why.

2. I try to change the mode by sending the command '#osrt' by using

    usb.write(byte("#osrt", "utf8"))

but it only returns the length of data written without changing the mode.

Thank You For Your Help !

Était-ce utile?

La solution

It is recommended when using readline you set a timeout. readline will wait until a \n is received.

from serial import *
    usb = Serial('/dev/ttyUSB0', 57600)
    usb.timeout = 1
    while True:
       print(usb.readline()) # you need to print what you are reading in the script
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top