Obtain buffer fast from arduino using labview

2019-08-09 02:36发布

问题:

I'm trying to read sensor's value obtained from arduino and displaying it on labview using a pulse sensor and a DS18B20 (temperature sensor) however I need the while loop to go fast (around 50 ms) in order to get the waveform chart from the heart pulses but if it goes slower than a minute the values get mixed up (I get temperature instead of the analog sensor value and viceversa) any suggestions to fix this? I obtain the values in a serial.print separated by a comma, so I used a match pattern on LV to separate them

回答1:

If you check how many bytes are waiting at the port and only read that number of bytes, you are likely to get incomplete messages. It's hard to say exactly how this is going wrong without seeing an example of the data and the line(s) of Arduino code that send it, but in general to get this sort of data exchange to work well I would suggest you:

  • Make sure the Arduino code sends a termination character e.g. a line feed (0x0A) after each pair of values
  • When you configure the serial port in LabVIEW, set and enable this as the termination character for reads - which should be the default anyway. I would then clear the read buffer before entering the loop to remove any old data that has accumulated there while the LabVIEW code wasn't running.
  • In the loop, don't check Bytes at Port, but wire a large value to bytes to read of the Serial Read function. The Serial Read will read and return bytes from the serial buffer up to and including the termination character, waiting for them to arrive if necessary (up to the timeout setting configured for the port). That way you should always receive a complete string.

To get data out of the received string I would use the Scan from String function. For your example it looks as if the appropriate scan string would be something like %f,%f\n which specifies two floating-point values separated by a comma and terminated with a newline. Scan from String has an error output and also allows you to wire in defaults for each value you are trying to read, so you can detect if the data hasn't been received correctly.

The best way to show LabVIEW code here, rather than taking a screenshot, is to select it on the diagram and choose Edit > Create VI Snippet from Selection. This saves a PNG image that also has the actual LabVIEW code embedded in it. Also please consider arranging your block diagram so the data flow is from left to right as far as possible as this convention makes it much easier to read.