From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Read: maybe Termination character?

Solved!
Go to solution

Hi all,

I'm trying to use labview to plot data that i'm sending from a pic microcontroller. I'm programming the pic in c so I can pretty easily format the output. Specifically, right now I've been sending hexadecimal numbers in this format "xxxx " to the serial port. I used the advanced serial read and write sample vi, got rid of the write portion, added a convert hex string to number function, and sent the output to a graph. My problem is getting the numbers to sink . For instance if I send the number FFFF sometimes it read _FFF sometimes it reads FFF_ F_FFF etc., when of course I want it to tread FFFF. I'm not sure how to fix this. I've been spending a ton of time on it because sometimes it works perfectly so i'ts frustrating because i can't seem to figure out what to do.  I have the following parameters

 

2400 baud rate

5 bytes read

5 bye input buffer(not really sure waht this should be)

no flow control

no end read on termination character

 

So I was thinking maybe I should add a termination character to my output such as \n... but if I do that and set labview to stop read on termination character as soon as it gets to \n it stops totally. I'm sorry if this post is unclear, please let me know if you need any informatoin I've really spent an incredible amount of time on this so any help would be really great

 

0 Kudos
Message 1 of 25
(4,148 Views)
sorry, I'm also sending data continuously from the pic(I can add a delay if you think that'd help maybe?)
0 Kudos
Message 2 of 25
(4,145 Views)

Try increasing the receive buffer size. Just make it something much bigger than you need; setting aside 512 or 1024 bytes for the receive buffer isn't going to hurt anyone.

 

If your PIC is sending out a constant stream of data like so:

 

<F><F><F><F><SPACE><F><F><F><F><SPACE><F><F><F><F><SPACE>

 

And your buffer is only holding on to 5 of those bytes at a time, it will be a crap shoot as to which "window" of 5 you'll see when you go to read. Your VISA read should be returning an error, something like "Another byte arrived before the receive finished", but depending on how you've handling errors it could be getting ignored.

 

If that doesn't solve your problem post your VI so we can get a better idea of how you're trying to go about this.

0 Kudos
Message 3 of 25
(4,141 Views)

You are going to have to post your code.

 

My guess you are opening and closing the serial port on each read and data may be arriving at the port while it is closed.

0 Kudos
Message 4 of 25
(4,135 Views)

You are expecting synchronous data from an asychnronous process. You have no way of controlling when the LabVIEW program is going to start to read in relationship to when the pic is transmitting. The space character is just another hex character so I don't see the point of that. I have no idea what your pic is doing, but hex 20 (space) is probably just as likely to appear as data as it some sort of separator. You would have the same problem with a \n terminator.

 

Sending ASCII with a terminator would work but you would have to send more data. Maybe instead of a space, you could send a checksum. At the beginning, read 10 bytes and put each byte in an array. Calculate the checksum of elements 0-4. If it matches element 5, you are done. If not, try elements 1-5 and compare to element 6, etc. The whole idea would be to find out how many bytes you off in the read. If the initial read is off, then the next read would be 1,2,3, or bytes which you would discard. After that, every 5 bytes you read would be synched.

 

0 Kudos
Message 5 of 25
(4,121 Views)

Okay, so let me put a brief apology for my code. This was very sloppy because I was trying to get it to work and was just adding and deleting things. I'd clean it up but can't open it from this computer, so can submit a new one in a bit if this is too unclear. However, note one thing, I at one point put a case statement that says if iteration number/1 has remainder of 0 do this... that's because originally i was getting 0 every other read... so I was just dividing my two and reading only even iterations(stupid I know, but againi just trying to get it to work). However, I fixed that problem and so just changed it to /1, so it's alwayss true and a worthless case statement, but I plan on removing it once i get the program working, it does nothing bad or good right now.  Again, settings aren't hard coded here but set from a gui, but the settings i listed above are what i use when running it(Easier for debugging).

 

right now, again I use the following settings

read bytes: 5

termination character read: off

baud 2400

let me know if you need more information. thanks a ton

Ben

0 Kudos
Message 6 of 25
(4,117 Views)

dennis - great idea. thank you i'll give that a shot, never thought about that!

 

0 Kudos
Message 7 of 25
(4,110 Views)

My understanding is that you are justing sending the ASCII characters for the letters A through F and also a space.  ???

 

So using a termination character for separating the data would be just fine whether it is a space for newline character ASCII 10.

 

Set your read to be a larger number of bytes (more than 4), enable the termination character, set the termination character to be a space.  Set the timeout to be a large enough time.

 

Just read.  It will read until it gets enough bytes (whatever you set above) or it receives the termination character.  Now the very first read could be short because you could have opened the port intially in the middle of your data frame, so just discard the first set of data.  After that, all of the data should be framed okay because it would have ended on the space character.

 

PS.  Turn ON autogrow for structures.  You have several elements that have a shadow (Error handler, Read boolean control) because they are on top of loops and case structures, but are not a part of them.  So the wires can't be seen and you can't tell what those nodes belong to.  There are other nodes that are behind the loops and thus are completely hidden.  (Close port)

Message 8 of 25
(4,104 Views)
sorry this will sound exceptionally stupid, but how do you actually set the termination character to be a space, I can't tell at all how to set it correctly.
0 Kudos
Message 9 of 25
(4,085 Views)
The control on your front panel is set for Hex.  Space is Hex 20.  So enter 20 in that control rather than A.
0 Kudos
Message 10 of 25
(4,080 Views)