08-11-2020 01:06 AM
Hi everyone I'm new to NI in terms of the programming side. I am trying to communicate with the Arduino through the USB port using UART protocol. The command is a simple one to five char numeric string, and the Arduino would return a long string, more or less in random interval updating the Arduino's current status. I have tested the communication between the computer and Arduino with NI Max and they work fine although the \s\r\n were also shown. I made a simple vi that supposed to replicate the serial port test in NI Max. When I run this vi, if I hit the button the Arduino would initialize and return the first line of the status string but that's as far as it would go. Every time I hit the button it would start the initialization process again but would not go further. I know there is something missing in my code but I couldn't work out what it is.
I attached a picture of the code blocks (can't upload pictures for some reason). I hope someone could point me in the right direction or suggest a useful tutorial. Many thanks!
Solved! Go to Solution.
08-11-2020 02:15 AM
You should check an example provided by help. Just in case i attached my own example.
First thing you should avoid is opening and closing the communication in every loop, instead Configure it at the start, then do the write/read inside while loop and when you are done exit while loop and Close the connection.
If you are only writing commands occasionally, i'd also suggest that you don't do it every loop with the same command if you don't need to.
There should always be a little bit of delay between each read/write. The way you configured it, Labview tries to read/write as fast as possible, several thousand times a second.
If the Arduino always returns \s\r\n at the end of it's data, the \n is most likely a term char and you can set it as such. This way you won't get garbled data if you don't read the correct amount of bytes (in my example i set it as 256 with term char enabled for carriage return; you tried to statically read 30 bytes each read, which most likely resulted in empty buffer reads or even visa errors because reading an empty buffer is a no no).
Hope this helps 🙂
08-11-2020 06:56 AM
@AeroSoul wrote:
There should always be a little bit of delay between each read/write. The way you configured it, Labview tries to read/write as fast as possible, several thousand times a second.
Use an Event Structure with the Boolean's Value Change event case instead of a Case Structure. Then you only send when the button is pressed with no processing happening otherwise.
@AeroSoul wrote:
you tried to statically read 30 bytes each read, which most likely resulted in empty buffer reads or even visa errors because reading an empty buffer is a no no).
Depends on the protocol. If the Arduino always replies with something, this setup is just fine. And 30 is typically enough to capture a full message. I tend to use 50. For some instruments with longer replies, I increase it to 100. Rarely do I need more than that.
08-12-2020 06:57 AM
Thanks a lot! Your example is a very helpful starter. I notice you have a read buffer display on the front panel but I can't find the visa read block or the indicator block anywhere in your diagram. Is that some kind of trick? Also the read buffer refreshes so fast the string from the Arduino can only be seen for a fraction of a second, is there a way to read the buffer only when there is a string available to read? Or hold the display until it is over written by a new string?
Again a million thanks for your help.
08-12-2020 06:59 AM
Thanks for the suggestion I'll try it once I got more confident programming with LabVIEW.
08-14-2020 07:28 AM
Sorry for late reply.
If you double click on a front panel element (such as read buffer) it'll take you to block diagram where that element is used.
I put it in case statement when "Write" command is false, so it read all the time except when writing.
If you want to have results displayed longer perhaps just put "Bytes at port" function before read buffer, so it only reads when there's something at port, and use shift register. (see example)