Jump to content

problem while reading Serial data


Recommended Posts

Hi All ,

I am  trying to read 8 fixed length strings using Serial communication in labview ,the designed vi work as i need but sometimes i found some errors .
the format of recieved data:
sting1 string2 string3 string4 string5 strin6 string7 string8\n


this is the vi design

 vi.PNG.ac915b0230d3c5cd18badf91db6e424e.PNG

and these are  errors which somtimes appears and when press continue button the vi keep running as i need.

error1.PNG.51c24dc2e5b3bc15954b6bb949bcee32.PNG  error2.PNG.0b4a381c0121437b2ea35b6bb2920be9.PNG                                                                                  error3.PNG.8e65a6219e92e1b1cdc1e1cab25b34b9.PNG

could anyone help me.
 

Link to comment

I'm surprised it works at all tbh.

The Intialise is in side the while loop but the close is outside. Either move the initialise to outside the loop or the close to the inside.

You start to read when there are greater than 0 bytes (1 or more), but you want 23 bytes. Either accumulate bytes as they come in until you have 23 and then decode into the numerics and strings or wait until there are 23 bytes at the serial port (rather than greater than zero) then decode.

See how far those changes get you.

Edited by ShaunR
Link to comment
9 hours ago, ShaunR said:

I'm surprised it works at all tbh.

The Intialise is in side the while loop but the close is outside. Either move the initialise to outside the loop or the close to the inside.

You start to read when there are greater than 0 bytes (1 or more), but you want 23 bytes. Either accumulate bytes as they come in until you have 23 and then decode into the numerics and strings or wait until there are 23 bytes at the serial port (rather than greater than zero) then decode.

See how far those changes get you.

And the Scan From String does a greedy pattern match, this means the first %s will eat the entire string as any character matches %s and then there is nothing left for the other 7 %s to process => error 85 as the Scan from String can't satisfy the second %s.

If you have a space as separation character as it seems you have from the format you show you should rather use following Scan From String format string

%[^\s]\s%[^\s]\s%[^\s]\s%[^\s]\s%[^\s]\s%[^\s]\s%[^\s]\s%[^\s]

For this you should also right click on the string constant and enable '\' Codes Display

The format string basically says, take every character until you see a space and put this in the first output parameter, then take the space and repeat this for the remaining 7 output strings.

Possibly 23 character could also mean that each substring consists of exactly 2 characters plus the 7 separation characters. If so you could also use:

%2s\s%2s\s%2s\s%2s\s%2s\s%2s\s%2s\s%2s

 

Edited by Rolf Kalbermatter
Link to comment

Before you convert to a string why not take the String from the Read Function, convert to Byte Array, split it into the fixed lengths you want then convert into string. This can be done in a for loop, have an array of lengths and use that to split the string.

Edited by mcduff
Link to comment

1. Move the VISA Configure Serial Port to before the loop.  No need keep re-initializing it.

2. You shouldn't need the Flush Buffer.

3. You should not need the wait.  This would also eliminate the need for the Sequence Structure

4. Since you are expecting a response immediately after you send the command (or soon after), you should not be checking the Bytes At Port.  This would eliminate the need for the Case Structure.

5. Because of the termination character, tell the VISA Read to read more bytes than you expect back.  In this case, I would probably use 50.  The read will complete when the Line Feed is encountered.

6. Since you are dealing with space separated data, use the Spreadsheet String To Array with a space set as the delimiter to make an array of strings, each element corresponding to a piece of data.  You can use Index Array if you really want them separated out.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.