From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, 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: 

Search and Replace Multiple Strings (Neatly!)

Solved!
Go to solution

Hello,

 

As part of an instrument control VI, I'd like to convert serial messages from the instrument to plain text. Most of them are currently 3-letter abbreviations and I'm trying to make it so the operator (not me) doesn't need to memorise/look up these messages. 

 

I found an example somewhere and have working code (see attached screenshot) but it's starting to get a little ugly with only a small fraction of the messages done.

 

I have a feeling there's a neater way, I just don't know what it is, a steer would be appreciated!

0 Kudos
Message 1 of 9
(1,709 Views)
Solution
Accepted by topic author gf34

Unfortunately you are only showing a truncated picture. We cannot really debug a picture and prefer the actual VI.

 

Personally, I would use a map with the abbreviation as key and the full text as value. Now simply iterate over all words and substitute the value for all keys that are part of the map.

 

Your code overall seems highly flawed.

 

  • Isn't there a termination character so you can eliminate "bytes at port"?
  • Your string grows forever and you are processing new and old data with each iteration. After 1000 iterations, you have replaced the first key 1000 times!!! Sisyphean!
  • Why is there a local variable?
  • Why do you create an array of errors just to merge them later?
  • Where is the rest of the code?
  • etc. etc.

 

Message 2 of 9
(1,684 Views)

Thanks - I'll have a go with the map.

 

The rest of the code is just as much of a mess with some things half-implemented/half-baked and so on - I wouldn't wish it on anyone 😂

 

Thank you for the additional feedback though - it's early days so I'll keep chipping away at it and come back to the forums if I get stuck.

 

0 Kudos
Message 3 of 9
(1,677 Views)

You could also have a 1D array of cluster where each cluster has two elements (source text and replacement text) or it could be a 2D string array where each row has two elements. You could even load this list from a file (like a CSV file, or tab separated, if you have commas in your text) and then edit it in something like Excel, which might make it easier to edit and add items.

 

If you intend to have things like line feeds in your text, as you show in the example, then you would need to escape them if you intend to save this in a file.


___________________
Try to take over the world!
0 Kudos
Message 4 of 9
(1,616 Views)

I like the idea of using an array as it means I don't need all the individual strings. I've not been able to crack it though I'm afraid - I can't work out how to search and replace from a 2D array.

 

I've attached the VI this time - if you have the time to show me how to do it, that would be great  (sorry if it's obvious, I've just been staring at it too long now). Feel free to tear into the rest of it but please bear in mind that this isn't my trade and I'm just learning!

 

As a bit of background: this is a water sampling instrument that is automatic once set (i.e. there is no continuous acquisition). This is how I've been able to get away with the mad string processing - there are only a few lines involved in the setup. When I come to try and download the data off the instrument it's going to die though 😂The idea is that the VI primarily serves as a user-friendly replacement for Putty and the instruction manual.  

 

Thanks in advance for any help!

 

0 Kudos
Message 5 of 9
(1,601 Views)

Well, that VI requires quite a bit of remodeling. 😄

 

  • Almost all your sequence structures are completely useless. A sequence frame without data dependency is the same as no sequence frame at all!
  • You have potential race conditions because your local variable initializations execute in parallel to the loops. Here you need a sequence frame to ensure that the loops only start once the locals have been written (Virtually all your booleans could be made latch action, eliminating all locals and inner case structures).
  • Why do you have a unused timeout event and a wait in that same loop?
  • Your reading loop still has all the faults that I have already mentioned.
  • Can you give a short example of typical received strings so we can play without having your hardware?
0 Kudos
Message 6 of 9
(1,597 Views)

@gf34 wrote:

I can't work out how to search and replace from a 2D array.


I haven't looked at your current VI (I'm sure Altenbach's list is enough for you to chew on), but the 2D array part or 1D cluster array is easy. In fact, you already have it in your original screenshot, but there it's split into two 1D arrays. If you make it one of the suggested options you can simply iterate over the rows in the same way you did and take the relevant info out of each row inside the for loop (using either an index array or an unbundle).


___________________
Try to take over the world!
0 Kudos
Message 7 of 9
(1,581 Views)

Hello, Mk2 attached!

 

I got rid of the Bytes at Port but now I get a timeout error whenever there is no data (of course I don't see it until I exit the VI though) - how would I stop that from popping up each time please? It also slows down the exit by a couple of seconds.

 

I've attached examples of the messages from the instrument (startup, info header, acknowledgment).

 

Hopefully things have improved anyway...the biggest thing remaining is implementing the data download.

 

Thanks again

0 Kudos
Message 8 of 9
(1,558 Views)

Hi gf,

 


@gf34 wrote:

I got rid of the Bytes at Port but now I get a timeout error whenever there is no data (of course I don't see it until I exit the VI though) - how would I stop that from popping up each time please?


As soon as you implement some error handling in your VI you will discover that error instantly - and not "until you exit the VI"!

 


@gf34 wrote:

Hopefully things have improved anyway...the biggest thing remaining is implementing the data download.


Well, several problems/issues:

  • NEVER use a "default if unwired" tunnel for references (of all kinds)! You lose the communication as soon as there is a value change on "amount (ml)"!
  • Why are there two loops iterating at 100ms just to update some UI elements ("Time interval", "Date Time…")? Why not place their code into just one loop? Do you really need to write all those property nodes each 100ms?
  • It can create problems to fork reference wires, like you do. What happens to the "Serial read…" loop when the reference is (somehow) changed/closed in a different loop? Again no error handling in that loop…
  • Why do you need two IndexArray nodes in the "Serial Read…" loop? You can resize them to show more than one output…
  • Why is there a wait function in parallel to the event structure - which has no timeout event!?
  • What's that Rube-Goldberg in the "Exit" event case???
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 9 of 9
(1,554 Views)