SteveMyres Posted June 6, 2023 Share Posted June 6, 2023 I'm looking at using a lidar unit, which among other modes, once started, will stream multiple measurement in packets, with range and azimuth. How do I listen on the serial port for those packets? I can send command strings from either the device or port monitor dialogs and in some cases get replies, but the streaming responses don't show up. Quote Link to comment Share on other sites More sharing options...
AzeoTech Posted June 6, 2023 Share Posted June 6, 2023 This is a lot like a GPS unit. Anyhow, basically you just do reads without any writes and let the loop automatically pause on the reads (so no Delay() except maybe inside a catch()). The trick is alignment, but usually those units have a CRLF at the end and some sort of string at the beginning that tells you what the line of data contains. Let's assume it does end with a CRLF and that it starts with "$GPRMC" (a GPS standard data packet). It'd go something like this: private string datain device.myDevice.purge() while(1) try datain = device.myDevice.ReadUntil(10) // note LF. If just a CR then change to 13 if (left(datain,6) == "$GPRMC") // process the datain string endif catch() ? strLastError // if you want delay(0.01) // just in case. Can remove or make smaller once tested endcatch endwhile That is pretty much it. You just need to add parsing script. If the string doesn't start with $GPRMC it is tossed and the next line is read. You don't need a delay() because the readUntil() will delay until it receives the full line. Quote Link to comment Share on other sites More sharing options...
SteveMyres Posted June 20, 2023 Author Share Posted June 20, 2023 Have a weird situation. Here's the current stage of implementing the latest sensor (Ethernet, industrial style). It's scanning at 15Hz / 900rpm, so I expect a data packet every 66-67ms. I have a 20ms delay, the loop processing seems to take about 6-7ms, so I would assume it would loop the while(), then wait at the ReadUntil() for another 40ms till the next complete packet is in the buffer, then rinse and repeat. When the systime() is active, it seems that the loop is executing every 20-25ms or so, as if the ReadUntil() doesn't have to wait when it gets there. I've confirmed with Wireshark that I'm only getting a transmission every 65-70ms. I'm assuming the ReadUntil() removes the bytes from the buffer when the test is met. If so, how can the loop iterate faster than received data transmissions?? while(1) RawResp = device.Lidar.ReadUntil(3) RespStrArr = parse(RawResp, -1,",") DataCount = NumRows(RespStrArr)-18 DataStrArr = Prefix[18,DataCount] + RespStrArr[18,DataCount] Data= StrTodouble(DataStrArr)*Sines[0,DataCount] Delay(0.02) //?Data //?systime() endwhile Quote Link to comment Share on other sites More sharing options...
AzeoTech Posted June 20, 2023 Share Posted June 20, 2023 My guess is that the buffer was pretty full when you started the loop and it is just working its way through old data. Try adding a purge() right before the while(): device.Lidar.purge() Quote Link to comment Share on other sites More sharing options...
AzeoTech Posted June 20, 2023 Share Posted June 20, 2023 You also should add some validation code. If Lidar is async, which I am sure it is by your code, then it is probable that the first time through you will get a partial packet. Quote Link to comment Share on other sites More sharing options...
SteveMyres Posted June 20, 2023 Author Share Posted June 20, 2023 Yeah, it doesn't start scanning till I tell it to earlier in the non-looping part of the sequence, and right before I start it, I purge(), so there "shouldn't" be anything queued up. Good thought about piled up packets though, hadn't considered that. And yeah, I'm still at the point of just getting it working to validate the device and feasibility for use with DF. If I deploy it, I'll do the code right. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.