Esp32C3 BarcodeScanner Serial blocking

Elan427
Posts: 4
Joined: Tue Jan 04, 2022 4:46 am

Esp32C3 BarcodeScanner Serial blocking

Postby Elan427 » Tue Jan 04, 2022 4:53 am

**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!

ESP_Sprite
Posts: 9582
Joined: Thu Nov 26, 2015 4:08 am

Re: Esp32C3 BarcodeScanner Serial blocking

Postby ESP_Sprite » 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.

al1fch
Posts: 7
Joined: Sat Mar 28, 2020 6:56 pm

Re: Esp32C3 BarcodeScanner Serial blocking

Postby al1fch » 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);

Elan427
Posts: 4
Joined: Tue Jan 04, 2022 4:46 am

Re: Esp32C3 BarcodeScanner Serial blocking

Postby Elan427 » Wed Jan 05, 2022 6:54 pm

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

ESP_Sprite
Posts: 9582
Joined: Thu Nov 26, 2015 4:08 am

Re: Esp32C3 BarcodeScanner Serial blocking

Postby ESP_Sprite » Thu Jan 06, 2022 1:58 am

Where do you read that? Directly from the C3 datasheet:
c3uart.png
c3uart.png (20.23 KiB) Viewed 11840 times

Elan427
Posts: 4
Joined: Tue Jan 04, 2022 4:46 am

Re: Esp32C3 BarcodeScanner Serial blocking

Postby Elan427 » Thu Jan 06, 2022 3:33 am

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

Elan427
Posts: 4
Joined: Tue Jan 04, 2022 4:46 am

Re: Esp32C3 BarcodeScanner Serial blocking

Postby Elan427 » Thu Jan 06, 2022 3:35 am

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.

al1fch
Posts: 7
Joined: Sat Mar 28, 2020 6:56 pm

Re: Esp32C3 BarcodeScanner Serial blocking

Postby al1fch » Thu Jan 06, 2022 9:14 am

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
Last edited by al1fch on Thu Jan 06, 2022 11:05 am, edited 1 time in total.

ESP_Sprite
Posts: 9582
Joined: Thu Nov 26, 2015 4:08 am

Re: Esp32C3 BarcodeScanner Serial blocking

Postby ESP_Sprite » Thu Jan 06, 2022 10:30 am

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);,

al1fch
Posts: 7
Joined: Sat Mar 28, 2020 6:56 pm

Re: Esp32C3 BarcodeScanner Serial blocking

Postby al1fch » Thu Jan 06, 2022 10:57 am

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 */

Who is online

Users browsing this forum: yohnsee and 55 guests