Sending/Receiving laser sensor data

I've purchased 2 laser modules (TW10S-UART) from aliexpress. They have no info about programming at all, only the following info and I can't figure out what commands I should send to tell the laser to take and reading and how to get the correct data back.

The information on the aliexpress site had the text formatted just as you see it below.

Would someone be willing to help?

Protocol instruction description:
1: The sensor is ready to continue
Before each ranging, set the EN pin to high level (3.3V ~ 5V), the sensor will be woken up and initialize the hardware, the sensor will continue to issue the following
Response:
01 03 02 00 00 B8 44
Set EN low after the measurement is completed, and the sensor enters low power mode.

2: Read the input register (function code 0x03) (Before issuing this command, wait for the sensor to continue)
Example:
Read measurement distance
Description Address code Function code Start address Number of registers CRC
Send: 0x01 0x03 0x00 0x0F 0x00 0x02 0xF4 0x08
Normal response (measurement distance 57.505m):
Description Address code Function code Number of bytes Register 1 value Register 2 value CRC
Normal response: 0x01 0x03 0x04 0x00 0x00 0xE0 0xA1 0x72 0x4B
Note (the distance in this instruction is 4 bytes, 0x00 0x00 0xE0 0xA1, and the distance is 0x0000E0A1, converted to 57505mm in decimal)

If you make a first attempt on your own how the code might look like.

I did a qoogling
https://www.google.de/search?as_q=TW10S-UART+code

This sensor has a manufacturer. Did you try to look up the manufacturers website?

best regards Stefan

Looks like you query distance with:

byte query[] = {0x01, 0x03, 0x00, 0x0F, 0x00, 0x02, 0xF4, 0x08};

LRFSerial.write(query, sizeof query);

That was:
0x01 (Address)
0x03 (Function: Read Register)
0x000F (Register start address)
0x0002 (Number of 2-byte registers to read)
0xF408 (CRC)

What you receive back is:
0x01 (Address)
0x03 (Function: Register Contents)
0x04 (Data length in bytes)
0xHHHHHHHH (four bytes of distance in mm, MSB first)
0xHHHH (CRC)

I made a valiant attempt on my own and could get the laser to turn on, but the data returned was always 'FF' (255 DEC). It would turn on but wouldn't actually take a reading and return anything that looked like distance data.

I wasn't sure who the manufacturer was and didn't find anything specific when searching. However, I never used the 'code' word as a search word. Your link looks like it might show some sites with info, so I'll continue looking.

Thank you John for parsing the register items. I didn't know how to break it up. I've performed a serial.read before, but how exactly would I break up 4 bytes and convert that to decimal?

Also, I'm not exactly sure what command actually tells the laser to fire to obtain a reading.

Thank you!!

Did you check that data was available before reading? Serial.read() returns -1 if you try to read from an empty buffer. If you store -1 in a byte it becomes 255. Perhaps you should show the sketch you are using.

What baud rate are you using for the sensor? One place says the default is 38400 but it could be anything.

Didn't get a chance to work on it last night after work. I will tonight. I'll post results later.

I also found the spec guide (below). Just gotta figure out the code now I suppose.

Hi, I have found code for that Lasermodule that works so far (not written by me)
Setup:
ESP32 D1 MINI with TW10S UART Laser
Serial2 Pin16/17
Laser EN-Power connected permanently to VCC

my problem now is that i can't bring the Laser to "continuous" mode
it always stopps for a moment so i have no continuous reading of distance.

does anyone know how to do that?

BR

//*****************************************************************************
#include <SoftwareSerial.h>

#define RXD2 16
#define TXD2 17

String sensorReadings;
uint8_t sensorReadingsArr[6];

uint8_t byte6_1, byte5_1, byte6_2, byte5_2, byte5_1_hex, byte5_2_hex, byte6_1_hex, byte6_2_hex;

uint8_t command[8] = {0x01, 0x03, 0x00, 0x0F, 0x00, 0x02, 0xF4, 0x08};
uint8_t data[9];
uint32_t distance;

//*****************************************************************************
uint8_t convert_hex_dec( uint8_t a) {
  uint8_t decimal;

  switch (a) {
    case 0 :
      decimal = 0;
      break;
    case 1 :
      decimal = 1;
      break;
  }
  return decimal;
}

//*****************************************************************************
uint32_t calcul_distance(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
  uint8_t byte5_1_hex = convert_hex_dec(a);
  uint8_t byte5_2_hex = convert_hex_dec(b);
  uint8_t byte6_1_hex = convert_hex_dec(c);
  uint8_t byte6_2_hex = convert_hex_dec(d);
  distance = (byte6_2 * 1 + byte6_1 * 16 + byte5_2 * 16 * 16 + byte5_1 * 16 * 16 * 16) - 50;
  return distance;
}

//*****************************************************************************
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial2.begin(9600);
}

//*****************************************************************************
void loop() {
  Serial2.write(command, 8);
  delay(200);
  Serial2.read(data, 9);
  delay(200);
  byte5_1 = data[5] / 16;
  byte5_2 = data[5] - byte5_1 * 16;
  byte6_1 = data[6] / 16;
  byte6_2 = data[6] - byte6_1 * 16;
  distance = calcul_distance(byte5_1, byte5_2, byte6_1, byte6_2);
  if (distance < 20000) {
    Serial.println(distance);
  }
}

When I try to compile that, I receive an error pointing to the LOOP routine: " 'Serial2' was not declared in this scope"

Ideas?

Did you tell the IDE that you were building for an Arduino MEGA 2560?

that code was done for ESP32 D1 MINI which has 2nd Serial, maybe your Board does not support that ... ?
Just use Serial1 instead ?

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