Go Down

Topic: Reading serial from a laser mic (Read 2746 times) previous topic - next topic

sspence65

May 31, 2018, 08:29 pm Last Edit: May 31, 2018, 08:32 pm by sspence65
I have an micro-epsilon laser micrometer. I'm attempting to read the serial output. It's rs422, and I have a rs422 to ttl adapter connected to an Arduino Nano using SoftSerial.

laser mic is set to ASCII, 38400, 8N1.

with the attached code I'm reading a repeating series of numbers on the serial monitor:

0 103 179 86 118 214 191 191 191 191 191 229 (repeats)

when the laser mic display is showing 0.2963"

I would like to see something like 2963 or similar in the serial monitor :-)

Code: [Select]

#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 3); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(38400);

  // set the data rate for the SoftwareSerial port
  mySerial.begin(38400);
  Serial.println("Hello, LaserMic!");
}


void loop() {
    if (mySerial.available() > 0) {
        int ch = mySerial.read();
        // show the byte on serial monitor
        Serial.println(ch, DEC);
    }
}



I've attached the appropriate 2 pages of the manual covering the data specification. I would love some ideas to try.
Steve Spence - KK4HFJ

Perehama

Code: [Select]
int ch = mySerial.read();
Serial.println(ch, DEC);

change int to char.
DEC is redundant
F=C/V=(A*s)/V=J/V^2=(W*s)/V^2=(N*m)/V^2=C^2/J=C^2/(N*m)=(s^2*C^2)/(m^2*Kg)=s/Ω=1/(Ω*Hz)=s^2/H

TolpuddleSartre

Have you downloaded the serial protocol specification for the device?

sspence65

#3
May 31, 2018, 08:53 pm Last Edit: May 31, 2018, 09:01 pm by sspence65
Quote
Have you downloaded the serial protocol specification for the device?
The serial specification was attached to my post.

ASCII format
Twelve characters are always output as a minimum with the first five figures as standard
corresponding to the digital value of the measurement and being continuously output.
Steve Spence - KK4HFJ

sspence65

change int to char.
DEC is redundant
Code: [Select]

char ch = mySerial.read();

Serial.println(ch);


prints repeating backwards question marks
Steve Spence - KK4HFJ

TolpuddleSartre

It's simpler to print the values in hex, rather than decimal - it will help you follow the values more easily.

Code: [Select]
int ch = mySerial.read();

Serial.println(ch, HEX);

sspence65

The operator needs to see the actual micrometer reading in inches and MM.
Steve Spence - KK4HFJ

TolpuddleSartre

The operator needs to see the actual micrometer reading in inches and MM.
I know that - I'm trying to help you to decode the serial stream. That's easier in hex than it is in decimal.
 (they're "mm", not "MM")

Perehama

Code: [Select]

char ch = mySerial.read();

Serial.println(ch);


prints repeating backwards question marks
In the "options menu" of the datasheet, one can select binary or ascii output. As I don't have a laser, I can't try the code myself... leaving the code alone (with char instead of int), have you tried playing with the options to see how they affect what is received by the Arduino?
In ASCII mode, you should see 5 numerals, tab, 5 numerals <cr>
It will be a number between 0 and 65535 with 65520 being the top measurement and the upper numbers being error codes.
If you see 5 numbers on the serial monitor repeated, instead of question marks or other stuff, you are reading the data correctly...
You will need to convert the ascii into number values by subtracting 48 and multiplying by the place value... then add those to an unsigned int...
once you have them stored in the unsigned int, you can perform the calculation to the float you want...
Code: [Select]
float measuredValue = unsignedIntValue * 40.824 / 65519 - 0.4204872
F=C/V=(A*s)/V=J/V^2=(W*s)/V^2=(N*m)/V^2=C^2/J=C^2/(N*m)=(s^2*C^2)/(m^2*Kg)=s/Ω=1/(Ω*Hz)=s^2/H

TolpuddleSartre

#9
May 31, 2018, 09:44 pm Last Edit: May 31, 2018, 10:03 pm by TolpuddleSartre
The closest I can get is 0.346" with those numbers (0x39c0, assuming binary, not ASCII protocol), but I'm a bit confused about the second, third and fourth triplets.

Rather than printing the numbers as you receive them, can you buffer them and then print?

(The protocol definition is a bit confusing - it refers to "DV", then uses "DW", and doesn't explain what "seg 1..4" refer to)

gfvalvo

Quick / simple things to check:

1. Lower the baud rate
2. Swap RS422 polarity

sspence65

I know that - I'm trying to help you to decode the serial stream. That's easier in hex than it is in decimal.
 (they're "mm", not "MM")
ok, cool. and yess, knew it was mm :-)
Steve Spence - KK4HFJ

sspence65

In the "options menu" of the datasheet, one can select binary or ascii output. As I don't have a laser, I can't try the code myself... leaving the code alone (with char instead of int), have you tried playing with the options to see how they affect what is received by the Arduino?
In ASCII mode, you should see 5 numerals, tab, 5 numerals <cr>
It will be a number between 0 and 65535 with 65520 being the top measurement and the upper numbers being error codes.
If you see 5 numbers on the serial monitor repeated, instead of question marks or other stuff, you are reading the data correctly...
You will need to convert the ascii into number values by subtracting 48 and multiplying by the place value... then add those to an unsigned int...
once you have them stored in the unsigned int, you can perform the calculation to the float you want...
Code: [Select]
float measuredValue = unsignedIntValue * 40.824 / 65519 - 0.4204872

using int I get numbers, using char I get reverse question marks. It looks like the 0 is the stop character, so I need to look for a zero, grab the next 5 readings, and start again at the next zero.
Steve Spence - KK4HFJ

TolpuddleSartre

#13
May 31, 2018, 10:55 pm Last Edit: May 31, 2018, 10:55 pm by TolpuddleSartre
Which protocol have you got selected?
I've been assuming binary.

(I don't believe @Perehama' interpretation is correct)

sspence65

ok, cool. and yess, knew it was mm :-)
It's simpler to print the values in hex, rather than decimal - it will help you follow the values more easily.

Code: [Select]
int ch = mySerial.read();

Serial.println(ch, HEX);

I get repeating combinations of

0 67 B3 56 D6 ED BF BF BF BF BF E5 with .2968 on the display

and

0 B3 F6 D6 16 D6 BF BF BF BF BF E5 with .4990 on the display
Steve Spence - KK4HFJ

Go Up