Android Question Bosch Distance Laser GLM Serie

Wolli013

Well-Known Member
Licensed User
Longtime User
Does anyone here use the Bosch GLM50C or similar?
I would like to make a small app where only the measurement data is transmitted and displayed via BLE.
Does anyone have something like this running?
I have only found the Python code so far.

 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Wolli013

Well-Known Member
Licensed User
Longtime User
a/ add a Log() message to the button click events, to make sure they're both being called by their respective buttons
b/ show us the code of the two button click events
B4X:
Private Sub Button2_Click
Dim BacklightOff() As Byte = Array As Byte(0xC0, 0x48, 0x00, 0x62)
B4XPages.MainPage.SendCommand(BacklightOff)
End Sub

Private Sub Button1_Click

        'measure':          b'\xC0\x40\x00\xEE',
        'laser_on':         b'\xC0\x41\x00\x96',
        'laser_off':        b'\xC0\x42\x00\x1E',
        'backlight_on':     b'\xC0\x47\x00\x20',
        'backlight_off':    b'\xC0\x48\x00\x62'   
    
    
'Dim BacklightOn()  As Byte = Array As Byte(0xC0, 0x47, 0x00, 0x20)

'Dim BacklightOn()  As Byte = Array As Byte(0xC0, 0x40, 0x00, 0xEE)'measure
'Dim BacklightOn()  As Byte = Array As Byte(0xC0, 0x41, 0x00, 0x96)'laser_on
Dim BacklightOn()  As Byte = Array As Byte(0xC0, 0x42, 0x00, 0x1E)'laser_off
    
B4XPages.MainPage.SendCommand(BacklightOn)
End Sub
 
Upvote 0

Wolli013

Well-Known Member
Licensed User
Longtime User
B4X:
Public Sub SendCommand (cmd() As Byte)
    AStream.Write(cmd)

    Dim bc As ByteConverter
    Log(bc.HexFromBytes(cmd))   
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim BacklightOn()  As Byte = Array As Byte(0xC0, 0x42, 0x00, 0x1E)'laser_off
  
B4XPages.MainPage.SendCommand(BacklightOn)

If the command works you just set it to LASER OFF?

Does the Laser goes OFF when you click the Button?

I would not expect to get any measures after shutting down the device. ;)

Effectively i would try to use the commands for
- Laser ON
Does the Laser goes ON?

- Measure
Does the sendcommand send out the right command?
Does AStream_NewData raise with data now?
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
No command works at all.
Nothing is triggered either.
Let's back up a little, first, do you see the Bluetooth activated icon on your display on the device?

If so, then we have a connection to your android device.
Next step would be to make sure we are using the correct data format.

Start by sending the following command
B4X:
Dim backlight_on() as byte = array as byte(0xC0, 0x47, 0x00, 0x20)
This should turn on the back light on your device's display

If this works then, you can proceed to send the measure command which is
B4X:
dim measure() as byte = array as byte(0xC0, 0x40, 0x00, 0xEE)

Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
No command works at all.
Nothing is triggered either.
Also if you are using the Bluetooth Chat example, try changing the following

B4X:
Sub AfterSuccessfulConnection
    If AStream.IsInitialized Then AStream.Close
    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
    AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")
    ConnectionState = True
    B4XPages.ShowPage("Chat Page")
End Sub

Sub AfterSuccessfulConnection
    If AStream.IsInitialized Then AStream.Close
    AStream.Initialize(serial.InputStream, serial.OutputStream, "astream")
    ConnectionState = True
    B4XPages.ShowPage("Chat Page")
End Sub

Change the Astream.InitializePrefix to Astream.Initalize, that should work.
 
Upvote 0

Wolli013

Well-Known Member
Licensed User
Longtime User
Also if you are using the Bluetooth Chat example, try changing the following

B4X:
Sub AfterSuccessfulConnection
    If AStream.IsInitialized Then AStream.Close
    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
    AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astream")
    ConnectionState = True
    B4XPages.ShowPage("Chat Page")
End Sub

Sub AfterSuccessfulConnection
    If AStream.IsInitialized Then AStream.Close
    AStream.Initialize(serial.InputStream, serial.OutputStream, "astream")
    ConnectionState = True
    B4XPages.ShowPage("Chat Page")
End Sub

Change the Astream.InitializePrefix to Astream.Initalize, that should work.
Now something is arriving at NewData.
AStream_NewData = 000082
It also works display on and off.
 
Upvote 0

Wolli013

Well-Known Member
Licensed User
Longtime User
This is the measurement data that is received
AStream_NewData = C055100608CF06211F3C3F0000000000000000000074
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
This is the measurement data that is received
AStream_NewData = C055100608CF06211F3C3F0000000000000000000074
Take a look at the python code here that takes the received data and converts it to distance measurements.

Python:
    def measure(self):
        self.socket.send(self.cmds['measure'])
        data = self.socket.recv(1024)
        #print('received:', data)

        if self.status[data[0]] is 'ok':
            try:
                # distance to object from top of device
                distance = int(struct.unpack("<L", data[2:6])[0])*0.05
                #print(distance, 'mm')
                return distance
            except:
                #print('corrupt data received, try again')
                return -1
        else:
            return -1

Looks like it would be bytes 2 to 6, and multiply * 0.05 to convert to mm.

Walter
 
Last edited:
Upvote 0

Wolli013

Well-Known Member
Licensed User
Longtime User
I've tried this now, but the results don't fit in the front and back. I'm doing something wrong.


B4X:
Private Sub AStream_NewData (Buffer() As Byte)
    Dim bc As ByteConverter
    Dim distance As Int
    distance = bc.IntsFromBytes(Array As Byte(Buffer(2), Buffer(3), Buffer(4), Buffer(5)))(0) * 0.05
    Log("Distance: " & distance)
End Sub
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I've tried this now, but the results don't fit in the front and back. I'm doing something wrong.


B4X:
Private Sub AStream_NewData (Buffer() As Byte)
    Dim bc As ByteConverter
    Dim distance As Int
    distance = bc.IntsFromBytes(Array As Byte(Buffer(2), Buffer(3), Buffer(4), Buffer(5)))(0) * 0.05
    Log("Distance: " & distance)
End Sub
have you seen this here
B4X:
Example measure:
send C04000EE ->reply 00 04 13 0E 00 00 32
Change endianness
distance in mm=0x00000E13*0,05=180mm

EDIT:
Example measure: send C04000EE --> reply 00 04 13 0E 00 00 32
Change endianness: 13 0E 00 00 --> 00 00 0E 13 distance in mm: 0x00000E13*0,05 = 180mm

It's always a good idea to utilize the nRF App to verify received data and compare it with your B4X App
and post your logs from you B4X App
 
Last edited:
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I've tried this now, but the results don't fit in the front and back. I'm doing something wrong.


B4X:
Private Sub AStream_NewData (Buffer() As Byte)
    Dim bc As ByteConverter
    Dim distance As Int
    distance = bc.IntsFromBytes(Array As Byte(Buffer(2), Buffer(3), Buffer(4), Buffer(5)))(0) * 0.05
    Log("Distance: " & distance)
End Sub
Can you post the hex data you are receiving, and can you run a quick test, can you point the laser at an object with a known distance, let's say 5 meters, then send the measure command and post the returned hex data.

Walter
 
Upvote 0

emexes

Expert
Licensed User
distance = bc.IntsFromBytes(Array As Byte(Buffer(2), Buffer(3), Buffer(4), Buffer(5)))(0) * 0.05

Sometimes I just plug bits'n'bytes together manually rather than rely on the current endiness of ByteConverter:

B4X:
Dim RawInt As Int = Bit.ShiftLeft(Bit.And(Buffer(5), 0xFF), 24) + _
                    Bit.ShiftLeft(Bit.And(Buffer(4), 0xFF), 16) + _
                    Bit.ShiftLeft(Bit.And(Buffer(3), 0xFF),  8) + _
                    Bit.ShiftLeft(Bit.And(Buffer(2), 0xFF),  0)

Dim distance As Float = RawInt * 0.05
 
Last edited:
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Sometimes I just plug bits'n'bytes together manually rather than rely on the current endiness of ByteConverter:

B4X:
distance = bc.IntsFromBytes(Array As Byte(Buffer(2), Buffer(3), Buffer(4), Buffer(5)))(0) * 0.05

Dim RawInt As Int = Bit.ShiftLeft(Bit.And(Buffer(5), 0xFF), 24) + _
                    Bit.ShiftLeft(Bit.And(Buffer(4), 0xFF), 16) + _
                    Bit.ShiftLeft(Bit.And(Buffer(3), 0xFF),  8) + _
                    Bit.ShiftLeft(Bit.And(Buffer(2), 0xFF),  0)

Dim distance As Float = RawInt * 0.05
Or we can do it this way too!
B4X:
    Dim s As String = "0004130E000032"
    Dim data() As Byte = bc.HexToBytes(s)
    Dim raw(4) As Byte = Array As Byte(data(2), data(3), data(4), data(5))
    bc.LittleEndian = True
    Dim dist() As Short = bc.ShortsFromBytes(raw)
   
    Dim distance As Float = dist(0) * 0.05
    Log("distance in mm: " & distance)

distance in mm: 180.14999389648438
 
Upvote 0
Top