Reading Data from Keyence Laser Micrometer to Arduino

const char Handshake = 'M0,1';
int byteRecieved;
int bytesend;

void setup(){
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("M0,1,/r");
}
void loop(){
Serial.println("M0,1,");

if(Serial1.available())
{

  Serial.println("Available");
  Serial1.write("M0");
  Serial1.write("1");
  byteRecieved = Serial1.read();
  Serial1.println(byteRecieved);
}
delay(1000);
}

This is the code I have created to test and see if I can receive any measured values from the Keyence Ls, 7030 laser micrometer. Currently, there is a signal being received to the Arduino and saying that the serial is available, but no values are coming through. The serial monitor reading is:

M0,1e
M0,1,
Available
M0,1,
Available
M0,1,
Available

M0,1,
Available
M0,1,
Available
M0,1,
Available
M0,1,
Available

You could change some of the print statements to Serial so it would not be so confusing as to what is printed, what is sent and what is received.

a7

You are sending the byte you received from Serial1, which I presume is the micrometer back to the micrometer. Instead to the serial monitor window.
So it should be:-

Serial.println(byteRecieved);
}

You are also requesting more data before gathering an entire response from the device you taking to.

So maybe it gets confused, or resets and always starts over at the beginning of what its message should consist in.

a7

That was an issue I thought might be occurring, but I don't know how to allow it to read all of the data.

I have made that change and am only receiving 0 back.

Do you know the format of the response, so you could read until some end marker?

Or you could read as long as data is available and print that to the monitor (for now), and stop that when, say, 500 ms went by with nothing available.

a7

Homework:

Not a waste of time. Should help you along.

a7

It's a 28 character long output that contains "," to separate the different sections. An example of what the manual says the output should be is:

M0,+01.23450,HI,+24.62340,LO

Another thing you can always do is to start by talking to the device just using PuTTY or CoolTerm or other terminal emulator program, direct from the PC, no code, no Arduino.

Until you get a feel for how it responds.

a7

I have read through this, but I could try it again. I was attempting to apply some of the example concepts, but was unsuccessful.

I have used Terra Term and I was able to enter the handshake statement "M0,1" and receive back the correct measured output.

OK, does the manual say there'll be a carriage return or line feed at the end of the message?

a7

carriage feed

Maybe

Once you see data is available, use read/until to grab up the entire package.

a7

Ill give it a read thank you.

Maybe stop sending something back to the micometer and print all the data you have in the buffer like this:-

while(Serial1.available())
{
  Serial.print("Received -  ");
  byteRecieved = Serial1.read();
  Serial.println(byteRecieved);
}

I have continued to plug away at this, but now with this suggestion I am receiving empty values.

What do you mean by this?

Is it the same as

No it was giving back no values at all. Also I was able to implement some new pieces to the code that is sending some numbers, but the are still not correct.