November 08, 2024, 05:53:48 AM

MOD-RS232 Example

Started by stanislav.nastasiu, November 02, 2022, 09:43:57 PM

Previous topic - Next topic

stanislav.nastasiu

Hi,
i've bought an ESP32-EVB and the MOD-RS232.
I'm looking for a guide on how to open a serial connection on the UEXT1.
I'm using the Arduino IDE and coudn't find any info on how to proceed.
Thanks in advance!

LubOlimex

It is just hardware level shifter. For meaningful communication you need something attached to MOD-RS232, aside from that you can short MOD-RS232's RX and TX and you can test your code if it returns echo.

There is nothing special in the Arduino code. Simple serial, pins can be seen in the schematic of ESP32-EVB (UEXT pin #3 and UEXT pin #4 are responsible for the UART, trace which ESP32 GPIOs are there and adjust the serial code with them).
Technical support and documentation manager at Olimex

stanislav.nastasiu

Thanks but i'm still having some trouble achieving what i want.
I'm trying to connect a RS232 scanner to the ESP32-EVB trought the MOD-RS232 to capture the readed code. The barcode scanner (Datalogic Quickscan QW2400) is powered from an outside powersupply as it requires 5V 0.3A. In my code even trying to open a second serial connection gives me an error:
14:11:16.458 -> E (22) uart: uart_set_pin(683): tx_io_num error
14:11:16.458 -> ESP_ERROR_CHECK failed: esp_err_t 0xffffffff (ESP_FAIL) at 0x4008b438
14:11:16.458 -> file: "C:\Users\stani\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5\cores\esp32\esp32-hal-uart.c" line 167
14:11:16.458 -> func: uartBegin
14:11:16.458 -> expression: uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)
14:11:16.492 ->
14:11:16.492 -> abort() was called at PC 0x4008b43b on core 1

my code:

#define TXD_PIN (GPIO_NUM_4)
#define RXD_PIN (GPIO_NUM_36)

void setup(){
 Serial2.begin(9600, SERIAL_8N1, TXD_PIN, RXD_PIN);
}

void loop() {
  while(Serial2.available()){
    Serial.println(Serial2.read());
  }
}


LubOlimex

First notice that this line:

Serial2.begin(9600, SERIAL_8N1, TXD_PIN, RXD_PIN);
Seems incorrect, since RX is first then TX, it should be

(9600, SERIAL_8N1, RXD_PIN, TXD_PIN);
Second - try to do a simple echo test to check if the MOD-RS232 works fine, disconnect your scanner and connect pin #2 and #3 of the MOD-RS232 (RX to TX) then try with the following code if there is echo:


void setup()
{
Serial.begin (115200);
Serial2.begin(115200, SERIAL_8N1, RXD_PIN, TXD_PIN);
}

void loop()
{
while(Serial2.available())
Serial.print((char)Serial2.read());
while(Serial.available())
Serial2.print((char)Serial.read());
}

Serial is the USB-seral, Seral2 is the serial at the UEXT. If this works then the problem is probably not in the MOD-RS232, nor in the ESP32-EVB, nor your code but between the scanner and the MOD-RS232. If that is the case:

Maybe the levels of MOD-RS232 and your scanner are incompatible.
Technical support and documentation manager at Olimex

stanislav.nastasiu

First of all thanks!
I followed your instruction and it worked.

For the scanner i ended up using a null modem adapter and can now read something.

The problem now is to correctly decode the data 🤔.
I've tried:
if (Serial2.available() > 0)
{
String data = Serial2.readString();
Serial.print(data);
}

unsigned char incomingByte;

if (Serial2.available() > 0)
{
incomingByte = Serial2.read();
Serial.print(incomingByte, HEX);
Serial.print(incomingByte, BIN);
Serial.print(incomingByte, DEC);
}

but it's all gibberish

LubOlimex

Well, you might need to consult with the documentation of the scanner and what is the format it sends. Can you share a screenshot of the gibberish you got, upload the picture somewhere online and post the link here.
Technical support and documentation manager at Olimex

stanislav.nastasiu

Never mind. The problem was caused by a faulty cable (which i butchered together). By using another cable in combination with a gender changer (since the new cable is male) i was able to read the correct string from the scanner.

The final diagram (if you can call it that  ;D ) is this:




And the data is read like this

if (Serial2.available() > 0)
  {
    String data = Serial2.readString();
    Serial.print(data);
  }