Jump to content
Sign in to follow this  
Klovis

String logic (Closed!)

Recommended Posts

Klovis

so in a tcp connection I'm receiving

channel1

freq=

9.426686e+010

1.923659e+010

1.190006e+010

1.557381e+010

channel2

freq=

1.300763e+011

2.108867e+009

1.961441e+009

8.797350e+008

and I need to get the first value of channel 1 and set it to a variable and the first value of channel two and set it to a variable. so how would I parse the recived data with stringleft,stringright,stringinstr,etc.?

Edited by Klovis

Share this post


Link to post
Share on other sites
sigil

Assuming the tcp input is formatted using @crlf line breaks:

;simulate the tcp input
$mystr="channel1"&@crlf&"freq="&@crlf&"9.426686e+010"&@crlf&"1.923659e+010"&@crlf&"1.190006e+010"&@crlf&"1.557381e+010"&@crlf&"channel2"&@crlf&"freq="&@crlf&"1.300763e+011"&@crlf&"2.108867e+009"

;split it into lines
$array=stringsplit($mystr,@crlf)

;get the frequencies
$freq1=$array[5]
$findchannel2=_arraysearch($array,"channel2")
$freq2=$array[$findchannel2+4]

;display frequencies
msgbox(0,"freq1,"$freq1)
msgbox(0,"freq2",$freq2)
Edited by sigil

Share this post


Link to post
Share on other sites
Klovis

It worked thanks now I have to implement it into my script ^^

Share this post


Link to post
Share on other sites
Klovis

okay so I implemented it into my script and I guess i left you in the dark about something I'm getting like an unknown amount of these every split second so the array succeeds the subscript so is there another method to parse the data that I can use?

Share this post


Link to post
Share on other sites
Klovis

here are some files that might help you, I cant give you the thing sending the data because its not on my computer but I emulated a tcp send for your convenience.

Server.au3

Button1.au3

Share this post


Link to post
Share on other sites
sigil

I'm getting like an unknown amount of these every split second so the array succeeds the subscript

I'm sorry, I don't understand what you mean by "succeeds the subscript".

Edited by sigil

Share this post


Link to post
Share on other sites
Klovis

Line 47

$freq1=$array[5]

$freq1=^ ERROR

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

That's the error im getting word for word

Share this post


Link to post
Share on other sites
sigil

Line 47

$freq1=$array[5]

$freq1=^ ERROR

Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

That's the error im getting word for word

Oh. I see.

You're using StringSplit() on the socket connection you opened with

$TCPRecv = TCPRecv($TCPAccept, 128)

You need to split up the log that you wrote to "MatLabReceived.txt".

So instead of creating $array using this line:

$array=stringsplit($TCPRecv,@crlf)

use this code instead:

dim $array
_FileReadToArray("MatLabReceived.txt",$array)

You'll need to include <File.au3>.

Also, since $array[0] will contain the total number of records in $array[], you need to change

$freq1=$array[5]

to

$freq1=$array[6]
Edited by sigil

Share this post


Link to post
Share on other sites
Klovis

Says that $array is a non-array variable did I mess up?

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

Global $hGUI, $TCPAccept = 10, $TCPListen, $TCPPrevious = 255, $TCPRecv
TCPStartup()
$port = 5000
$TCPListen = TCPListen(@IPAddress1, $port)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Server", 148, @DesktopHeight, 0, 0)
$Label1 = GUICtrlCreateLabel("Server IP: ", 8, 8, 54, 17)
$Label2 = GUICtrlCreateLabel("Connected Port:", 8, 40, 81, 17)
$Label3 = GUICtrlCreateLabel("Recieved Data: ", 8, 72, 82, 17)
$Label4 = GUICtrlCreateLabel("Label4", 8, 24, 132, 17)
$Label5 = GUICtrlCreateLabel("Label5", 8, 56, 132, 17)
$Label6 = GUICtrlCreateLabel("Waiting For Connections...", 8, 88, 132, @DesktopHeight)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUICtrlSetData($Label4, @IPAddress1)
GUICtrlSetData($Label5, $port)

Do
    $TCPAccept = TCPAccept($TCPListen)
Until $TCPAccept <> -1

GUICtrlSetData($Label6, "Connection Open")

While 1
    $TCPRecv = TCPRecv($TCPAccept, 1000000)
    If $TCPRecv <> "" And $TCPRecv <> $TCPPrevious Then
        $TCPPrevious = $TCPRecv
        ConsoleWrite("What is recieved >> " & $TCPPrevious & @CRLF)
        FileWrite("MatLabRecived.txt", $TCPPrevious)
        If $TCPPrevious > $TCPPrevious +1 Then
            Do
                $TCPPrevious = $TCPPrevious + 1
            Until $TCPPrevious = $TCPRecv
        EndIf
        If $TCPPrevious < $TCPPrevious -1 Then
            Do
                $TCPPrevious = $TCPPrevious - 1
            Until $TCPPrevious = $TCPRecv
        EndIf
        dim $array
        _FileReadToArray("MatLabReceived.txt",$array)
        $freq1=$array[6]
        $findchannel2=_arraysearch($array,"channel2")
        $freq2=$array[$findchannel2+4]
        GUICtrlSetData($Label6, Number($freq1) & @CRLF & Number($freq2))
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites
sigil

Made a couple changes:

-changed FileWrite() to FileWriteLine()

-implemented file read/write using file handle

-used same code to get $freq1 and $freq2

Try it now. Also, open MatLabReceived.txt after your script runs so you can see if it's being written to appropriately.

You should probably put in some error checking at some point, e.g., testing to make sure MatLabReceived.txt opens without error, if $array has been successfully created as an array, etc.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

Global $hGUI, $TCPAccept = 10, $TCPListen, $TCPPrevious = 255, $TCPRecv
TCPStartup()
$port = 5000
$TCPListen = TCPListen(@IPAddress1, $port)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Server", 148, @DesktopHeight, 0, 0)
$Label1 = GUICtrlCreateLabel("Server IP: ", 8, 8, 54, 17)
$Label2 = GUICtrlCreateLabel("Connected Port:", 8, 40, 81, 17)
$Label3 = GUICtrlCreateLabel("Recieved Data: ", 8, 72, 82, 17)
$Label4 = GUICtrlCreateLabel("Label4", 8, 24, 132, 17)
$Label5 = GUICtrlCreateLabel("Label5", 8, 56, 132, 17)
$Label6 = GUICtrlCreateLabel("Waiting For Connections...", 8, 88, 132, @DesktopHeight)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUICtrlSetData($Label4, @IPAddress1)
GUICtrlSetData($Label5, $port)

Do
    $TCPAccept = TCPAccept($TCPListen)
Until $TCPAccept <> -1

GUICtrlSetData($Label6, "Connection Open")

$fHandle=fileopen("MatLabReceived.txt",2)

While 1
    $TCPRecv = TCPRecv($TCPAccept, 1000000)
    If $TCPRecv <> "" And $TCPRecv <> $TCPPrevious Then
        $TCPPrevious = $TCPRecv
        ConsoleWrite("What is recieved >> " & $TCPPrevious & @CRLF)
        FileWriteLine($fHandle, $TCPPrevious)
        If $TCPPrevious > $TCPPrevious +1 Then
            Do
                $TCPPrevious = $TCPPrevious + 1
            Until $TCPPrevious = $TCPRecv
        EndIf
        If $TCPPrevious < $TCPPrevious -1 Then
            Do
                $TCPPrevious = $TCPPrevious - 1
            Until $TCPPrevious = $TCPRecv
        EndIf
        dim $array
        _FileReadToArray($fHandle,$array)
        $findchannel1=_arraysearch($array,"channel1")
        $freq1=$array[$findchannel1+4]
        $findchannel2=_arraysearch($array,"channel2")
        $freq2=$array[$findchannel2+4]
        GUICtrlSetData($Label6, Number($freq1) & @CRLF & Number($freq2))
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
Edited by sigil

Share this post


Link to post
Share on other sites
Klovis

AutoIt Error

Line 52

$freq1=$array[$findchannel1+4]

$freq1=$array^ERROR

Error: Subscript used with non-Array variable.

Share this post


Link to post
Share on other sites
sigil

AutoIt Error

Line 52

$freq1=$array[$findchannel1+4]

$freq1=$array^ERROR

Error: Subscript used with non-Array variable.

I think at this point I'm going to let you debug your own code, or let someone else step in to help out.

Some last tips on how to find out what to fix in your program:

-Use _ArrayDisplay() and IsArray() to check the status of $array at different points in the script.

-Try using Exit to halt your script prematurely, and check to see if you've written to MatLabReceived.txt correctly.

-Move pieces of code around. Put things inside and outside of your if-then statements and while loops, see what happens.

Good luck!

Share this post


Link to post
Share on other sites
Klovis

Okay well then I guess I'll have to restate the question then.

Okay so I get 50 or so of these a second:

channel1

freq=

9.426686e+010

1.923659e+010

1.190006e+010

1.557381e+010

channel2

freq=

1.300763e+011

2.108867e+009

1.961441e+009

8.797350e+008

And they all come in at once so the letters fly off the page.

I get this data through a tcp connection, I just need to find out how to parse the data and take the first value after Channel 1 and make it into a variable, and same with the first value after channel 2.

This is the code I'm using now:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>

Global $hGUI, $TCPAccept = 10, $TCPListen, $TCPPrevious = 255, $TCPRecv
TCPStartup()
$port = 5000
$TCPListen = TCPListen(@IPAddress1, $port)

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Server", 148, @DesktopHeight, 0, 0)
$Label1 = GUICtrlCreateLabel("Server IP: ", 8, 8, 54, 17)
$Label2 = GUICtrlCreateLabel("Connected Port:", 8, 40, 81, 17)
$Label3 = GUICtrlCreateLabel("Recieved Data: ", 8, 72, 82, 17)
$Label4 = GUICtrlCreateLabel("Label4", 8, 24, 132, 17)
$Label5 = GUICtrlCreateLabel("Label5", 8, 56, 132, 17)
$Label6 = GUICtrlCreateLabel("Waiting For Connections...", 8, 88, 132, @DesktopHeight)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUICtrlSetData($Label4, @IPAddress1)
GUICtrlSetData($Label5, $port)

Do
    $TCPAccept = TCPAccept($TCPListen)
Until $TCPAccept <> -1

GUICtrlSetData($Label6, "Connection Open")

$fHandle=fileopen("MatLabReceived.txt",2)

While 1
    $TCPRecv = TCPRecv($TCPAccept, 1000000)
    If $TCPRecv <> "" And $TCPRecv <> $TCPPrevious Then
        $TCPPrevious = $TCPRecv
        ConsoleWrite("What is recieved >> " & $TCPPrevious & @CRLF)
        FileWriteLine($fHandle, $TCPPrevious)
        If $TCPPrevious > $TCPPrevious +1 Then
            Do
                $TCPPrevious = $TCPPrevious + 1
            Until $TCPPrevious = $TCPRecv
        EndIf
        If $TCPPrevious < $TCPPrevious -1 Then
            Do
                $TCPPrevious = $TCPPrevious - 1
            Until $TCPPrevious = $TCPRecv
        EndIf
        Dim $array
        _FileReadToArray($fHandle,$array)
        $findchannel1=_arraysearch($array,"channel1")
        $freq1=$array[$findchannel1+4]
        $findchannel2=_arraysearch($array,"channel2")
        $freq2=$array[$findchannel2+4]
        GUICtrlSetData($Label6, Number($freq1) & @CRLF & Number($freq2))
    EndIf
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

And this is the error I'm getting

Line 52

$freq1=$array[$findchannel1+4]

$freq1=$array^ERROR

Error: Subscript used with non-Array variable.

Thanks so much for taking the time to help me

-Klovis

Share this post


Link to post
Share on other sites
BrewManNH

_FileReadToArray($fHandle,$array)

$fhandle is a file handle, you need to read the file with the file name. $array is probably empty and if you'd done some basic error checking you'd probably have realized that by now.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Klovis

two things of which I didn't input into my code, arrays and file handles. I'll see what I can do

Share this post


Link to post
Share on other sites
Klovis

basically all i did was to the

Dim $array

I turned it into

Dim $array[6]

Now what you asked me to do with the file handle totally boggles my mind

Share this post


Link to post
Share on other sites
Klovis

I don't see how changing dim to global would help

Edit: Where did your post go!

Edited by Klovis

Share this post


Link to post
Share on other sites
BrewManNH

Instead of using

_FileReadToArray($fHandle,$array)

Use this instead:

_FileReadToArray("MatLabReceived.txt", $array)

The help file says that _FileReadToArray needs a filename and path, it won't work with the file handle returned by the FileOpen command.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Klovis

Okay well I'm getting the data received in my .txt doc but I honestly don't care about the text document I just want to see the results in $Label6

Share this post


Link to post
Share on other sites
BrewManNH

What does the Array "$array" contain after reading the text file in after every read operation? Check to make sure it's actually reading something from the file before trying to use the array. Either insert an _ArrayDisplay($array) to debug, after the _FileReadToArray, or use an If IsArray($array) to do a check of the $array variable to make sure it actually contains something.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×