Send multiple bytes over Uart

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
UmbraAtrox
Posts: 4
Joined: Mon Jan 18, 2021 11:12 am

Send multiple bytes over Uart

Post by UmbraAtrox » Tue Jan 19, 2021 5:48 am

ESP8266 running micropython 1.13

So i'm working on a device to talk to my modbus enabled power-meter. I have used the search and have found lots of dead threads which basically more or less the same problems i faced until now. I found the "pycom" modbus library but i haven't found an answer to the question "wtf is pycom and how does it differ from micropython?" and therefore didn't test it. Then i found someone who attempted to port it to micropython which has the _uart.wait_tx_done(2) part which doesn't work, as already discussed in a different thread, so i patched that out and it does the same one character send as my 4 lines.

Screenshots from Com monitor: https://imgur.com/a/tjS4mSd

code:

Code: Select all

from machine import Pin, UART
uMB = UART(0, baudrate=9600, bits=8, parity=0, stop=1, timeout_char=10, tx=Pin(15), rx=Pin(13))
outp = bytearray(b'\x01\x03\x00\x09\x00\x02\x14\x09')
uMB.write(outp)
So my questions are:
Is this possible with the current uart implementation? if yes how?

The only obvious difference i see is the "datalength" which for micropython seems to be 1 and for modbus 8

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Send multiple bytes over Uart

Post by Roberthh » Tue Jan 19, 2021 7:40 am

First answer: Pycom is a company which develops & sells esp32 based module with MicroPython firmware. Their MicroPython variant is close to the one of MicroPython.org, based on the CC3200 port. So porting between the two versions is easy. The main difference is the scheme for numbering GPIO pins.

Second: You can send as many bytes as you want through UART. When you call it with:

Code: Select all

from machine import Pin, UART
uMB = UART(0, baudrate=9600, bits=8, parity=0, stop=1, timeout_char=10, tx=Pin(15), rx=Pin(13))
outp = b'\x01\x03\x00\x09\x00\x02\x14\x09'
uMB.write(outp)
It will send 8 bytes. Trying to send the bytearray instead of the byte string may have been the problem.

UmbraAtrox
Posts: 4
Joined: Mon Jan 18, 2021 11:12 am

Re: Send multiple bytes over Uart

Post by UmbraAtrox » Tue Jan 19, 2021 3:58 pm

Thanks for the pycom explanation.

but for the bytearray, I changed that. but my output on the serial monitor remains unchanged. It still sends every byte separate. :(

edit: i think i found the issue.

When i use the onboard CH340 usb interface and monitor that the bytes come all at once as expected. Now even the bytearray works.

but when i send them through that MAX485 (yes its 5V, 3v3 version is ordered) the bytes come one at a time. So that seems to be a me problem not a micropython problem. i will add a transistor to drive the tx line properly and hope that fixes it. ¯\_(ツ)_/¯

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Send multiple bytes over Uart

Post by Roberthh » Tue Jan 19, 2021 4:21 pm

Can't confirm. If I run these few lines and listen to them with the Logic analyzer, they come all together.

Script code:

Code: Select all

uos.dupterm(None, 1)
from machine import Pin, UART
uMB = UART(0, baudrate=9600, bits=8, parity=0, stop=1, timeout_char=10, tx=Pin(15), rx=Pin(13))
outp = b'\x01\x03\x00\x09\x00\x02\x14\x09'
uMB.write(outp)
Result in the logic analyzer:
uart_tx.jpg
uart_tx.jpg (105.25 KiB) Viewed 2088 times

UmbraAtrox
Posts: 4
Joined: Mon Jan 18, 2021 11:12 am

Re: Send multiple bytes over Uart

Post by UmbraAtrox » Tue Jan 19, 2021 5:05 pm

Did you use a rs485 converter chip? if yes which one?

because as stated in my edit it works with "usb" but doesn't with max485
When i use the onboard CH340 usb interface and monitor that the bytes come all at once as expected. Now even the bytearray works.


EDIT: i get the same logic analyzer output Ch0 is TX from Esp and CH1 and CH2 is differential pair from MAX485 - receiving end (USBtoRS485) disconnected. Seems fine so far.

So to conclude:
Micropython output is fine
MAX485 output seems fine
Com monitor input is bad

I will edit this post when i find more
Attachments
485cap.jpg
485cap.jpg (76.8 KiB) Viewed 2077 times

Post Reply