Problem when sending data to uart module

Hi, I am seeking for help related to data communication with a uart module. The module was connected via RX0 and TX1 pins, however when I am trying to send data with Serial.write(...), the program just cannot upload to the board at all. Was it because of code uploading also need to use Serial via USB? Or something else?
This is the code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600, SERIAL_5E1);
  delay(1000);
  Serial.write(0xFE);
  Serial.write(0xFD);
  Serial.write(0x01);
  Serial.write(0x01);
}

On the Classic Nano, RX0 and TX1 are reserved for program upload and output on the serial monitor. You should not connect anything to those pins.

SoftwareSerial (or AltSoftSerial or NeoSoftwareSerial) are options that allow you to use other pins, with some limitations.

1 Like

Thank you for your reply. I am just playing with a serial VFD display. But have no idea what board to use. Maybe it is better to choose a board with extra serial pins?

If you are using UNO or NANO, then you can use Software UART Port (SUART Port) to communicate with your VFD Display Unit leaving the UART Port (RX0/TX1) for IDE/SM. You must know the following parameters of your VFD Display Unit in order to initialie the SUART Port accordingly.

BuadRate (Bd)    : 9600, 11520, ...
Character Length : 7, 8
Parity Option    : Odd, Even, None
Stop Bits        : 1, 1 and 1/2, 2

Hints:

#include<SoftwareSerial.h>
SoftwareSerial SUART (10, 11);  //SRX = DPin-10, STX = DPi-11

void setup()
{
     Serial.begin(960);
     SUART.begin(9600); //Bd = 9600, Charater = 8, Parity = None, Stop it = 1
}

void loop()
{
     byte n = SUART.available();
     if(n != 0)  //at least 1 data byte/charcters is in Serial Buffer
     {
           char ch = SUART.read();
           Serial.print(ch);
     }
}
1 Like

@GolamMostafa, your code will not compile :slight_smile: You don't have a SUART object but a UART object.

1 Like

Thank you. Corrected.

1 Like

Thank you so much, I will take a look into it. If i want to send parity bit and stop bit and data bits, what order shall I follow?

SoftwareSerial only supports 8N1. There are no constructors or methods to use 5E1.

So you will need a board with a processor that gives additional UARTs. With (kind of) the same form factor as the classic Nano, you can use the Arduino Micro, Arduino Nano Every or SparkFun Pro Micro.
If form factor is not important, you can use an Arduino Mega.

There are more boards but they are 3.3V boards instead of 5V boards.

Alternatively you can check the other libraries mentioned by @jremington and check if they support the needed serial format.

Can you post the Serial frame structure of your target device in respect of:
Bd
Character length
Parity option
Stop bit length

Look at the Serial.begin() in the opening post :wink:

I have expected that the OP will confirm explicity that the Serial frame structure of his target devicee is the following. Hope that the OP will confirm it this time.

Bd = 9600
Character Lenghth = 5
Parity Option = Even
Stop Bit = 1
1 Like

Got it, I will try to find the right config info from the manufacturer. And I think another problem is I did not give enough power to the module (in which case I connect the vcc directly to arduino board)

#include <HardwareSerial.h>

#define TX2 17 // TX Pin on ESP32
#define RX2 16 // RX Pin on ESP32

HardwareSerial UART(1);

void setup() {
  // put your setup code here, to run once:
  // This Serial is to log essential information
  Serial.begin(115200); 
  Serial.println("Program started"); 
  //Serial2.begin(9600, SERIAL_8E1, RX2, TX2); 
  // This is to control the VFD Display through UART port (RX2 & TX2) on ESP32
  UART.begin(9600, SERIAL_8E1, RX2, TX2); // baud, config (8bit data + Even parity + 1 stop bit), rx_pin, tx_pin
  // hour data (display 10 here)
  UART.write(0xFE);
  UART.write(0xFD);
  UART.write(0x02);
  UART.write(0x0A);
  // minute data (display 56 here)
  UART.write(0xFE);
  UART.write(0xFD);
  UART.write(0x03);
  UART.write(0x38);
  // : on 
  UART.write(0xFE);
  UART.write(0xFD);
  UART.write(0x04);
  UART.write(0x01);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (UART.available() > 0) {
    char c = UART.read();
    Serial.print(c); 
  }
}

I use ESP32 Dev Board instead as it has second UART ports.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.