sareehy ak Posted August 12, 2023 Report Share Posted August 12, 2023 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 and these are errors which somtimes appears and when press continue button the vi keep running as i need. could anyone help me. Quote Link to comment
ShaunR Posted August 12, 2023 Report Share Posted August 12, 2023 (edited) 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 August 12, 2023 by ShaunR Quote Link to comment
Rolf Kalbermatter Posted August 12, 2023 Report Share Posted August 12, 2023 (edited) 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 August 12, 2023 by Rolf Kalbermatter Quote Link to comment
mcduff Posted August 12, 2023 Report Share Posted August 12, 2023 (edited) 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 August 13, 2023 by mcduff Quote Link to comment
crossrulz Posted August 14, 2023 Report Share Posted August 14, 2023 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. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.