I have been trying to send and receive serial communications with a laser measurement system. I have been trying serial.write and print and println and looping individual bytes one at a time. . I've tried forcing HEX,DEC,OCT,BIN and nothing seems to be working. I am running out of ideas on how to make it work. The error i keep getting is the data is junk going to and from the laser. If i do the commands just in serial monitor it prints out fine there. I'm using a MEGA and using the serial1 port for communications. The laser claims it is regular ascii txt. Now when i use putty terminal all the commands i send and receive are just fine so i know my serial connections all work. I have backed out to simplify troublshooting and now i am just trying to send the command to the laser and watch the readout on putty.
The command I send to the laser is MS,0,01
The response I am expecting back is MS,+01.23456
Could someone help me out by giving me a sample of how you would send MS,0,01 out the serial1 port?
Where / how are you reading the response from the Laser? If the serial port is connected to the laser, the serial monitor will not be connected therefore cannot show any response.
I do not see how you send anything to the serial port (PC). You're missing at least a Serial.begin() and you're never reading and printing the reply that you receive.
Please provide a link to the description of the protocol.
Very far from correct syntax.
The modificator "DEC" is applicable with numeric data only (but you trying using it with a string) and within the print() command (but not with write())
The part that throws me off is that with putty running with theses settings I am getting the right responses using just serial on windows and no arduino.
That part states RS232. Are you using a serial-to-RS232 converter? Or did you directly connect the laser measurement system to the Mega?
Where do you get the communication parameters (baud rate etc) from?
Below can act as a framework to start testing the communication; it uses a simple state machine.
char command[] = "MS,0,01";
char terminator = '\r';
// timeout duration for receive from laser equipment
const uint32_t timeoutDuration = 5000;
void setup()
{
Serial.begin(115200);
Serial1.begin(9600);
}
void loop()
{
// send command and receive reply
if (fsmComms() == true)
{
// wait a bit
delay(2000);
}
}
/*
Finite state machine for comms with laser equipment
Returns:
false while in progress
true if full reply received or in case of timeout
*/
bool fsmComms()
{
enum STATES
{
SEND,
RECEIVE,
TIMEOUT,
};
bool retVal = false;
// state of state machine
static STATES state = SEND;
// start time for timeout
static uint32_t timeoutStartTime;
switch (state)
{
case SEND:
Serial1.print(command);
Serial1.write(terminator);
timeoutStartTime = millis();
state = RECEIVE;
break;
case RECEIVE:
// if a timeout occurs
if (millis() - timeoutStartTime >= timeoutDuration)
{
state = TIMEOUT;
}
// check if there is data received
else if (Serial1.available())
{
// reset start time for timeut
timeoutStartTime = millis();
// read a byte
char ch = Serial1.read();
// print it
if (ch < 0x10)
{
Serial.print("0");
}
Serial.print(ch, HEX);
Serial.print(" ");
// stop when we receive a terminator
if (ch == terminator)
{
Serial.println();
state = SEND;
retVal = true;
}
}
break;
case TIMEOUT:
Serial.println(F("[Timeout]"));
retVal = true;
state = SEND;
break;
}
return retVal;
}
Tested with a Leonardo that echoes the received data back to the sending Mega.
Thank you so much. This got me working for my first hurdle. I did have to remove the println line to remove spaces between char and change the baud rate for my serial monitor but other than that it was perfect. Now I just have to figure out getting the rest of the way which will be a bunch more reading up on everything. I'll admit it but i did do a dumb thing and had my wires backwards on my ttl to 232 converter and that held me up for a few days of not understanding the garbage i was getting as well,