Reading inverted RS-232 data
-
Just some notes in case anyone has to read data from a serial RS-232 device that caused me some headache:
Serial UART and serial RS-232 data differ in voltage swing and polarity.
A Gpy expects to see 5 V as a logical "1" and 0 V as a logical "0".
But RS-232 defines about -15 V as a logical "1" and +15 V as logical "0" - what is completely invers.
Given You only want to read from an RS-232 device, one can use a simple solution of a diode (rectifier) and two resistors (voltage divider) to attach the TX wire of the serial device to the RX1 pin of a Gpy.
But there is still the problem of the inverted data bits polarity. There really should be a simple python solution like
uart.init(9600, bits=8, parity=None, stop=1, invert=UART.INV_RX | UART.INV_TX)
as suggested by other authors.
Meanwhile, however, we can achieve the same goal by setting the required UART bits ourselves:
UART_CONF0_REG = machine.mem32[0x3FF50020] | (1<<22) | (1<<19) machine.mem32[0x3FF50020] = UART_CONF0_REG
will do the trick and voila! the RS-232 data is received correctly.
(By the way: 0xFF40020 is the serial console, 0xFF50020 is TX1/RX1 and 0xFF6E020 is tied to the LTE modem.)
-
@Elizar That#s a good hint. There is also a PR #362 outstanding since Nov. 2019 for UART inverted mode, which supports that in a more API consistent fashion. And that PR is compatible to the mainstream MicroPython.