I’ve been having tons of trouble getting the cRio to communicate to my arduino board through serial. My goal is to send three comma separated values in an ASCII string. Then the arduino will wait for a new line (ASCII decimal 10) and it will use the parseint function to find the three values. I know that the program on the arduino is working because i can use a basic stamp 2 to generate an ASCII string of 1,2,3 then a new line/line feed and the arduino will send me a confirmation message and read back the values on another serial port controlled by the software serial library. I am using a max232 ic to change the rs-232 voltage to ttl. I can see on my oscilloscope that the max232 is doing the right thing when it is connected to the crio and that the serial connection idles in the high state. Whenever I try to send data from the crio to the arduino the data sent cannot be read as an ASCII string of comma separated values. I am using the serial vi’s in labview. My labview code uses a vi (I forget what it’s called-it looks like two cylinders with a plate in between), which converts decimals into ASCII characters. I can probe the data and see that the output of the string concatenate vi has a 1,2,3 like I wanted. I originally thought that labview was properly sending the the numbers but not the new line ASCII code, but the problem shows up on the oscilloscope. The working output from the basic stamp is about ten times shorter than the output from labview. Does anybody know a way to get the crio serial port to output ASCII characters or another easy way to have the arduino and crio communicate?
Maybe plug your computer into the serial port of the crio. Use hyperterminal or something similar to be sure the crio is sending what you think & the com parameters are right. You could to the same to the arduino.
It sounds like you may be casting the numbers to string when you intended to format them to a string.
Look in the string palette for the format to string or to decimal string.
Greg McKaskle
Thank for the quick replies! I haven’t had time to try them out, but I think that the format into string vi is what I want. It seems that it will let me format he inputs and add a line feed at the end. It also seems to be able to stop adding spaces between strings if I input a format string such as %s%s%s. I would connect the crio to the computer to see if it was working, but I do not have the proper serial cable to make this happen. The only cable I have with me at home is one with a USB to serial converter on one end, and the other connector has been cut off and the wires have been broken off to program my basic stamp. For now, i am just looking at the output on an oscilloscope although it is not quite triggering properly on the signal.
I’ve just tried it with the format string vi and it works perfectly! The serial connection is great because it can easily update 12 ascii characters several times per second operating at a pretty slow 9600 baud.
Few months old but our team is now trying to do the same thing. Would you mind taking screen shots of your labview code and copy/pasting your arduino code so we can compare and see where things have gone wrong? Thanks!
For the arduino code, we switched to using a demo project that worked a little better than what we wrote.
/*
Reading a serial ASCII-encoded string.
This sketch demonstrates the Serial parseInt() function.
It looks for an ASCII string of comma-separated values.
It parses them into ints, and uses those to fade an RGB LED.
Circuit: Common-anode RGB LED wired like so:
* Red cathode: digital pin 3
* Green cathode: digital pin 5
* blue cathode: digital pin 6
* anode: +5V
created 13 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int red = Serial.parseInt();
// do it again:
int green = Serial.parseInt();
// do it again:
int blue = Serial.parseInt();
// look for the newline. That's the end of your
// sentence:
if (Serial.read() == '
') {
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
red = 255 - constrain(red, 0, 255);
green = 255 - constrain(green, 0, 255);
blue = 255 - constrain(blue, 0, 255);
// fade the red, green, and blue legs of the LED:
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
// print the three numbers in one string as hexadecimal:
Serial.print(red, HEX);
Serial.print(green, HEX);
Serial.println(blue, HEX);
}
}
}
As for the labview, I can’t get to that right now, it was just a while loop with a 100ms wait in it. We opened a serial port with all the default settings outside the loop, then in the loop we used the serial write vi. For the buffer input, we used format into string vi. You can drag the vi so it gets bigger and you can have three inputs. These are your red/green/blue values. Leave the initial string blank and for the format string use
%g,%g,%g
Make sure that you put the commas and that the slash goes the right way. Then you can connect the resulting string into the buffer input of the serial write. I would recommend starting by wiring constants of 127 into the inputs of the format into string so that it doesn’t matter if 0 or 255 is on or off. Also, we had some issues with having both the arduino’s usb connection and the serial connection running at the same time, so if it doesn’t work you may also have to disconnect the arduino from the computer. Also, I don’t know how much you’ve already done, but it helps to make sure that your arduino board can actually control the lights. We used TIP-120 transistors with ours and it worked well.
I’ll be at the computer with the LV code on friday, so I can post that.