OK I see what you mean now–you are measuring the speed in setup in a non-functional loop. I believe that the serial data buffer is larger on Xenon’s as compared to Photon/Electron so that could have some impact, but I’m not sure why you are getting the results you are seeing. Maybe you are processing more bytes in your readLaser while loop than you think you are. Can you count the total bytes read in both cases and report that too?
By the way, I would not do the integer divide you are doing above to get hertz–why not work in floats or doubles once you have the end-start time as you do below?
Nothing in your code prevents the readLaser function from reading more than one set of measurements since you have that while loop there. You could be reading two or more serial port measurements for every value returned through the global and you would never know. If you count bytes, you will know the bandwidth for sure.
@Jimmie, I would like to emulate this sensor on a Photon and have the Xenon “read” the data. Which sensor are you using? What is the data from the sensor look like? If I understand correctly the sensor simply pushes its data at 200Hz as a steady stream. The first thing done in readLaser is to purge the receive buffer. Then the software waits for the buffer to contain 3 bytes, reads them, calculates values and returns. So the code ignores any buffering essentially. What “typical” data do byte0 to byte 2 contain? Does the sensor have a “start” byte to indicate a new value?
Thank you. The reason for my puzzlement is because I am using the same sensor with a Photon. That Photon reads 150 readings from the sensor per second. The same reading routine is used but within a much larger application. I could not test the simple code with the Photon I have because they are in sealed enclosures and it would be a big hassle to get to.
The sensor is a laser distance sensor. Its frequency is configurable and can be much higher than 200 Hz but that I was I am using. The sensor is this:
The distance is output as 2 bytes. You noted correctly the shortcoming of the sensor I have, namely that the stream has no start string, hence reading the byte millis() times to figure out he first byte. The sensor company has addressed this problem in their new sensors.
Hello @rickkas7. Thank you for taking the time to do the test. I am not sure what the problem is but I am considering taking one of my Photons out of its enclosure and testing it since I know that with a Photon, I do get much faster readings.
I think what the discussion here is telling you is that your code is not really doing what you expect. The fact that it works OK for you on Photon is pretty irreverent. You seem to be trying to use the microsecond timer and the number of bytes buffered to tell where you are in the stream, but I don’t see that as being desirable or robust.
@Jimmie, it seems the sensor uses a “sychronization” output to indicate when a measurement is made and thus can be used to synchronize the serial data. Have you considered using the analog output of the sensor which could be sampled at a much higher rate than the serial can? In either case, have you considered using the synchronization output?