UART

UART is a method for communication between computers. The features of the UART are as follows:

  • Only one electric wire + GND are needed to send data. Two wires + GND to send and receive.
  • Can be used with almost every microcomputer.
  • Compatible from low to relatively fast speed communication. obniz Board series is up to 115.kbps.
  • Mainly used for communication between two computers. There is no master-slave system and they can send data at any time each other.
  • No error-correcting function. Parity check is optional.

The UART is the simplest way to exchange data and there are USB-UART conversion cables. It is often used because it is the easiest way to communicate between a PC and a microcomputer.

Initial settings

The number of available UART depends on a devise. In terms of obniz Board series, it is UART0 and UART1. You can use the UART by setting output and input IO. There is no relation between the UART number and the IO number. UART0 can be used at IO1.

Set UART using uart.start(). When you set IO1 as input, settings are as shown below. The IO set by tx is the output of UART itself, and is the IO connected to the rx of the other party.

var uart = obniz.getFreeUart();
uart.start({tx: 0, rx: 1, baud:9600 });

obniz.getFreeUart() returns unused UART out of several UARTs. If you know the UART certainly not used, you can use it by setting UART.

obniz.uart0.start({tx: 0, rx: 1, baud:9600 });

baud means communication speed and the larger the baud, the faster. Transmission and reception are the same speed, and when communicating, it is necessary to match with the other party. You can use any value you like, but there are some commonly used values for UART. 9600 is one of them. It means it can send 9600 bits per second; actually, it is not possible to send the data at bits per second because some control signals other than data are included.

Also, you need to connect GND to the communicating party in addition to tx and rx. When connecting GND to the other party using IO and in order to connect IO11 to the other party’s GND, you can set as follows:

var uart = obniz.getFreeUart();
uart.start({tx: 0, rx: 1, gnd:11, baud:9600 });

The driving method of IO used as tx, rx can be changed.

var uart = obniz.getFreeUart();
uart.start({tx: 0, rx: 1, gnd:11, baud:9600, drive:"3v", pull:null });

1 byte at start

Unintended data may be sent or received at the start of the UART.

This is because when the UART starts, it may have a waveform as if the signal had started, so that the other party or the UART itself misunderstood and performed 1-byte communication. It occurs when starting the UART, not when sending data.

Parity

The start() function can add parity. See Interface PeripheralUARTOptions for details.

Flow control

Flow control using RTS / CTS is possible. See Interface PeripheralUARTOptions for details. The type of flow control can be selected by flowcontrol.

var uart = obniz.getFreeUart();
uart.start({
  tx: 0,
  rx: 1,
  cts: 2,
  rts: 3,
  flowcontrol: "rts-cts",
  baud:9600});

Send and receive

You can send and receive data with the UART you set.

End

The started UART can be ended by calling uart.end().

obniz.uart0.start({tx:0, rx:1})
obniz.uart0.send("Hi");
obniz.uart0.end();

Articles