Interfacing the RS232 to TTL with Raspberry pi pico using arduinoIDE

Hi all,
I am trying to read the data packets which are in hex form, using the RS232 to TTL converter.
Initially I have tried with the raspberry pi and able to receive the correct data packets, further I need to implement the same on Pi pico, for which I am using the Arduino IDE.
I could read the data, but wrong !
Expected data: A2 01 80 DA 01 01 1A AE 00 00 03 8D 00 3F E0 AB 00 00 00 00 7F 16
Data on pico: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

any suggestion and help would be appreciated
Thanks in advance :slightly_smiling_face:

Welcome to the forum

RS232 and TTL use different voltages and transmission protocols. Have you got anything between the two to convert one to the other ?

Yeah, I am using the RS232 and TTL converter, which has the MAX3232 transceiver

Welcome

I suggest that you post your code; do not forget to use code tags as described in How to get the best out of this forum

Serial.read will return -1 (0xFF) if there is nothing to read.

A wiring diagram might also be useful.

Thanks for looking into it,
here I am pasting the snippet where I am reading the data

// Main loop function
void loop() {

  // Check if data is available to read
  if (Serial1.available()) {
    size_t index = 0;
    while (Serial1.available() && index < RadarBuffLength) {
      byte data = Serial1.read();
      
      RadarBuff[index++] = data;
      if (data == stop_delimiter && index >= RadarBuffLength) {
        break;
      }
    }

    if (index > 0) {
      process_packet(RadarBuff, index);
    } else {
      // No data received, reset radar
      Serial.println("Received data packet is zero.");
     
    }
  } else {
    // No data available, reset radar
    Serial.println("No data available.");
  
  }
}

The below are the connection details

TTL Side
vcc --> 3.3V
Gnd --> gnd
Tx --> Tx on pico
RX --> Rx on pico

RS232 Side
gnd --> gnd
pin 2 of the device to pin 3 of RS232
pin 3 of the device to pin 2 of RS232

How about the serial format (# bits, parity, start/stop bits etc)
they nust be the same on both ends.

Hi @lastchancename
A2 01 80 DA 01 01 1A AE 00 00 03 8D 00 3F E0 AB 00 00 00 00 7F 16
start bit A2, stop bit 16
The the remaining packets are variable data

Please post complete code. Or a representative example that demonstrates the issue.

So even if you have found the stop-delimiter, you don't break.

What is the sender?

Note: you might find a suitable approach to serial reading in Robin's updated serial input basics tutorial

That is not what @lastchancename meant.
A2 is not a bit it is a byte. The same goes for 16.

He is talking about the data format.

Thanks @Grumpy_Mike
I think I know where this is heading. :scream:

@Grumpy_Mike Thanks for the clarification,
yes the format is same on the both the ends

Hi @sterretje
Consider the xyz device which is sending the data packet of hex format
which I need to read on the pico
Apologies for not giving much information of the sender due to some reasons
Thanks

 if (data == stop_delimiter && index >= RadarBuffLength) {
        break;
      }

Can ignore this condition, the main issue is I am not able to read the exact data

This will probably read only one byte of data, because Serial is very slow compared to the while loop. You need some sort of timeout/pause that it longer than the time it takes to transmit a character serially.
See @Robin2 's "Serial Input Basics."

Hi, @westfw, Thanks for your input. I will check out the Serial Input Basics

before connecting the external RS232 device check that the RPi RP2040 <> TTL-RS232 setup is working, e.g. carry out a loopback test

// Raspberry Pi Pico RP2040 (and W)   Serial1 test - for loopback test connect pins Rx GP1 and Tx GP0

// Note (also  on RPi Pico W)
// Serial is the USB serial for program loading and serial mointor
// there are two hardware serial ports UART0 and UART1
//  Serial1 is mapped to UART0 on Rx pin GP1 Tx pin GP0
//  Serial2 is mapped to UART1 on Rx pin GP5 Tx pin GP4

// for RS232 shield connect - note RP2040 uses 3.3V logic
// RP2040 UART0 RX GP1 to TTL/RS232 Rx
// RP2040 UART0 TX GP0 to TTL/RS232 Tx
// connect GND pins together and VCC to 3.3V on RP2040
// if connecting VCC to 5V use a potential divider on TTL/RS232 Tx to RP2040 RX
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)

#include <Arduino.h>

void setup() {
  // initialize both serial ports:
  delay(5000);
  Serial.begin(115200);
  Serial1.begin(9600);  //115200);
  Serial.println();
  Serial.println("\n\nRaspberry Pi Pico RP2040 Serial1  test Rx pin GP1 Tx pin GP0");
  Serial.write("   for loopback test connect pin 0 to pin 1\n");
  Serial.printf("RS232: RP2040 UART0 GP1 RX to TTL/RS232 Rx and RP0 TX to TTL/RS232 Tx\n");
  Serial.printf("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    //Serial.write('>');
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);
    Serial1.write(inByte);
  }
}

first connect RP2040 pins GP0 and GP1 to carry out loopback test

if that works connect TTL-RS232 module to the RP2040
// RP2040 UART0 RX GP1 to TTL/RS232 Rx
// RP2040 UART0 TX GP0 to TTL/RS232 Tx
// connect GND pins together and VCC to 3.3V on RP2040

connect TTL-RS232 pins 2 and 3 to give a loopback test, e.g. a test run

Raspberry Pi Pico RP2040 Serial1  test Rx pin GP1 Tx pin GP0
   for loopback test connect pin 0 to pin 1
RS232: RP2040 UART0 GP1 RX to TTL/RS232 Rx and RP0 TX to TTL/RS232 Tx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx
loopback test for TTL-RS232
test2 1234567890

I also face the issue in opening the serial monitor sometimes, after connecting the common ground, the serial connection stops and I get the below error message while I try to open the serial monitor

Error while setting serial port parameters: 115,200 N 8 1

Hi @horace I could able to perform the loop back test and below is the data seen on serial monitor

⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮

never seen that error message
is it at compile or run time?

This is a run time error, it occurs as I connect the ground and try to open the serial monitor

have you the RP2040 to TTL-RS232 connections correct?
my connections are

// RP2040 UART0 RX GP1 to TTL/RS232 Rx
// RP2040 UART0 TX GP0 to TTL/RS232 Tx
// connect GND pins together and VCC to 3.3V on RP2040

photo

you may have a faulty TTL-RS232 module - I have had several that failed (probably due to my connecting them up incorrectly)

connected to a PC using a USB-RS232 dongle
image

RP2040 communicates OK with teraterm terminal emulator on PC