RS232/485 Half Duplex

Description

This feature, present ton OI-Core/OI-CoreLite allow you to use two different protocol on same physical interface: RS485 half duplex or RS232.

There is no terminaison resistance. If you use RS485 bus and place the module at end of line you will need to add a 120Ohms resistance.

Characteristics

RS232 specifications

Requirements

Type

Value

Driver output voltage (min)

±5V

loaded with 3kΩ to Ground

Receiver input voltage high

2.2V

above this value, a logic ‘1’ is guaranteed

Receiver input voltage low

0.8V

below this value, a logic ‘0 ‘ is guaranteed

Receiver maximum input voltage

25V

Receiver minimum input voltage

-25V

RS485 specifications

Requirements

Type

Value

Driver differential voltage (typical)

2V

Driver differential voltage (min)

1.5V

Driver differential voltage (max)

5V

Code examples

The example code above demonstrates how to use RS485 on OI-Core/OI-CoreLite.

#include "OpenIndus.h"
#include "Arduino.h"

OICore core;

void setup(void)
{
    Serial.begin(115200);
    
    /* Initialize RS485 */
    core.rs.begin(9600, RS::Config::_8N1, RS::Mode::RS485);
}

void loop(void)
{
    uint8_t data[64] = {0};
    size_t size = 0;
    
    size = core.rs.available(); // Get number of available bytes
    if (size) {
        core.rs.read(data, size); // Read available data
        Serial.printf("%s\n", data);
    }

    core.rs.write(data, size); // Echo back the received data

    delay(10);
}

To use RS232 instead of RS485, just change the mode in the begin function:

core.rs.begin(9600, RS::Config::_8N1, RS::Mode::RS232);

Software API

class RS

RS485/232 bus class.

Public Types

enum Mode

RS communication mode.

Values:

enumerator RS485
enumerator RS232
enum Config

RS communication configuration Format: [Data bits] + [Parity] + [Stop bits] Parity: N=None, E=Even, O=Odd Exemple: 8N1 = 8 data bits, No parity, 1 stop bit.

Values:

enumerator _8N1
enumerator _8E1
enumerator _8O1
enumerator _7N1
enumerator _7E1
enumerator _7O1

Public Functions

inline RS(spi_host_device_t host, gpio_num_t cs, gpio_num_t intr)

Constructor.

Parameters:
  • host – SPI host device

  • cs – Chip select pin

  • intr – Interrupt pin

inline ~RS()

Destructor.

void begin(uint32_t baudrate = 115200, Config config = _8N1, Mode mode = RS485)

Initializes RS communication.

Parameters:
  • baudrate – in bits per second (baud)

  • config – 8N1 by default

  • mode – RS485 or RS232

void end(void)

Disables RS communication.

int available(void)

Get the number of bytes available for reading from the serial buffer.

Returns:

The number of bytes available to read.

int read(void)

Reads incoming serial data.

Returns:

The first byte of incoming serial data available (or -1 if no data is available)

size_t read(uint8_t *buffer, size_t size)

Reads bytes from the serial port into a buffer.

Parameters:
  • buffer – the buffer to store the bytes in. Allowed data types: array of char or byte.

  • size – the number of bytes to read. Allowed data types: int.

Returns:

The number of bytes placed in the buffer.

size_t write(uint8_t byte)

Writes binary data to the serial port.

Parameters:

byte – byte to send

Returns:

The number of bytes written.

size_t write(const uint8_t *buffer, size_t size)

Writes binary data to the serial port.

Parameters:
  • buffer – an array to send as a series of bytes.

  • size – the number of bytes to be sent from the array.

Returns:

The number of bytes written.