Beginner Serial Communication

Hello,

I am relatively new to Arduino and have never done any serial communication before. The process sounds simple enough but I'm having trouble getting it to work. Let me describe my problem. I am using a Fluke 414D Laser distance meter, coupled with the Porcupine Electronics LR4 Interface Board. There are two ways to communicate with the LR4 interface board: via USB through your computer, and via the TX and RX pins on the board to an appropriate micro-controller. I am using the latter, with an Arduino Leonardo.

I should note however, that the laser meter works with the USB communication to my computer and with the Porcupine Electronics demo software.

Here is a link to the LR4 website: http://www.porcupineelectronics.com/Laserbotics.html And this is the datasheet (please refer to page 10 and 11): http://www.porcupineelectronics.com/uploads/LR4_Data_Sheet.pdf

As per the datasheet, I am trying to start the laser by sending the byte 'g' via the TX pin on the Arduino, to the RX pin on the LR4 board...but I am not getting any response. Additionally, the LR4 board is outputting the decimal value 255 when I call Serial.read().

I have tried a number of different variations of my code but nothing seems to work. Here is an example anyway of some code...that didn't work.

byte goByte = 0x67;                //g
byte stopByte = 0x73;              //s
byte inByte = 0;                   //recieved bytes via RX

void setup(){
  Serial.begin(9600);              //9600 as per the datasheet
  while(!Serial){                  //I have read that this is important for the Leonardo
    ;
  }
}

void loop(){
  Serial.write(stopByte);          //Stop the laser
  delay(5000);
  Serial.write(goByte);            //Start the laser
  delay(5000);
  
  if(Serial.available() > 0);{
   inByte = Serial.read();          //Read what's on the RX line
   delay(500);
   Serial.print("Incoming Byte: ");  
   Serial.println(inByte);   
   delay(500);
  }
}

Now, this code isn't suppose to read distance measurements or anything, I am simply trying to get the laser to turn on.

Please help!!

Thank you in advance!! :)

if(Serial.available() > 0);{ <= there should not be a semicolon here

I don't see how you can communicate with both the external device and the computer at the same time with only a single Serial object.

  while(!Serial){                  //I have read that this is important for the Leonardo
    ;
  }

Read that where? If you'd been paying attention, you'd have observed that you need to wait for Serial1, not Serial, to be ready.

In addition to HazardsMind's comment, spacesinyourcodewouldbeagoodidea.

HazardsMind:
if(Serial.available() > 0);{ <= there should not be a semicolon here

Thank you, now it’s not reading anything off of the RX line! :~

Arrch: I don't see how you can communicate with both the external device and the computer at the same time with only a single Serial object.

No, sorry to confuse. I was simply testing the USB communication to make sure my installation was working properly. I was not testing with both Arduino serial and computer USB at the same time.

PaulS:

  while(!Serial){                  //I have read that this is important for the Leonardo

;
  }



Read that where? If you'd been paying attention, you'd have observed that you need to wait for Serial1, not Serial, to be ready.

In addition to HazardsMind's comment, spacesinyourcodewouldbeagoodidea.

I read it off of the Arduino reference…from this post http://arduino.cc/en/Tutorial/SerialCallResponseASCII
In the setup function, there is a while loop and the comment specifies “Needed for Leonardo only”.

Also…not sure what you mean about spaces?

bjornert: No, sorry to confuse. I was simply testing the USB communication to make sure my installation was working properly. I was not testing with both Arduino serial and computer USB at the same time.

Then how do you know what value you are getting from the external device if you're not using both the device and the serial monitor at the same time?

Also...not sure what you mean about spaces?

if(Serial.available() > 0);{

If there were space (or, even better, a carriage return) after the ;, it would be a lot easier to see that hiding there. If the ; were not (incorrectly) there, there SHOULD be a space (or, even better, a carriage return) before the {.

Arrch: Then how do you know what value you are getting from the external device if you're not using both the device and the serial monitor at the same time?

I know the value I'm getting because I am using the serial monitor when I have the laser hooked up to the Arduino.

If you refer to the links I posted in my first question, you'll notice that the LR4 interface board can communicate both through USB and serially to a micro-controller. I simply used the USB method to make sure my installation was correct (LR4 directly to my computer - ie no Arduino). It now has nothing to do with my problem as the LR4 has been hooked up to the Arduino via the TX, RX, 5V and GND pins.

Your using the serial monitor correct? Then what you type should appear as the output.

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
/* what ever else you have */
  if(Serial.available() > 0) 
    {
       byte inByte = Serial.read(); //read it

       delay(10); //just some delay to make sure you get everything

       Serial.println(inByte, DEC); // show it
   }
}

bjornert:
It now has nothing to do with my problem as the LR4 has been hooked up to the Arduino via the TX, RX, 5V and GND pins.

The problem is that your Serial monitor is using those same TX and RX pins, so anything you send to the Serial Monitor is also getting sent to the LR4.

It now has nothing to do with my problem as the LR4 has been hooked up to the Arduino via the TX, RX, 5V and GND pins.

If that's the case, then you need to get an Arduino with more than one set of TX/RX pins. You can maybe use Software Serial, or get an Arduino Mega.

HazardsMind: Your using the serial monitor correct? Then what you type should appear as the output.

void setup() 
{
  Serial.begin(9600);
}

void loop() { /* what ever else you have */  if(Serial.available() > 0) {    {       byte inByte = Serial.read(); //read it

      delay(10); //just some delay to make sure you get everything

      Serial.println(inByte, DEC); // show it   } }

Thank you, but what I'm really trying to get done is turn the laser on. The incoming data means nothing to me unless the laser is on. I'm more interested in this bit of code...

Serial.write(goByte);  //where goByte = 0x67 (or 'g')

Is this correct? Am I initializing the serial ports correctly? I'm just not sure why the laser isn't turning on...

I have also tried a number of different methods to send the 'g', and have placed them in many different spots. The setup function, the main loop, a new function "initialize()" (not shown)...

Serial.write('g');
Serial.write("g");
Serial.print('g');

Arrch:

bjornert: It now has nothing to do with my problem as the LR4 has been hooked up to the Arduino via the TX, RX, 5V and GND pins.

The problem is that your Serial monitor is using those same TX and RX pins, so anything you send to the Serial Monitor is also getting sent to the LR4.

OH...so would it work then if I load the code, then disconnect the USB power and hook it up to a separate power supply?

bjornert: OH...so would it work then if I load the code, then disconnect the USB power and hook it up to a separate power supply?

Maybe, but with the code you have now, you'll never know because there is no feedback mechanism. You're better off using something like SoftwareSerial and to communicate with the LR4. That way, you can still use the Serial monitor to debug.

Arrch: Maybe, but with the code you have now, you'll never know because there is no feedback mechanism. You're better off using something like SoftwareSerial and to communicate with the LR4. That way, you can still use the Serial monitor to debug.

Oh fantastic! I think I understand now...I'll give it a try!!

Arrch: Maybe, but with the code you have now, you'll never know because there is no feedback mechanism. You're better off using something like SoftwareSerial and to communicate with the LR4. That way, you can still use the Serial monitor to debug.

IT WORKED! Thank you kindly! XD

If you look at the ref section of the site you will see that Serial.read() does NOT return a byte it returns an int, -1 is used to indicate no data to return!

Mark

The data sheet for the LASER unit details the RS232 signal pins use 3.0v logic level. Are the voltage levels on your Arduino serial pins 3v RS232 logic level? Or 5v ?