RS232 Serial Device input to Mega 2560

Hi there,
I'm a n00b as you'd put it and out of my depth here. Looking for some guidance please.

I am trying to interface a TM-8818 device to Mega 2560.

I have run all manner of code from simple to advanced, partly my own work and partly ChatGPT to assist.

I have the device connected to 5v, ground, TX2/RX2.
I wait to see 10h from the device then send 20h to accept the next transmission where i expect to see 08h but never see it. I do some some bytes come through but the results are all over the place, no consistency and I cannot decode it into anything that makes sense.
I am using an adapter (2 actually) JY-R2T V1.4 and HW-044:
Arduino Compatible RS-232 to TTL UART Converter Module | Jaycar Electronics
RS232 to Serial Converter | Core Electronics Australia (core-electronics.com.au)

When I check the volts (DC) on the output of the device (which is a 2.5mm stereo plug) I see:
4.2v between the middle band and the ground.
0.8v between the tip and the ground.
The base band is ground.
The 4.2v drops to 4.0v and keeps going between the 2 values in a steady fashion when the probe is in use during a measurement activity.
The 0.8v seems to drop to 0.1v then doesn't do much after that.

When the code is running, and the TX/RX is one way I get back:
14:24:51.826 -> Received data from Serial2 - Byte 1: 0 (Decimal: 0)
14:24:51.924 -> Received data from Serial2 - Byte 2: 0 (Decimal: 0)
14:24:52.023 -> Received data from Serial2 - Byte 3: 0 (Decimal: 0)
14:24:52.153 -> Received data from Serial2 - Byte 4: 0 (Decimal: 0)
14:24:52.252 -> Received data from Serial2 - Byte 5: 0 (Decimal: 0)
14:24:52.350 -> Received data from Serial2 - Byte 6: 0 (Decimal: 0)
14:24:52.481 -> Received data from Serial2 - Byte 7: 0 (Decimal: 0)
14:24:52.579 -> Received data from Serial2 - Byte 8: 0 (Decimal: 0)
14:24:52.710 -> Received complete data from Serial2:
14:24:52.776 -> Byte 1: 0 (Decimal: 0)
14:24:52.808 -> Byte 2: 0 (Decimal: 0)
14:24:52.874 -> Byte 3: 0 (Decimal: 0)
14:24:52.940 -> Byte 4: 0 (Decimal: 0)
14:24:52.973 -> Byte 5: 0 (Decimal: 0)
14:24:53.038 -> Byte 6: 0 (Decimal: 0)
14:24:53.071 -> Byte 7: 0 (Decimal: 0)
14:24:53.136 -> Byte 8: 0 (Decimal: 0)
14:24:53.169 -> Invalid product code received.

When I swap the RX/TX I get:
14:24:56.250 -> Received data from Serial2 - Byte 2: 0 (Decimal: 0)
14:24:56.348 -> Received data from Serial2 - Byte 3: 20 (Decimal: 32)
14:24:56.479 -> Received data from Serial2 - Byte 4: 0 (Decimal: 0)
14:24:56.577 -> Received data from Serial2 - Byte 5: 20 (Decimal: 32)
14:24:56.708 -> Received data from Serial2 - Byte 6: 0 (Decimal: 0)
14:24:56.807 -> Received data from Serial2 - Byte 7: 20 (Decimal: 32)
14:24:56.938 -> Received data from Serial2 - Byte 8: 0 (Decimal: 0)
14:24:57.036 -> Received complete data from Serial2:
14:24:57.102 -> Byte 1: FE (Decimal: 254)
14:24:57.167 -> Byte 2: 0 (Decimal: 0)
14:24:57.232 -> Byte 3: 20 (Decimal: 32)
14:24:57.265 -> Byte 4: 0 (Decimal: 0)
14:24:57.331 -> Byte 5: 20 (Decimal: 32)
14:24:57.396 -> Byte 6: 0 (Decimal: 0)
14:24:57.429 -> Byte 7: 20 (Decimal: 32)
14:24:57.494 -> Byte 8: 0 (Decimal: 0)
14:24:57.527 -> Invalid product code received.
14:24:57.593 -> Received data from Serial2 - Byte 1: 20 (Decimal: 32)
14:24:57.724 -> Received data from Serial2 - Byte 2: 0 (Decimal: 0)
14:24:57.822 -> Received data from Serial2 - Byte 3: 20 (Decimal: 32)
14:24:57.953 -> Received data from Serial2 - Byte 4: FE (Decimal: 254)
14:24:58.051 -> Received data from Serial2 - Byte 5: 20 (Decimal: 32)
14:24:58.182 -> Received data from Serial2 - Byte 6: FE (Decimal: 254)
14:24:58.281 -> Received data from Serial2 - Byte 7: 0 (Decimal: 0)
14:24:58.412 -> Received data from Serial2 - Byte 8: 20 (Decimal: 32)
14:24:58.510 -> Received complete data from Serial2:
14:24:58.609 -> Byte 1: 20 (Decimal: 32)
14:24:58.641 -> Byte 2: 0 (Decimal: 0)
14:24:58.707 -> Byte 3: 20 (Decimal: 32)
14:24:58.740 -> Byte 4: FE (Decimal: 254)
14:24:58.805 -> Byte 5: 20 (Decimal: 32)
14:24:58.871 -> Byte 6: FE (Decimal: 254)
14:24:58.903 -> Byte 7: 0 (Decimal: 0)
14:24:58.969 -> Byte 8: 20 (Decimal: 32)
14:24:59.035 -> Invalid product code received.

I believe this is more correct but still I don't know how to decode the feedback or whether it's even giving me back any real data at all.

The RX and TX lights never blink on the RS232-TTL Adapter.
The TX light on the Mega 2560 is lit constantly, no blinking.
The RX light on the Mega 2560 remains unlit the whole time.

This is the documentation I was provided with, but I don't know its accuracy. On one hand it was given to me as a Bluetooth data protocol but on the other it was claimed to be for RS232.

Data flow:

  1. Send 10H ;always begin with 10H
    reply: not agree to receive : FFH
    agree to receive: 20H
  2. Send 08H and start to accumulate the sum ; will send 8 bytes continuously as below
  3. Then send the 8-bytes Da
    1st byte is the product code,
    e.g. 06H (MtypeCode), meaning Ultrasonic Thickness Meter
    2nd byte is function code, see the table below. (DtypeCode),
    E.g. 11 H meaning Cast Iron,its unit is mm
    3rd to 7th byte (from high to low),digit 0-9h, meaning 0-9
    0Ah, do not display
    8th byte is dot position
    0 mean integer (no dot)
    1 means 1 bit dot (xx.x)
    2 means 2 bit dot (x.xx)
    3 means 3 bit dot (x.xxx)
    4 means 4 bit dot (x.xxxx)
    5 means Negative, 1 bit dot (-xx.x)
    6 means Negative,2 bit dot (-x.xx)
  4. Send the accumulative sum

MTypeCode MTypeName DTypeCode DTypeName Unit
06 Ultrasonic Thickness Gauge 11 Cast Iron mm
06 Ultrasonic Thickness Gauge 12 Steel mm
06 Ultrasonic Thickness Gauge 13 Aluminium mm
06 Ultrasonic Thickness Gauge 14 Red Copper mm
06 Ultrasonic Thickness Gauge 15 Brass mm
06 Ultrasonic Thickness Gauge 16 Zinc mm
06 Ultrasonic Thickness Gauge 17 Quartz Glass mm
06 Ultrasonic Thickness Gauge 18 Polyethylene mm
06 Ultrasonic Thickness Gauge 19 PVC mm
06 Ultrasonic Thickness Gauge 20 Gray Cast Iron mm
06 Ultrasonic Thickness Gauge 21 Nodular Cast Iron mm
06 Ultrasonic Thickness Gauge 22 Measured by Velocity mm
06 Ultrasonic Thickness Gauge 23 Gold mm
06 Ultrasonic Thickness Gauge 24 Silver mm
06 Ultrasonic Thickness Gauge 25 Platinum mm
06 Ultrasonic Thickness Gauge 26 Palladium mm
06 Ultrasonic Thickness Gauge 31 Cast Iron inch
06 Ultrasonic Thickness Gauge 32 Steel inch
06 Ultrasonic Thickness Gauge 33 Aluminium inch
06 Ultrasonic Thickness Gauge 34 Red Copper inch
06 Ultrasonic Thickness Gauge 35 Brass inch
06 Ultrasonic Thickness Gauge 36 Zinc inch
06 Ultrasonic Thickness Gauge 37 Quartz Glass inch
06 Ultrasonic Thickness Gauge 38 Polyethylene inch
06 Ultrasonic Thickness Gauge 39 PVC inch
06 Ultrasonic Thickness Gauge 40 Gray Cast Iron inch
06 Ultrasonic Thickness Gauge 41 Nodular Cast Iron inch
06 Ultrasonic Thickness Gauge 42 Measured by Velocity inch
06 Ultrasonic Thickness Gauge 43 Gold Inch
06 Ultrasonic Thickness Gauge 44 Siver Inch
06 Ultrasonic Thickness Gauge 45 Platinum Inch
06 Ultrasonic Thickness Gauge 46 Palladium Inch

These devices (TM-8818) are very common low-cost item sold under many different brands and model numbers; I imagine there's a very simple way to collect the data.

Now on the below code, my basic flow should be to wait for 10h from the device, respond with 20h to accept the data flow... expect 08h to signify the start of data transmission, followed by:
06H, 32, 0, 0, 0, 4, 5, 1, ??
Expecting measurement of 4.5mm from the above, I think?
That's what the screen on the device is showing

My current code, courtesy of ChatGPT (please go easy on me, I started with very basic code but got nowhere):

Also, I tried connecting the adapter via FTDI usb plug straight to my pc and use terminal programs and also putty but I got nowhere..
I hope I have provided enough details.
Thanks in advance for any support.

// Global variables
const int waitTime = 10000; // 10 seconds
const int baudRate = 4800; // Baud rate for communication
byte incomingByte;
byte commandBuffer[8]; // Buffer for 8 bytes of data
int byteIndex = 0; // Index for storing data in commandBuffer

// Setup function
void setup() {
  // Initialize all serial interfaces
  Serial.begin(baudRate);
  Serial1.begin(baudRate);
  Serial2.begin(baudRate);
  Serial3.begin(baudRate);
  Serial.println("Setup complete. Starting communication...");

  // Start communication with the device
  startCommunication();
}

void loop() {
  // Interrogate each serial interface
  Serial.println("Interrogating devices...");
  readSerialData(Serial, "Serial");
  readSerialData(Serial1, "Serial1");
  readSerialData(Serial2, "Serial2");
  readSerialData(Serial3, "Serial3");

  // Wait for 10 seconds before repeating
  delay(waitTime);
}

void startCommunication() {
  Serial.println("Sending 20H...");
  Serial2.write(0x20); // Send command to agree to receive data

  // Wait for the device to send 08H
  Serial.println("Waiting for 08H response...");
  while (Serial2.available() == 0) {
    // Wait for a response
  }

  byte response = readResponse(Serial2);
  Serial.print("Response received: ");
  Serial.println(response, HEX);

  if (response == 0x08) {
    Serial.println("Received 08H. Expecting 8 bytes of data...");
    for (int i = 0; i < 8; i++) {
      readSerialData(Serial2, "Serial2");
    }
  } else {
    Serial.println("Did not receive expected 08H.");
  }
}

byte readResponse(Stream& serial) {
  if (serial.available()) {
    return serial.read();
  }
  return 0; // No response
}

void readSerialData(Stream& serial, const char* serialName) {
  while (serial.available()) {
    incomingByte = serial.read();

    Serial.print("Received data from ");
    Serial.print(serialName);
    Serial.print(" - Byte ");
    Serial.print(byteIndex + 1);
    Serial.print(": ");
    Serial.print(incomingByte, HEX);
    Serial.print(" (Decimal: ");
    Serial.print(incomingByte, DEC);
    Serial.println(")");

    commandBuffer[byteIndex++] = incomingByte;

    if (byteIndex >= 8) {
      Serial.print("Received complete data from ");
      Serial.print(serialName);
      Serial.println(":");

      for (int i = 0; i < 8; i++) {
        Serial.print("Byte ");
        Serial.print(i + 1);
        Serial.print(": ");
        Serial.print(commandBuffer[i], HEX);
        Serial.print(" (Decimal: ");
        Serial.print(commandBuffer[i], DEC);
        Serial.println(")");
      }

      // Check for valid product code
      if (commandBuffer[0] == 0x06) {
        // Extract the function code
        byte functionCode = commandBuffer[1];
        Serial.print("Function Code: ");
        Serial.println(functionCode, HEX);

        // Build the measurement from bytes 3 to 7
        int measurement = 0;
        for (int i = 0; i < 5; i++) {
          measurement = (measurement * 10) + (commandBuffer[i + 2] & 0x0F);
        }

        // Check dot position from byte 8
        int dotPosition = commandBuffer[7];
        if (dotPosition > 0 && dotPosition < 5) {
          float decimalFactor = pow(10, -dotPosition);
          measurement *= decimalFactor;
        } else if (dotPosition >= 5 && dotPosition < 7) {
          measurement = -measurement; // Apply negative
          float decimalFactor = pow(10, -(dotPosition - 4));
          measurement *= decimalFactor;
        }

        Serial.print("Measurement: ");
        Serial.println(measurement);
      } else {
        Serial.println("Invalid product code received.");
      }

      byteIndex = 0; // Reset for next reception
    }

    serial.write(0x20); // Respond with 20H
  }

  if (byteIndex == 0) {
    Serial.print("No data available on ");
    Serial.println(serialName);
  }
}

edit: sorry I forgot to add, probably very important.
This is the monitor feedback I get on the Arduino when the device is off and unplugged !!!!
Clearly creating its own data.

14:49:31.898 -> Received data from Serial2 - Byte 1: 20 (Decimal: 32)
14:49:31.996 -> Received data from Serial2 - Byte 2: 0 (Decimal: 0)
14:49:32.127 -> Received data from Serial2 - Byte 3: 0 (Decimal: 0)
14:49:32.226 -> Received data from Serial2 - Byte 4: 20 (Decimal: 32)
14:49:32.357 -> Received data from Serial2 - Byte 5: 20 (Decimal: 32)
14:49:32.455 -> Received data from Serial2 - Byte 6: 0 (Decimal: 0)
14:49:32.554 -> Received data from Serial2 - Byte 7: 20 (Decimal: 32)
14:49:32.685 -> Received data from Serial2 - Byte 8: 0 (Decimal: 0)
14:49:32.782 -> Received complete data from Serial2:
14:49:32.881 -> Byte 1: 20 (Decimal: 32)
14:49:32.914 -> Byte 2: 0 (Decimal: 0)
14:49:32.979 -> Byte 3: 0 (Decimal: 0)
14:49:33.012 -> Byte 4: 20 (Decimal: 32)
14:49:33.078 -> Byte 5: 20 (Decimal: 32)
14:49:33.143 -> Byte 6: 0 (Decimal: 0)
14:49:33.176 -> Byte 7: 20 (Decimal: 32)
14:49:33.242 -> Byte 8: 0 (Decimal: 0)
14:49:33.274 -> Invalid product code received.

You have to specify the individual baudrate for each channel. For Serial (PC) the baudrate can be set in the Serial Monitor, usually to 9600.

Don't use ChatGPT code. Instead try the IDE Examples. Also see the Related Topics below.

Hi, thanks for your reply.
I believe each interface already is set with the global rate defined.
the monitor is also set to match.
You mention the pc. when I tried direct to pc I also set the port as 4800 and putty too. Also tried 9600 in the code and pc as an option, no different.

I've tried so many now I'm clutching at straws, willing to try anything.
I feel like there's something major I've overlooked.

Are you certain that has an actual RS-232 output? The only picture I can find of the accessory cable has the 2.5mm plug at one end and USB at the other, and a software disk labeled "RS232C Software".

Hi, no, not certain at all. I can only go off what the product marketing says. At a guess, the adapter simply converts the RS232 to something the computer can interact with on USB. I will probably need to purchase one of these adapters and take a look at what it does. Hoping someone here might have some experience with these, there are some similar threads but nothing exact unfortunately.
I have opened the device and found it has a MM74HC chip with quite a few pins that are only a few ohms from the stereo plug. I'm not sure whether it is doing ttl conversion, my googling says no but it's definitely not far away from the output socket so probably related. I don't know if that helps at all.

I think your wring is incorrect.

Unlike connection between two RS232 devices, connection between MCU and TTL to RS232 module should be:

  • Arduino RX pin should connect to RXD of RS232 module
  • Arduino TX pin should connect to TXD of RS232 module

You can see the wiring diagram and test code in this Arduino RS232 tutorial

Here's the output of the device when it's not plugged into anything:

81

And here's the output when I'm actively measuring a piece of steel:
83

EDIT:
Changed some settings and got what I thought I might see.

84

Where do I look next? The saga continues......

Thanks !

If you want to verify the wiring of the RS-232 adapter board, you should be able to do a loopback test by connecting pins 2 & 3 of the 9-pin connector together.

Can you provide either the complete chip number, or a good picture of the circuit board? The device may be sending TTL level serial, instead of actual RS-232 levels.

4800 baud would be a base frequency of 4800Hz, or a pulse width of a little over 0.2mS, far from what you show in the pictures.

Google sez :

“Use USB/RS-232 data output to connect with PC. (The USB and software is Optional,please contact us if you need it) “

Check each connection using the approved IDE example codes before combining everything. If you want to use a special PC connection you may have to close the Serial Monitor first.

It looks to me like you missed to connect GND or measure AC instead of DC-

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