|
Forum Index : Microcontroller and PC projects : Need help with picoMite & LIDAR
| Author | Message | ||||
| Amnesie Guru Joined: 30/06/2020 Location: GermanyPosts: 720 |
Hello again, I am struggeling with interfacing a LIDAR (TF Luna) I just bought. This sensor calculates the "time of flight" and is way more preceise than the ultrasonic thingy. The problem is I am unable to do what I want in BASIC and the manual does not help in Appendix A to interface the LIDAR sensor via Serial COM. I just want to read Serial data comming in and decode it. Normally it should be pretty easy but for example I don't know how to count the number of incomming bytes and reset the buffer etc. Here is a python script that does all the work, but I am not really able to translate this into MMBASIC: ser = serial.Serial("/dev/serial0", 115200,timeout=0) # mini UART serial device # ############################ # read ToF data from TF-Luna ############################ # def read_tfluna_data(): while True: counter = ser.in_waiting # count the number of bytes of the serial port if counter > 8: bytes_serial = ser.read(9) # read 9 bytes ser.reset_input_buffer() # reset buffer if bytes_serial[0] == 0x59 and bytes_serial[1] == 0x59: # check first two bytes distance = bytes_serial[2] + bytes_serial[3]*256 # distance in next two bytes strength = bytes_serial[4] + bytes_serial[5]*256 # signal strength in next two bytes temperature = bytes_serial[6] + bytes_serial[7]*256 # temp in next two bytes temperature = (temperature/8.0) - 256.0 # temp scaling and offset return distance/100.0,strength,temperature if ser.isOpen() == False: ser.open() # open serial port if not open distance,strength,temperature = read_tfluna_data() # read values print('Distance: {0:2.2f} m, Strength: {1:2.0f} / 65535 (16-bit), Chip Temperature: {2:2.1f} C'.\ format(distance,strength,temperature)) # print sample data ser.close() # close serial port Description what this does: In the above script, the serial port is being accessed for serial0 at a baudrate of 115200. The serial port is first opened before reading or writing any commands [ser.Open()]. Then in the test script, 9-bytes are read and the first two bytes are checked for the correct data format (0x59 and 0x59 are cited as the data return in the product manual). Finally, the data is printed out as distance, signal strength, and chip temperature. And this is how far I got: CLS Option Explicit MODE 2 SETPIN GP5, GP4, COM2 ' assign the I/O pins for the first serial port OPEN "COM2:115200" AS #5 ' open the 2nd serial port w/ speed of 115200 bd Do dat$ = INPUT$(9, #5) ' get up to 9 characters from the serial port PRINT dat$ Loop I am getting a lot of Characters, so I am receiving, ok this is obvious, because I must translate the string into byte but ... Hm... Maybe anyone could help me a bit with this, since it should not be that complicated... only for me ... I am NOT expecting someone to translate all this python script into MMBASIC, but maybe one could help me to just get this counting of 9 bytes correct and display it, then resetting it. The decoding work (hopefully) I can do myself :) Greetings Daniel Edited 2022-05-19 00:22 by Amnesie |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4147 |
The terminology "reset the ..." is bizarre. It seems to mean remove the data from the buffer, which is what happens by default. To get exactly 9 bytes (characters, if you prefer the term) something like dat$="" do while len(dat$) < 9 dat$ = dat$ + input$(1, #5) loop You could check along the lines of if left$(dat$, 2) = chr$(&h59)+chr$(&h59) then ... John [sorry about the hurried post] |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3481 |
If you have the baud rate set right, the LOC function will tell you how many bytes are in the serial input buffer. You can pick them up in chunks with the INPUT$ function, or if the input is terminated with CR/LF, you can pick them up line by line with LINE INPUT. How about providing a sample of what you are receiving? Serial input is something which MMBasic makes very easy--all the buffering is done for you and you can check the status of the buffer at any time--at your convenience. ~ Edited 2022-05-19 00:36 by lizby PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
| Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 1001 |
Hi Daniel, I have read a barcode scanner with this code snippet: Open "COM1:" As #1 Zaehler=0 Do If Loc(#1)>=15 Then 'Puffer auslesen wenn 15 Zeichen im Puffer dat$ = Input$(15,#1) 'Lese 15 Zeichen aus Puffer dat$ = Mid$(dat$,2,14) Print dat$ 'gelesenen Barcode zu Terminal senden LCD 2,1, " " LCD 2,1,datalt$ 'Zeige letzten Barcode LCD 1,1, " " LCD 1,1,dat$ 'Zeige akt. Barcode datalt$ = dat$ Exit Do Else Zaehler = Zaehler + 1 'Schleifenzaehler erhoehen Pause 1 End If Loop Until Zaehler >= 1500 'Timerueberlauf oder Barcode gelesen Close #1 'Puffer loeschen" Pause 50 Open "COM1:" As #1 I had problems with residues in the buffer - hence the closing and reopening of the interface... Frank |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |