Page 1 of 2
Trouble initializing UART communication
Posted: Mon Dec 28, 2020 2:44 pm
by 0_djek
Hello,
I have rs485 connected to my esp8266 to ports tx=D6 and rx=D5, but I can't figure a way to establish UART communication. Any help would be appreciated!
My code:
Code: Select all
from machine import Pin, UART
#D6 - TXD (12)
#D5 - RXD (14)
def test():
#modbus = UART(1, 9600)
modbus = UART(1, baudrate=9600)
modbus.init(tx=Pin(12), rx=Pin(14))
print("modbus: {}".format(modbus))
With this code I get error message
Code: Select all
Traceback (most recent call last):
File "main.py", line 30, in <module>
File "modbusComs.py", line 10, in test
ValueError: invalid tx/rx
Line 10 is this line:
Code: Select all
modbus.init(tx=Pin(12), rx=Pin(14))
Re: Trouble initializing UART communication
Posted: Mon Dec 28, 2020 7:55 pm
by Roberthh
UART1 has no RX part, only TX. For a full duplex UART communication you have to use UART0. For that, you can detach UART0 from the REPL prompt using uos.dupterm() and use REPL with the WiFi webrepl app. See
http://docs.micropython.org/en/latest/e ... ht=dupterm
Note about using the UART module 8and others like SPI or I2C too): You can put all init parameters into the constructor. That is also a good method to avoid confusion, because when using uart.init(), all parameters which differ form the default have to be specified. You cannot put some parameters in the constructor and some into the init() call.
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 1:29 pm
by 0_djek
Roberthh wrote: ↑Mon Dec 28, 2020 7:55 pm
UART1 has no RX part, only TX. For a full duplex UART communication you have to use UART0. For that, you can detach UART0 from the REPL prompt using uos.dupterm() and use REPL with the WiFi webrepl app. See
http://docs.micropython.org/en/latest/e ... ht=dupterm
Note about using the UART module 8and others like SPI or I2C too): You can put all init parameters into the constructor. That is also a good method to avoid confusion, because when using uart.init(), all parameters which differ form the default have to be specified. You cannot put some parameters in the constructor and some into the init() call.
Thanks a lot for clarification!
But then I have a question - can I use any tx and rx pins for UART0, or I have to use Pin1 and Pin3? And how should I initialize those pins? Just write tx=1, rx=3, or tx=Pin(1) rx=Pin(3)? Or somehow else?
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 1:48 pm
by Roberthh
With the ESP8266 you cannot assign UART to different pins. You have to use GPIO1 and 3. While that might not be a problem for TX in your case, RX may be hard, since it is connected on many boards to the USB/UART bridge. On some boards lime the Wemos D1 there is a resistor between the ESP8266 and the USB/UART bridge, allowing to overdrive the signal.
Note: Looking in to the data sheet I noticed, that there is indeed a UART1 RX signal mentioned at GPIO8, which however is used for the SPI flash.
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 4:02 pm
by 0_djek
Roberthh wrote: ↑Tue Dec 29, 2020 1:48 pm
With the ESP8266 you cannot assign UART to different pins. You have to use GPIO1 and 3. While that might not be a problem for TX in your case, RX may be hard, since it is connected on many boards to the USB/UART bridge. On some boards lime the Wemos D1 there is a resistor between the ESP8266 and the USB/UART bridge, allowing to overdrive the signal.
Note: Looking in to the data sheet I noticed, that there is indeed a UART1 RX signal mentioned at GPIO8, which however is used for the SPI flash.
Maybe you could help what is wrong with this code? I import it to main.py, but I cal it separately in console on uPyCraft. It prints "modbus", but that's it, I don't get anything else, it freezes. I'm trying to read values from this meter:
https://www.fif.com.pl/en/usage-electri ... -01mr.html. Thanks for help!
Code: Select all
from machine import Pin, UART
import utime
import uos
def test():
#modbus = UART(1, 9600)
values = []
print("modbus")
modbus = UART(0)
modbus.init(9600)
uos.dupterm(None, 1)
print("Reading from modbus...")
for i in range(0, 5):
val = modbus.read()
values.append(val)
utime.sleep(1)
modbus.deinit()
uart = machine.UART(0, 115200)
uos.dupterm(uart, 1)
print(values)
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 4:34 pm
by Roberthh
a) there is no uart.deinit() method for ESP8266
b) you call machine.UART() instead UART() when initing the uart again.
Both would be visible in the webrepl, which continues to work when you detach REPL from UART
Changed code:
Code: Select all
from machine import Pin, UART
import utime
import uos
def test():
#modbus = UART(1, 9600)
values = []
print("modbus")
uos.dupterm(None, 1)
modbus = UART(0, 9600)
print("Reading from modbus...")
for i in range(0, 5):
val = modbus.read()
values.append(val)
utime.sleep(1)
uart = UART(0, 115200)
uos.dupterm(uart, 1)
print(values)
Another comment on uart.read(). It reads whatever is present at the time of calling. That may be incomplete messages.
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 4:49 pm
by 0_djek
Roberthh wrote: ↑Tue Dec 29, 2020 4:34 pm
a) there is no uart.deinit() method for ESP8266
b) you call machine.UART() instead UART() when initing the uart again.
Both would be visible in the webrepl, which continues to work when you detach REPL from UART
Changed code:
Code: Select all
from machine import Pin, UART
import utime
import uos
def test():
#modbus = UART(1, 9600)
values = []
print("modbus")
uos.dupterm(None, 1)
modbus = UART(0, 9600)
print("Reading from modbus...")
for i in range(0, 5):
val = modbus.read()
values.append(val)
utime.sleep(1)
uart = UART(0, 115200)
uos.dupterm(uart, 1)
print(values)
Another comment on uart.read(). It reads whatever is present at the time of calling. That may be incomplete messages.
It connects, it reads, but then it prints my values array with all values being as None. Also, I can no longer connect to it with uPyCraft, it asks me to flash my esp, but I can continue logging in using webrepl. Maybe I need to set pins somewhere?
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 5:32 pm
by Roberthh
With a dumb terminal emulator like Putty you can connect again. So it is a promblem of uPyCraft.
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 5:57 pm
by 0_djek
I added line to ouput my created modbus, and it doesn't show my assigned tx/rx pins. Am I doing something wrong here? Also, on the manufacturer website of the electricity consumption meter, it writes about command codes, and command code 3 "Registers values reading". Do I have to pass that command somehow?
My code:
Code: Select all
from machine import Pin, UART
import utime
import uos
def test():
#modbus = UART(1, 9600)
values = []
print("modbus")
uos.dupterm(None, 1)
modbus = UART(0, 9600)
modbus.init(stop=2, timeout=100, tx=Pin(1), rx=Pin(3))
print("Reading from modbus: {}".format(modbus))
for i in range(0, 50):
val = modbus.read()
print("Value of {} reading: {}".format(i, val))
values.append(val)
utime.sleep(1)
uart = UART(0, 115200)
uos.dupterm(uart, 1)
print(values)
Console output:
Code: Select all
Reading from modbus: UART(0, baudrate=9600, bits=8, parity=None, stop=2, rxbuf=15, timeout=100, timeout_char=2)
It assigns everything correctly, except that it doesn't assign my tx and rx pins.
Re: Trouble initializing UART communication
Posted: Tue Dec 29, 2020 7:29 pm
by Roberthh
Looking into the code I noticed (and rembered then) that you can swap the TX and RX pint to 15 (tx) and 13( (rx). That should solve the electrical collision with RX on 3 and may stop the irritation of uPyCraft.
The Pin assignment is simply not printed, so you do not know whether it worked by printing the uart object.