Receive data on lopy through serial
-
I am trying to send string data from python over serial to my lopy. Could anyone tell me in steps how can i receive it and test if it is received on my lopy?
Thanks in advance!
-
@monersss This is not complicated, but has a lot of details.
Handling of the serial interface is done through the machine.UART module. See the documentation about defining and using it. https://docs.pycom.io/chapter/firmwareapi/pycom/machine/UART.html
That works well for all UART ports expect UART 0, which is connected to the USB/UART bridge. This port then can used by sys.stdin.buffer and sys.stdout.buffer for receivings and sending. You have to use the buffer methods, to achieve transparent transmission. If the data you send may contain 0x03, them you have to disable keyboard interrupt, by micropython.kbd_intr(-1).https://docs.pycom.io/chapter/firmwareapi/micropython/micropython.html
Once you have the basic communication done, you need to define a protocol. Ususally you would send a block of data, like up to 64 bytes, with some management values. If you do not care about time, then the easiest protocol would be to echoe the received data, and the sender would compare the reponse with what it has sent. Instead, you can add the usual protocol elements like sequence numbers and integrity check values.
-
@robert-hh I have script in Matlab to send the message to lopy over serial:
import os
import serialser = serial.Serial("/COM6", 9600, timeout = 10)
print(ser.name)
ser.write(b"hello")
ser.close()I have connected to lopy via pins 3 and 4 and running this code from atom:
from machine import UART
import pycomuart = UART(1, baudrate=9600, timeout_chars=2000)
uart.write('hola')
y = uart.read(10)
print(y)I am able to receive message from Lopy to my computer over serial ( i have checked that on putty), but unfortunately sending message from computer using python does not work, the output is 'None'. Am i missing something in the uart.read part?
-
@monersss I tried to recap that with my PC, using python3, and it works. if you have connected at the same time the LoPy using USB, there may have happened a reassignment of the COM ports.
-
@robert-hh Yes i am connected to lopy both via serial USB and via pins to my computer at the same Time. I run uart script with atom and send Python script via serial. Unfortunately in atom i do not receive any message - none is printed. Sending data from atom to Python works ok... What other way i can check if receive on lopy works?
-
@monersss You can simply open Python in a cmd window. Then you do not have Atom with all its known and unknown features in place.
Since you are connected with two serial interfaces between LoPy and PC, you have to take care to use the right COM port for your test.
To avoid any confusion with the COM ports, you could connect to the Lopy REPL via Telnet, and supply the Lopy for instance with a separate 5V supply, e.g. from a smartphone charger. -
@robert-hh com ports are ok since i am able to receive message from lopy on putty or Python connected to serial USB. Do you have any other idea why sending to lopy in this setup may not work?
-
@monersss No Idea. You could try loopback tests on both devices (connect Tx and Rx ). Since the driver in both cases buffers what it receives, you could send a message and receive it after that. Then you know that both sides are working by themselves.
-
@robert-hh how exactly i can do that? Could you give me a hint
-
@monersss At the LoPy, just connect Tx and Rx (P3 and P4). The issue 'uart.write("some")' and then uart.read(4). The uart.read should get, what was sent with write. The same for the PC side, where you connect Tx and RX of the USB/UART bridge, and then with ser.write() and ser.read().
If that is successful, then you know that both sides can send & receive. If one side fails, then you know where to look at.
Edit: Did you verify that the USB/UART bridge works at 3.3V levels? Normally, they are configurable. -
@robert-hh problem solved, the cause was pretty trivial, but I believe it may happen to a begginer... recieving did work, but since i was executing code only once without a loop, code was executed so fast that it was happening before I run python script to send any data.
Now I would like to try sending txt or jpg to lopy, do I need to give it an information where to store it or this will happen automatically? -
@monersss
The last question is reall surprising ;-)
If you know how to recive data throught UART(i see that you succesfully retrive it) then how can you assume that e.g. jpg will be stored somwhere automatically?
Especially that you retrive "data" from UART and reciving side do not know what data mean.
You must "tell" what to do with data and especially what data really is :) -
@livius @robert-hh i thought this is getting started forum so suprising questions are allowed.. I was searching the forum to find out how to store received data on lopy but did not find the answer.
-
@monersss
Yes, are allowed :)
look at documentation about uart
https://docs.pycom.io/chapter/firmwareapi/pycom/machine/UART.htmlespecially you can be interested in:
uart.readall() uart.readline() uart.read([nbytes])then look at python docs about working with files:
https://docs.python.org/3/tutorial/inputoutput.htmlfor
jpgdata you should user+bmode in open method of the filee.g. (i wrote this sample without any testing if it is working)
uart = UART(1, baudrate=9600) with open('/flash/myfile.dat', 'r+b') as f: for i in range(0, 1000): #while True: d = uart.readall() if d: f.write(d)you must of course have some protocol implementing to know when to start writing to the file and when to finish it.
-
@monersss Micropython supports the usual file operations. So you can open a file and write the received data to that file. Like:
with open("mydata", "wb") as f: # receive data from uart f.write(data) -
@monersss You might also check if you can use ftp for your purpose, beause the pycom devices have a built-in ftp server, and Matlab may be able to send data through ftp.
-
@robert-hh That is not possible. Lopy will be connected to our custom built device in remote location collecting different data through sensors. Thus we can connect it only through serial to the collecting system and later use WiFi to send the data through lopy to another device where they will be stored.
-
@livius Thanks for help, I will try to implement it over this weekend.
-
@livius Hello there, I have tried to write the data on several ways, but i am doing something wrong:
from machine import UART
import pycom
import timeuart = UART(1, baudrate=9600, timeout_chars=2000)
while True:
data = uart.readall()
print(data)
if data != None:
with open('/flash/'mydatittaa','wb') as f:
f.write(data)what i am getting back is invalid syntax data in line 12.
What am I doing wrong? data is assigned to uartread, and the message that is send to lopy is a string in binary format..
-
@monersss That's strange, especially since there is no line 12. Besides that, I would rearrange the lines, otherwise the content of the file would always be overwritten. I have not xxPy deviec here at the moment and cannot try these few lines.
from machine import UART import pycom import time uart = UART(1, baudrate=9600, timeout_chars=2000) with open('/flash/'mydatittaa','wb') as f: while True: data = uart.readall() print(data) if data is not None: f.write(data)P.S.: Iy you include text in lines conatining three backquotes (```), they will be shown as code.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login