04-15-2010 04:14 PM
Hi,
I have a inclinometer connected to my COM1 serial port. The inclinometer outputs a string of "R -0.572" or "R 1.458" for example to signify roll angles, at a configurable pace. I can successfully read this string output in a scrolling window on labview, and I can use scan string to turn it into a number, but I cannot write it to a file properly. It simply records the first value and then keeps writing it. Also, the scan string function only works once on the first value, and then doesn't seem to update at all after that. My numeric indicator is stuck on the first roll angle I receive. I would like to be able to build a file of the inclinometer output vs. time in excel.
Furthermore, since the serial output is not constant, say once every 10ms or so, will labview build a output file with only those values? Currently my output file takes the first value and then write it about 1000 times at very small time increments when in reality only 4 or 5 serial outputs have been received.
I have attached the VI that I am using. Ive tried putting the wr ite block both inside and outside of the loop but I cannot get it to work.
Thank you,
Steve
04-15-2010 04:44 PM
04-19-2010 06:34 PM
Thank you for the help but I have one more question.
I can successfully extract the numerical value and write it to a spreadsheet now. However, the output data, whether I include a time column or not, has many 0 readings in it where I assume labview tries to take a reading but there is no output present from the inclinometer. I changed the value of the internal clock in the VI to 1ms to capture all the data output from the inclinometer because it outputs very fast.
How do I make labview only write to the spreadsheet when a value is outputted from the inclinometer? I have attached my VI and a spreadsheet of data taken. Notice how sometimes the same time reading has a value of a inclination and also zero. How can I fix this?
Thank you,
Steven P
04-19-2010 08:52 PM
I'd highly recommend you eliminate those ancient serial port functions and replace them with the VISA read and write functions that are currently in the palettes. They will also provide you error wires you can monitor for errors and will also let you set the execution order for you write then read.
Why did you make your loop iterate at 1 msec? That is going to mess you up. You won't have enough time to get an entire messge in. if you only get a portion of a message such as the "R" or a space, it will be greater than 0 bytes, your evaluation and writing code will execute, but it will evaluate as a zero because it has no real data.
Do your incoming messages end in a termination character such as a carriage return or linefeed? If so, then use that to terminate your reads. Enable the termination character and read a sufficiently large number of bytes so that you get your entire message. When the termination character comes in, the read will end and you will have an entire message which you can then evaluate and write to the file.
04-20-2010 09:25 AM
Thanks for the help,
I tried to used the basic serial read and write example to start off from to replace the old VI I had setup but can't quite get it working. With the new VI attached below, when I try to take readings I usually get an error:
"Error -1073807252 occurred at VISA Read in Basic Serial Write and Read Try 2.vi
VISA: (Hex 0xBFFF006C) An overrun error occurred during transfer. A character was not read from the hardware before the next character arrived."
and after switching from true to false on enable termination character a few times I get this error:
"Error 85 occurred at Scan From String (arg 2) in Basic Serial Write and Read Try 2.vi
LabVIEW: Scan failed. The input string does not contain data in the expected format."
regardless of the true/false position. When it is in the false position I get many readings but when it is in the true position I seem to get only the last reading displayed. Also, the data file I am writing only has one value in it when I open it, is the scan string and write functions placed properly? Should I be using a different termination character? In hyperterminal, my output for the inclinometer is as follows:
I type "go" and press <enter>
R -0.607
R -0.606
R -0.607
R -0.606
R -0.605
R -0.605
And I can type "h" (no enter needed) to stop it. "go" and <enter> will resume. In my old vi, go\r\n worked for this. Im not sure if there is a termination character outputted by the inclinometer, in hyperterminal would the fact that the readings each go onto a line under it signify a carriage return output character?
It also seems in the new vi whenever I hit the execute VI once button, it spits out the accumulation of the strings that has been incoming since its last execution if I press it at the right moment as not to start in the middle of a reading. Is this VI meant to be run once or continuously? I am not sure what I am doing wrong.
Thanks,
Steven P
04-20-2010 10:22 AM
That VI is meant to be run once. Do not use the Run Continuously button.
Put the Write and Read functions in a while loop. Configure the port before the loop, close the port after the loop.
Do not use the bytes at port method. Just wire a sufficiently large number to your Read function. It looks like all of your data coming in consists of 10 bytes, so wire a 10.
You can eliminate the wait between the write and the read. It is only there to give sufficient time for the whole message to come in. Without it, and using the bytes at porth method, you might only get a few of your 10 bytes. Honestly, I feel the serial port examples that NI has are not the best. In theory, they work, but they are often not the best method for communicating with real instruments. Take a look at the advanced serial write and read as well. I think that one implements a few more features. For some reason, my example finder won't let me open it write now to look at it.
If you are getting an incomplete reading on the first read after starting, you may want to just throw out the first reading and not write it to a file. If all of your responses consist of 10 bytes, you may want to look at the length of the just received message and only write it if it is 10 bytes long.
04-20-2010 10:46 AM - edited 04-20-2010 10:50 AM
Evidently your inclinometer is constantly putting data on the serial bus. So when you run your vi, there is some data that might be lost if the input buffers are full. This is why you get overrun error. Also, your current vi does not have a loop to read continuously. You run it and it reads the serial data once. Then the vi ends. More data is coming in while the vi is stopped. So if you run it again, there is a chance that the buffers have overflowed and you have missing data, which is why your read seems to start in the middle of a sentence. Yes you will get an accumulation of data since the last read.
Can you stop your inclinometer from sending data until a command is sent? If the inclinometer constantly puts out data, your vi needs to be in a loop so that it continuously reads data. Then you would not get any errors. The vi you attached is almost fine. You don't need the delay before read. Instead you should put the Bytes at Port inside a loop and end it when the number of bytes > 0. When the termination character is received, the bytes at port will be greater than 0, and the loop will quit, and then the read takes place. Then the entire write/read process can repeat in a loop until a stop button is pressed. Upon stopping, if you could send a command to the inclinometer to stop sending data, that would be ideal. I have modified your code to make these changes (except for stopping inclinometer at end - add this yourself since I don't know the command).
NOTE: Looks like Ravens Fan beat me to the punch, although my solution is somewhat different. If you are sure the number of bytes is always 10, you can do as he said and you don't need the Bytes at Port loop in my solution. My solution was meant to be generic.
04-20-2010 10:55 AM
04-20-2010 12:15 PM
04-20-2010 04:26 PM
Thanks for the replies guys. I think I'm almost there now.
tbob, I am using the VI you uploaded now and can successfully write the data to a file. Unfortunately, only only values I write are either a 0, or the first data point the inclinometer has received regardless of whether or not enable termination character is set to true or false. If I hit run and then move the inclinometer, the first value captured will be rewritten down the excel spreadsheet, it doesn't respond to any changes in output. Also, I don't see anything come up in the string read box on the front panel but the data is there in the excel file.
To start the inclinometer the command is "go\r\n" and to stop it is "h\r\n", how would I wire the string "h\r\n" to the stop button? I tried to wire a visa write in but don't know how to get the stop button to control it? Currently when I hit the stop button I receive the error
"LabVIEW: Scan failed. The input string does not contain data in the expected format."
but the file is still l created. And if I flip the switch to write "on" does that write the string in the text box on each iteration of the loop?
Thanks,
Steven P