Get value from text file into Click Koyo via Advanced HMI

ASF

Lifetime Supporting Member
Join Date
Jun 2012
Location
Australia
Posts
3,921
Hi all,

I've got an application where I have a text file located on a PC, and among a whole mass of irrelevant text there is a number I want. For the sake of the example, let's say that in this text file, there will always be the exact string "The number for today is" followed by a whole number. The PC that has this file on it has an Advanced HMI application running on it, and is connected via Ethernet to a Click Koyo PLC.

I want to get that number into DS1 on the Click.

I imagine there are a lot of different ways to approach this, but I'm after hints on the neatest and cleanest way to do it.

I'm using a batch file to obtain the text file in the first place, so it wouldn't be too much effort to add some more code to the batch file to clean it up and effectively delete everything except that number, or convert it to a CSV or anything like that. But perhaps Advanced HMI has some functions that can parse through and grab the relevant number more easily?

I'm a total newb to Advanced HMI - I've got it communicating with the Click, but that's about it so far. About to get my feet wet some more, but in the meantime, suggestions involving AHMI might have to be explained in a little more detail than usual :)

Thanks!
 
If you are new with AdvancedHMI, this may seem a little complex at first, but it is VB code to parse a text file, extract the value, then write to the PLC. I just typed this up and didn't test, so it may need a tweak.

- From the All Windows Forms group in the Toolbox, add a Button to your form
- Double click the button to get back to the code
- Enter this code:
Code:
        Dim LineOfText As String = ""
        Dim KeyText As String = "The number for today is"
        Try
            '* Search the text file for the line of text
            Using sw As New System.IO.StreamReader("MyTextFile.txt")
                While LineOfText.IndexOf(KeyText) < 0   ' And Not sw.EndOfStream
                    LineOfText = sw.ReadLine
                End While
            End Using
        Catch ex As Exception
            MsgBox("Error with Text File : " & ex.Message)
            Exit Sub
        End Try

        '* Attempt to parse the value from the line read 
        Dim value As Integer
        Try
            '* Strip off the key text to leave just the value
            value = CInt(LineOfText.Substring(KeyText.Length))
            '* Write to DS1 (Modbus 40001)
            ModbusTCPCom1.Write("40001", value)
        Catch ex As Exception
            MsgBox("Failes to extract value or write to PLC")
        End Try

- Modify the code for the correct file name and key text
- Run the application and when you click the button it should do what you want
 
Last edited:
Thanks Archie, I'll give that a try and let you know how it goes.

I might have to modify things a little more as I've just found out that it's actually the text after the value that's always consistent, not before. The three characters before the value are also always consistent, but there's a chance that those three characters could sometimes by chance appear in another word before that point, so it won't be 100% reliable.

The other potential complication is that the text and the value I want is not on it's own line. There may be text before or after it on the same line, and the value itself may be one or two digits. I can be sure that the next 10-12 characters after the number will all be spaces or letters, not numbers, and I now know that they will be the same each time, so that will probably help.

In any case, if I can't work out the VB Script to extract the value, I can probably do that using the batch file and then just use the second half of your example to write it to the PLC.

Thanks, I'll report back when I get back onto that project!
 
Just to put this one to bed: the example above worked quite well, although I had to play around a little bit with the code to get the extraction of the value working correctly. I also ended up amending the batch file to remove a whole lot of the unnecessary text from the file before AdvancedHMI gets at it, so that streamlined things a bit.

The writing to the PLC part of it worked well. Thanks Archie!
 

Similar Topics

Using Tia Portal v14 and a KTP400 Basic PN, is there a way to embed a tag value into a text list item? I would like to create a selection list...
Replies
3
Views
6,310
Basically, I want a message in Alarm Setup to be string text followed by a DINT. The string text is normal (just like any other text of a...
Replies
9
Views
10,394
I have a device that gives an alarm value as an integer value. i want to display the fault code of this value in text, the range of this value...
Replies
3
Views
3,943
I have tag bits0, bits1, bits2 ... bits1000. Is there a way to use a "fFor" loop in structured text to assign the value 0 to bits0..1000?
Replies
4
Views
2,059
I want to make a program with two POU's, one in Ladder Diagram and the other is in Structure Text. The 1st POU produces an integer output...
Replies
9
Views
3,869
Back
Top Bottom