Page 1 of 1

Esp32C3 BarcodeScanner Serial blocking

Posted: Tue Jan 04, 2022 4:53 am
by Elan427
**Hardware:**
Esp32C3: https://jacobstoner.com/ESP32-C3/esp32- ... eet_en.pdf
https://jacobstoner.com/ESP32-C3/esp-c3 ... cation.pdf

**Barcode sensor:**
https://cdn.sparkfun.com/assets/b/5/0/e ... 4.6___.pdf

**Connection:**
**Esp32C3 == BarcodeSensor**
3.3v == 3.3v
Rx (pin 19) == Tx
Tx (pin 18) == Rx
Gnd == gnd

**Code**

Code: Select all

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() { // run over and over
  if (Serial.available()) {

    String recvdCode = Serial.readStringUntil('');

    int strToInt = recvdCode.toInt();
    Serial.println("int");
    Serial.println(strToInt);
    
    if (strToInt)
    {
      Serial.println(String("recvdCode str ") + String(recvdCode));
      Serial.println(String("recvdCode int") + String(strToInt));
    }
  }
}
**Output:**

Code: Select all

int
int
int
234010
023680
042740
**Problem:**
As soon I scan a barcode, the serial blocks anything that is in the loop.
Like it will keep printing "int" until I have scanned through barcode reader, then it just only outputs whenever a barcode is scanned.

Even if i keep my void loop empty, it still prints the read barcode, which makes sense as the pins are directly connected to Tx and Rx.

Apparently Esp32C3 has only 1 hardware Uart [ 18 and 19 pins].

Maybe my knowledge on Serial through Rx tx is naïve but I do not see this behavior on Arduino Mega.

Perhaps is this a Esp32C3 issue? Any workaround?

Thanks in Advance to your kind replies!

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Tue Jan 04, 2022 7:26 am
by ESP_Sprite
Looks like your issue is that you use the serial port both for your computer connection as well as the barcode reader indeed. The ESP32C3 has two serial ports, though, so you can move your barcode reader to the other one.

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Wed Jan 05, 2022 11:56 am
by al1fch
Use UART1 (= Serial1) for your barcode reader,
UART0 (= Serial) is for UART/USB chip through GPIOs 20 and 21 on DEVKIT Board

Code: Select all

Serial1.begin(9600, SERIAL_8N1, 19,18);

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Wed Jan 05, 2022 6:54 pm
by Elan427
ESP_Sprite wrote:
Tue Jan 04, 2022 7:26 am
Looks like your issue is that you use the serial port both for your computer connection as well as the barcode reader indeed. The ESP32C3 has two serial ports, though, so you can move your barcode reader to the other one.
From the C3 datasheet, it has only 1 uart? In that case what should i do.
Thanks man

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Thu Jan 06, 2022 1:58 am
by ESP_Sprite
Where do you read that? Directly from the C3 datasheet:
c3uart.png
c3uart.png (20.23 KiB) Viewed 12237 times

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Thu Jan 06, 2022 3:33 am
by Elan427
al1fch wrote:
Wed Jan 05, 2022 11:56 am
Use UART1 (= Serial1) for your barcode reader,
UART0 (= Serial) is for UART/USB chip through GPIOs 20 and 21 on DEVKIT Board

Code: Select all

Serial1.begin(9600, SERIAL_8N1, 19,18);
This didn't work. Same results.
here is the code:

Code: Select all

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, 19,18);

}

void loop() { // run over and over

Serial.println("ayooo");
delay(1000);
if (Serial.available()){
  Serial.println("flushin");
  Serial.flush();
}

}
here is the output:
ayooo
ayooo
0490440
0490440
ayooo
ayooo
ayooo
ayooo

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Thu Jan 06, 2022 3:35 am
by Elan427
ESP_Sprite wrote:
Thu Jan 06, 2022 1:58 am
Where do you read that? Directly from the C3 datasheet:
c3uart.png
Yes i can read that too, but i dont see the pins. Can you please tell me where are the 2nd uart pins are defined because i literally cant find them. Thanks.

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Thu Jan 06, 2022 9:14 am
by al1fch
TWO Uarts ... and their RxTx nearly anywhere

on my last -C3 boards I found Uart0 on GPIO20 and GPIO21 pins (routed to CH340 chip)

I choosed 18 and 19 for Uart1. just beside ..... ;-)

Image

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Thu Jan 06, 2022 10:30 am
by ESP_Sprite
Yep. The ESP32C3, like most ESP32-series chips has a GPIO matrix, meaning you can switch the signals of most peripherals (I2C, I2S, UART, SPI, CAN, ...) to any GPIO you want (that is not otherwise occupied). I think Arduino by default assigns two GPIOs (don't know which) to it, but you should also be able to manually assign them using something like Seria1l.begin(19200, SERIAL_8N1, tx_gpio_num, rx_gpio_num);,

Re: Esp32C3 BarcodeScanner Serial blocking

Posted: Thu Jan 06, 2022 10:57 am
by al1fch
I think Arduino by default assigns two GPIOs
Here is pins_arduino.h file for esp32c3 variant in 2.0.2 core
(maybe spécific assignements for specific -C3 boards)

Code: Select all

#ifndef Pins_Arduino_h
#define Pins_Arduino_h

#include <stdint.h>

#define EXTERNAL_NUM_INTERRUPTS 22
#define NUM_DIGITAL_PINS        22
#define NUM_ANALOG_INPUTS       6

#define analogInputToDigitalPin(p)  (((p)<NUM_ANALOG_INPUTS)?(esp32_adc2gpio[(p)]):-1)
#define digitalPinToInterrupt(p)    (((p)<NUM_DIGITAL_PINS)?(p):-1)
#define digitalPinHasPWM(p)         (p < EXTERNAL_NUM_INTERRUPTS)

static const uint8_t TX = 21;
static const uint8_t RX = 20;

static const uint8_t SDA = 8;
static const uint8_t SCL = 9;

static const uint8_t SS    = 7;
static const uint8_t MOSI  = 6;
static const uint8_t MISO  = 5;
static const uint8_t SCK   = 4;

static const uint8_t A0 = 0;
static const uint8_t A1 = 1;
static const uint8_t A2 = 2;
static const uint8_t A3 = 3;
static const uint8_t A4 = 4;
static const uint8_t A5 = 5;

#endif /* Pins_Arduino_h */