How to talk [YDLIDAR X4] on USB port

I am trying to connect to YDLIDAR X4 with Processing serial library.
As the first step, I want to send system commands to X4, for example 0xA5 0x60 to being scan mode.
But X4 return nothing so far. X4 is definitely connecting on COM3. Thanks your help.

YDLIDAR manual
[https://www.ydlidar.com/Public/upload/files/2022-06-28/YDLIDAR%20X4%20Development%20Manual%20V1.6(211230).pdf](https://YDLIDAR manual)

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() {
  size(200, 200);
  myPort = new Serial(this, "COM3", 9600);
}

void draw(){
  background(255);    
  if (val == 0) {          
    fill(0);                 
  } 
  else {                 
    fill(204);            
  }
  rect(50, 50, 100, 100);
  println(val);
}
void serialEvent(Serial myPort){
  String myString = myPort.readString();
  if(myString != null){
    println(val);
  }
  println("getEvent!");
}
void mousePressed(){
  myPort.write(char(0xA5));
  myPort.write(char(0x60));
}

Hello @koba, An interesting challenge, at least the manual is clearly written. Your transmit of xA5 and x60 looks correct to me. Does the X4 have transmit and receive lights like an Arduino? You would see the enquiry arriving, and then if there’s a reply.

What about baud rate? You must have the same baud rate in X4 and Serial.begin. Do you have another piece of equipment that talks to the X4? That would show you the X4 is alive, and you might be able to use Processing to receive the conversation in parallel.

For receiving the chars from X4 I think you should have very simple code, without Serial.Event and readString. Suggest lift the code from this draw.

import processing.serial.*;

Serial myPort;
int val;

void setup() {
  size(100, 100);
  frameRate(60);
  String portName = "COM8";
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  int val;
  while (myPort.available() > 0) {
    val = myPort.read();
    print(String.format("%02x ", val));
  }
}

1 Like