LR4 Sample Software with Source Code

Download this package of sample software (with source) to see several applications that work with the LR4.  Full source code is included for all of these applications under a BSD style license.  See the license information at the top of each source file for more information.

You can use these applications as-is or feel free to modify them to build your own custom distance measurement application.  It's also easy to integrate the LR4 sample software into your larger Windows, Linux, or embedded application.

SampleSoftware3.PNG

Windows

LRSimple
The LRSimple application is a bare bones Win32 Console app that implements the simplest method to start the LR4 and read distance measurements.  If you intend to integrate the LR3 into a larger custom application, then this source code is the place to start.

LRDemo
The LRDemo application is a full featured Windows MFC application that demonstrates all of the features of the LR4.  It is somewhat more complex than the LRSimple app and probably should be used as a reference when needing to implement more advance features.

LRLog
A Win32 command prompt app that logs data from the rangefinder to a CSV file.  Each measurement is date and time stamped with millisecond precision.

LRMulti
Demostrates how to work with multiple LR3 devices attached to a single PC.  This app lists all attached LR4 devices along with their serial numbers, then data is collected from each rangefinder and displayed.

LRMultiLog
Opens all LR3/LR4 devices and logs data from each one in a separate CSV file along with a date/time stamp.


LINUX

Under Linux, the LR4 shows up as a raw HID device, and simple POSIX read/write functions can be used to configure and read from the device.

This is a C code sample for configuring and continuously reading measurements over the USB connection.

Compiles with:

gcc -std=gnu99 -o lr4 lr4-linux-example.c

Run with super user privileges:

sudo ./lr4-linux-example

This works both on x86 and the Raspberry Pi.


Embedded (Arduino)

This is untested code, but something like this should work:

char measureBuf[6]; // enough space for five digits plus a zero byte
int offset;
void setup()
{
    Serial.begin(9600);
    while (!Serial); // Wait untilSerial is ready (for Leonardo)
    Serial.print("g"); // tell the LR4 to go
    offset = 0;
}

void loop()
{
    if (Serial.available()) {
        char ch = Serial.read();

        if (ch == '\r') {
            measureBuf[offset] = '\0' // measureBuf now contains a string like "12345"
            ProcessMeasurement(measureBuf); // do something with measureBuf
            offset = 0;
        }

        if (ch == '\n')
            offset = 0; // nothing to do, set offset to zero just to be safe

        if (offset < 5)
            measureBuf[offset++] = ch;
    }
}