a/ add a Log() message to the button click events, to make sure they're both being called by their respective buttonsI have now made 2 buttons, but cannot switch the light on or off.
Bosch GLM50 Cwhat Device do you own exactly?
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
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
Dim BacklightOn() As Byte = Array As Byte(0xC0, 0x42, 0x00, 0x1E)'laser_off
B4XPages.MainPage.SendCommand(BacklightOn)
Let's back up a little, first, do you see the Bluetooth activated icon on your display on the device?No command works at all.
Nothing is triggered either.
Dim backlight_on() as byte = array as byte(0xC0, 0x47, 0x00, 0x20)
dim measure() as byte = array as byte(0xC0, 0x40, 0x00, 0xEE)
Also if you are using the Bluetooth Chat example, try changing the followingNo command works at all.
Nothing is triggered either.
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
Now something is arriving at NewData.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.
Take a look at the python code here that takes the received data and converts it to distance measurements.This is the measurement data that is received
AStream_NewData = C055100608CF06211F3C3F0000000000000000000074
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
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 hereI'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
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
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.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
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!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
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
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?