04-27-2020 11:15 AM
Hi all,
Hopefully someone an help me. As part of a larger program, where a linear encoder reads a displacement and a few motors and electromagnetic brakes are being controlled from a LabVIEW VI, I was trying to print out the recorded dislacement and trial number to an OLED display. I figured the easiest way would be to build a string with these values and send it to an Arduino over the COM port using the VISA Write command.
I feel I am almost there - I had the OLED display working as desired when I send the string from the Arduino serial port (see attached images), however when I send the exact same string over the VISA write command from LabVIEW, I see that my Arduino recieves it (Rx LED blinks), but the display just flashes white and no text is displayed.
I have attached a VI that shows exactly what was trying to do in the larger VI. This smaller one is doing the exact same thing. For those interested, I have also attached my Arduino script, although I do warn that I am not far above novice level with it.
Would love some help with this, thank you
Couldn't attach the script (even as a text file?) so here it is for anyone interested:
#include <TFT.h>
#include <SPI.h>
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// pin definition for Arduino UNO
#define cs 10
#define dc 9
#define rst 8
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
void setup() {
//initialize the library
TFTscreen.begin();
// clear the screen with a black background
TFTscreen.background(0, 0, 0);
//set the text size
TFTscreen.setTextSize(2);
TFTscreen.stroke(255, 0, 0);
Serial.begin(115200);
}
void loop() {
if (Serial.available())
{
String trialNo;
String strDisplacement;
while (Serial.available() > 0) {
trialNo = Serial.readStringUntil(','); // writes in the string all the inputs till a comma
Serial.read();
strDisplacement = Serial.readStringUntil(','); // writes in the string all the inputs till a comma
Serial.read();
}
TFTscreen.background(255,255,255);
String strDisplay = "Trial: " + trialNo + "\n" + "\n" + "Displacement:" + "\n" + "\n" + " " + strDisplacement;
int len = strDisplay.length();
char txt[len];
strDisplay.toCharArray(txt,len);
TFTscreen.text(txt, 22, 20);
delay(2000);
txt[0] = 0;
}
delay(200);
}
04-28-2020 03:01 AM
Hello,
For me the blinking of the Rx LED is not enough to tell if the command is correct. Can you print what you receive on the Arduino IDE to confirm this is the right command ? If it is, then it is more an Arduino question than a LabVIEW question and I suggest you post on https://forum.arduino.cc/
I worked with Arduino before but I am not an expert (at all). When I used a LED panel I had to loop very quickly and continuously send commands to avoid blinks of the LEDs. But I never used the display you use, I don't know if that's applicable.
Yddet
04-28-2020 04:12 AM
Thank you for the reply.
I am not sure how to check what the Arduino is receiving, as if I open the serial port it blocks the COM port so LabVIEW cannot write to it, or if I am running the VI I attached, I am unable to open the serial monitor in the Arduino IDE. Any ideas on how I can do this?
If I send the string "64, 1.23 mm" through the Serial Monitor, the OLED displays the information as desired, however if I send the exact same string over the VISA Write command (same baud rate and COM port), the OLED display doesn't display anything. It just flashes white when I initially run the program.
04-28-2020 02:26 PM - edited 04-28-2020 02:35 PM
@jcarr00 wrote:
Thank you for the reply.
I am not sure how to check what the Arduino is receiving, as if I open the serial port it blocks the COM port so LabVIEW cannot write to it, or if I am running the VI I attached, I am unable to open the serial monitor in the Arduino IDE. Any ideas on how I can do this?
If I send the string "64, 1.23 mm" through the Serial Monitor, the OLED displays the information as desired, however if I send the exact same string over the VISA Write command (same baud rate and COM port), the OLED display doesn't display anything. It just flashes white when I initially run the program.
First, your Arduino code is configuring its serial port for 115,200 bits/s, but your LabVIEW code by default is opening its port at 9600 bps. Second, if you send the string "64, 1.23 mm" doesn't the C code just hang at the second Serial.readStringUntil(',') waiting for a second comma that never arrives? Actually, it looks like the default timeout is one second, so it'll wait that long for the comma before proceeding.
Third, assuming you actually are putting a second comma at the end of your string, are you certain you aren't sending even one extra byte (such as a carriage return or linefeed) at the end of your string, after the second comma? Even a single stray byte will cause the while loop to repeat again, looking for another complete transmission including two commas, and until it's received the display will not be updated.
Another thing I noticed is that the readStringUntil(',') apparently discards the comma once it reaches it, so after the first part of the string is read and the comma discarded, your first Serial.read() is going to eat the first byte of the second string before the second readStringUntil gets to it.
04-29-2020 03:27 AM
Hi, thank you for the reply
I have tried baud rates of 115,200 bits/s and 9600 bits/s (you can see I have a numeric control on the Configure Serial Port block in the VI I originally attached), but I see the same results - works from Arduino Serial Monitor but not from LabVIEW.
You can see the "5, 1.23 mm" string has a white space after it so that the Serial.read() just eats that byte and doesn't affect the displacement data. And yes, you are correct about sending a second comma - it just stops the timeout from that Serial.readStringUntil(',') being triggered. Again, I have tested "5, 1.23 mm ," successfully in the Arduino's serial monitor - it just gets displayed a second faster on the OLED display, but if I send the exact same string from LabVIEW, all I see is a white OLED display.
I would like to know more about your thoughts on the extra byte(s) that might be sent from LabVIEW, such as a carriage return or a linefeed. Is there a more robust way to capture the data after the first comma so that a possible stray byte will not affect the while loop? Is there a way of testing linefeed/ carriage return bytes from the serial monitor? I tried sending "5, 1.23 mm\n" and "5, 1.23 mm\r" but the OLED display just displays the "\n" and "\r" characters beside the "mm" and if I send "5, 1.23 mm,\n" the OLED display shows "Trial: n Displacement:"
I am surprised that I don't at least see "Trial: Displacement:" on the OLED display, as that should be triggered if the Arduino picks up data at the COM port
Thank you for the help
04-29-2020 01:42 PM
@jcarr00 wrote:
I would like to know more about your thoughts on the extra byte(s) that might be sent from LabVIEW, such as a carriage return or a linefeed. Is there a more robust way to capture the data after the first comma so that a possible stray byte will not affect the while loop? Is there a way of testing linefeed/ carriage return bytes from the serial monitor? I tried sending "5, 1.23 mm\n" and "5, 1.23 mm\r" but the OLED display just displays the "\n" and "\r" characters beside the "mm" and if I send "5, 1.23 mm,\n" the OLED display shows "Trial: n Displacement:"
In LabVIEW he "\n" and "\r" constants only get interpreted as line feed and carriage return characters if you have the string control set for '\' codes display; otherwise they're just interpreted literally. I'd suggest enabling '\' codes display for your string control, as well as limit to single line, so you can definitively see what you're sending. If you're never sending the second comma and instead relying on the timeout to end the readStringUntil call, then you shouldn't have an issue with a stray LF or CR at the end of the string, because it'll be read as part of the strDisplacement read. Can you try simplifying your Arduino code further, to have it just read a single string, and then try sending just one string from LabVIEW?