Hi all, I have a project where I'm trying to interface with a laser rangefinder module using arduino, however I'm having a little bit of difficulty.
Essentially I need to send a command in hex, receive the hex reply, convert the sixth and seventh bytes to decimal at which point I can use that however I need.
Right now I'm trying to use software serial to connect to the module so the arduino can output to the computer.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
byte singleshot[8]={0x55,0xAA,0x88,0xFF,0xFF,0xFF,0xFF,0x84};
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Native USB only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(115200);
}
void loop() // run over and over
{
Serial.write(singleshot, sizeof(singleshot));
delay(5000);
mySerial.write(singleshot, sizeof(singleshot));
Serial.println(mySerial.read());
delay(1000);
}
The problem I'm having at least right now, just trying to send, is that a whole bunch of extra stuff is getting put in front of my message.
55 AA 88 FF FF FF FF 84 is what I need to send to the module however when I use a serial monitor that can handle hex, I instead see
47 6F 6F 64 6E 69 67 68 74 20 6D 6F 6F 6E 21 OD 0A 55 AA 88 FF FF FF FF 84
I have a feeling this same extra stuff at the start is being sent to my module hence getting a garbage response, but I'm not sure why it's adding all that extra stuff in.
When I use a different serial program I don't specify 0x55, 0xAA etc and maybe that's part of it? The thing is if I remove the 0x section then I get errors when I try to compile, saying that things such as "AA" weren't defined in the scope.
The only other part of this I think will be tricky is receiving the serial response from the module also in hex, and filtering out specific bytes to use, the third byte of the response would be 01 or 00 for success or failure, and ignoring failures would be good, and then I just need to isolate the sixth and seventh bytes to use with strtol as far as I can tell.
The second part doesn't matter much if I can't get the stuff being sent to the module to make sense though.