Sign in to follow this  
Followers 0
rharwood

parsing serial data

12 posts in this topic

Can anyone point me in the correct direction

I have been working on something using a sample file I got from the samples forum involving reading serial data(commgExample) from the comm port and while the sample seems to capture a live serial input, I can't seem to modify it.

I am trying to parse the data coming in from the serial port and if it contains certain strings, IE ALARM or TROUBLE i want to change the output font color (I have tryed _GuiCtrlRichEdit_SetCharColor($instr, "109010")but can't figure out how to change colours)

Also at the same time i want to be able to log the data to a text file IS this correct for the file generation "_GuiCtrlRichEdit_StreamToFile($instr, @DesktopCommonDir & "\myfile.rtf")"

Share this post


Link to post
Share on other sites



Can anyone point me in the correct direction

No problem. Just post a _short_ example code that doesn't behave as you expect, explain what you expect and you'll probably get answers.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

No problem. Just post a _short_ example code that doesn't behave as you expect, explain what you expect and you'll probably get answers.

ok here is what I have come up with so far this is a mod from the commgExample.au3

While 1

;gets characters received returning when one of these conditions is met:

;receive @CR, received 20 characters or 200ms has elapsed

$instr = _CommGetString()

If $instr <> '' Then;if we got something

If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr,@CR,@CRLF)

GUICtrlSetData($Edit1, $instr,1)

_GuiCtrlRichEdit_SetCharColor($Edit1, "109010") ; does not work

if StringRegExp( $instr, "ALARM" ) then ; this does not work

;code to change to red text on display and make sound

ConsoleWrite($instr );this does not work here but does work below

endif

if StringRegExp( $instr, "Trouble" ) then

;code to change to yellow text on display and make sound

ConsoleWrite($instr )

endif

if StringRegExp( $instr, "SUPERVISORY" ) then

;code to change to Yellow text on display and make sound

ConsoleWrite($instr )

endif

;following statement does not work yet

_GuiCtrlRichEdit_StreamToFile($Edit1, @DesktopCommonDir & "\gcre.rtf")

; log it to a file as well , this option will be set by chkbox later on

; once i get basic program functional

ConsoleWrite($instr );This works

EndIf

WEnd

Share this post


Link to post
Share on other sites

Please, post code within AutoIt tags (the blue square icon below Bold)

I've annotated your code and changed several things. Read the comments and tell us if things seem clearer.

We have no idea what the response of your device can be: multi-line or not, fixed format, whatever. That should be well defined in your code. You can also lookup keywords returned in an array of constants, ...

As I see it (and I'm far enough away to guess wrong!) you shouldn't kep @CR or @CRLF but only the data. You can always add your own control characters as needed for display and storage. The more _you_ control, the less room is left for unexpected things to break a previously working program.

Also beware that GUI* and _GUI don't always coexist without problems. Take your pick!

;; you should tell yourself (and those who try to help you) what's the expected format of your data, protocol, etc.

While 1

    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed

    ;; probably a loop waiting for end of line char with _CommGetLine would be better here, with timeout and graceful recover
    $instr = _CommGetString()

    If $instr <> '' Then;if we got something

        ; you should probably remove @CR, or use _CommGetLine
        ConsoleWrite("We received >" & $instr & "<" )   ;This works

        ;; as a rule of thumb, don't mix GUI* and _GUI* functions/objects
        ;;
        ;; make sure $Edit1 if the handle returned by _GUICtrlRichEdit_Create
        ;;
        ;; finally, you should probably postpone displaying things until they have been recognised and validated ???
        If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr,@CR,@CRLF)
        _GuiCtrlRichEdit_SetText($Edit1, $instr)        ;; <<-- !!!
        ;; select some text before applying set color
        _GuiCtrlRichEdit_SetSel($Edit1, 1, -1)
        _GuiCtrlRichEdit_SetCharColor($Edit1, "109010") ; does not work

    ;; should the comparison be case sensitive?
    ;; also StringInStr should do as well
    ;;
    ;; I personally would write it:
    ;;
;~      Select
;~          Case StringInStr($instr, 'ALARM')
;~              ;; process alarm condition
;~          Case StringInStr($instr, 'Trouble')     ;; <<-- beware the case, could be important
;~              ;; process Trouble condition
;~          Case StringInStr($instr, 'SUPERVISORY')
;~              ;; process supervisory condition
;~          Case Else
;~              ;; received something else, comm error or unexpected response, or man-in-the-middle attack ;-)
;~      EndSelect

        if StringRegExp( $instr, "ALARM" ) then ; this does not work
            ;code to change to red text on display and make sound
            ConsoleWrite($instr );this does not work here but does work below
        endif
;; shouldn't that be ElseIf? can you have simultaneous conditions? _We_ don't know and can only wild guess

        if StringRegExp( $instr, "Trouble" ) then
            ;code to change to yellow text on display and make sound
            ConsoleWrite($instr)
        endif

        if StringRegExp( $instr, "SUPERVISORY" ) then
            ;code to change to Yellow text on display and make sound
            ConsoleWrite($instr)
        endif

        ;following statement does not work yet
        _GuiCtrlRichEdit_StreamToFile($Edit1, @DesktopCommonDir & "\gcre.rtf")

        ; log it to a file as well , this option will be set by chkbox later on
        ; once i get basic program functional

        ConsoleWrite($instr);This works
    EndIf

WEnd

Hope this helps.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks it will get me started.. The whole idea of this is to just have color coding added to the output screen, and a logging capability. We are basically taking serial data meant for a serial dot matrix printer and having it show up on the pc screen. As you saw in the snippets of code that I wrote any string with the word alarm in it should change the text red etc. Would you recommend ditching the _gui stuff altogether the only reason I used it was all the code I code find referencing changing colors seemed to be there.

Share this post


Link to post
Share on other sites

As you saw in the snippets of code that I wrote any string with the word alarm in it should change the text red etc. Would you recommend ditching the _gui stuff altogether the only reason I used it was all the code I code find referencing changing colors seemed to be there.

If you want color coded display, keep the _GUI rich edit control. But keep in mind that you then need to create, control, destroy it all wih _GUI functions. The reason for this is that the GUI* control IDs and the _GUI* handles are two completely different guys. Don't use GUI functions with _GUI handles, not _GUI functions with GUI IDs as it either won't work or break something else. Again, I'm certainly not a GUI or _GUI guru and I hope some guru would correct me if I'm telling bullshit.

Globally, _GUI UDFs gives you finer grain control over your GUI. They also need finer grain attention from your application. For instance, _GUICtrlRichEdit_SetCharColor changes the color of the selected text (thus you need to explicitely select text for it to be effective) or, if no text selected, changes the background color of the text inserted at the insertion point. That's what the help file for this function says, and what it actually does.

I now understand that you use a PC in place of a (now obsolete I presume) serial printer. In this case, you need a slightly different strategy for processing your input.

Here's why: your current code grabs input with _CommGetString, which reads up 20 characters of data until @CR or timeout. It can happen that your input (in red) will arrive like this:

abcdef 123456 ALA <<-- 200ms timeout

... you process this and find nothing "alarming"

RM safe being blaste <<-- 20 character limit reached

... you process this as well, still unsuspecting of what really happens

d by a gang!CR <<-- CR received

Gotcha!

Be sure that if it may happen, it will happen!

If you can ascertain that your "keywords" won't be split in the middle of a line, you should buffer input until you receive a @CR (within a loop and with a timeout to make things responsive in case something goes wrong) and only then, process you line(s) looking for alarm and friends.

That's some guidelines that I think of right now.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

actually i got the color thing working i think, but now the select part does not ,, as for data right now I am using the send function from within the program to kinda simulate the data,(I am on a road trip right now outa town with no access to the panels that this will connect to). Here is the complete code that I just put together. If you run it with a loopback on serial port anything right now will show white text on a black background. Hopefully if I can get the select statements to work it will set the Text color depending on which case statement it follows.

;this example is a very simple terminal
;Version 1
;changes-

#include <GUIConstants.au3>
#include 'CommMG.au3';or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'
#include <GuiEdit.au3>
#include <GuiComboBox.au3>
#include <windowsconstants.au3>
#include <buttonconstants.au3>
#include <GuiRichEdit.au3>
Opt("WINTITLEMATCHMODE", 3)
OnAutoItExitRegister("alldone")
HotKeySet("{ESC}", "alldone")

$result = '';used for any returned error message setting port
Const $settitle = "COMMG Example - set Port", $maintitle = "Virtual Print"
$setflow = 2;default to no flow control
Dim $FlowType[3] = ["XOnXoff", "Hardware (RTS, CTS)", "NONE"]
#Region main program

#Region ### START Koda GUI section ### Form=d:\my documents\miscdelphi\commg\ExampleComm.kxf
$Form2 = GUICreate("Virtual Print", 473, 349, 339, 333, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))
$Edit1 = GUICtrlCreateEdit("", 10, 25, 449, 223, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$BtnSend = GUICtrlCreateButton("Send", 380, 273, 53, 30, $BS_FLAT)
$Input1 = GUICtrlCreateInput("", 18, 279, 361, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Add LF to incomming CR", 273, 4, 145, 17)
$Checkbox2 = GUICtrlCreateCheckbox("Log to file", 105, 4, 145, 17)
GUICtrlSetState($Checkbox1, $GUI_CHECKED)
GUICtrlSetState($Checkbox2, $GUI_CHECKED)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
$Label11 = GUICtrlCreateLabel("Text to send", 24, 261, 63, 17)
$BtnSetPort = GUICtrlCreateButton("Set Port", 16, 312, 73, 30, $BS_FLAT)
$BtnSilence = GUICtrlCreateButton("Silence Alarms", 150, 312, 73, 30, $BS_FLAT)
$Label21 = GUICtrlCreateLabel("Received text", 34, 6, 70, 17)
$Label31 = GUICtrlCreateLabel("commg.dll version unknown", 272, 328, 135, 17)
GUICtrlSetColor(-1, 0x008080)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
WinSetTitle($Form2, "", $maintitle & _CommGetVersion(1))

While setport(0) = -1
    If MsgBox(4, 'Port not set', 'Do you want to quite the program?') = 6 Then Exit
WEnd






;GUISwitch($Form2)
ConsoleWrite("stage 1" & @CRLF)
GUICtrlSetData($Label31, 'using ' & _CommGetVersion(1))
ConsoleWrite("stage 2" & @CRLF)
Events()
GUICtrlSetBkColor($Edit1, 0x000000)     ;black
GUICtrlSetColor($Edit1, 0xffffff)   ; white
GUICtrlSetState($Edit1, $GUI_FOCUS)

While 1

    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _CommGetString()

    If $instr <> '' Then;if we got something

        If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr,@CR,@CRLF)
            
                ;GUICtrlSetColor($Edit1, 0xff0000)   ; Red
                ;GUICtrlSetBkColor($Edit1, 0x000000)     ;black
                ;GUICtrlSetData($Edit1, $instr,1) ;This will have to be removed once Select is working
        
Select
          Case StringInStr($instr, 'ALARM')
              ;; process alarm condition
                GUICtrlSetColor($Edit1, 0xff0000)   ; Red
                GUICtrlSetData($Edit1, $instr,1)
                SoundPlay(@WindowsDir & "\media\tada.wav",1); alerts user to alarm condition, siren sound prob or bell ring

          Case StringInStr($instr, 'Trouble')     ;; <<-- beware the case, could be important
              ;; process Trouble condition
                GUICtrlSetColor($Edit1, 0xffff00)   ; yellow
                SoundPlay(@WindowsDir & "\media\tada.wav",1); set this to alert tone
                
          Case StringInStr($instr, 'SUPERVISORY')
              ;; process supervisory condition
                GUICtrlSetColor($Edit1, 0xffff00)   ; yellow
                GUICtrlSetData($Edit1, $instr,1)
                SoundPlay(@WindowsDir & "\media\tada.wav",1); just beep once
                
          Case StringInStr($instr, 'Restored')
              ;; process supervisory condition
                GUICtrlSetColor($Edit1, 0x7FFF00)   ; Green
                GUICtrlSetData($Edit1, $instr,1)
                
          
          
          
          
          
          
      Case Else
            GUICtrlSetColor($Edit1, 0xffffff)
            GUICtrlSetData($Edit1, $instr,1)
            
              ;; received something else, comm error or unexpected response, or man-in-the-middle attack ;-)
      EndSelect
        ;GUICtrlSetData($Edit1, $instr,1)   
        
        ;following statement does not work yet
        if GUICtrlRead($Checkbox2) = $GUI_CHECKED Then 
        _GuiCtrlRichEdit_StreamToFile($instr, @DesktopCommonDir & "\myfile.rtf") ; log it to a file as well , 
        
        endif
        
    EndIf

WEnd

Alldone()


Func port11()
    ;MsgBox(0,'now set to channel',_CommSwitch(2))
    _commSwitch(2)
    $s2 = "1 2 3 4";_CommGetString()
    ConsoleWrite("comm1 gets " & $s2 & @CRLF)
    _CommSendString($s2)
    _CommSwitch(1)

EndFunc   ;==>port11

#EndRegion main program
Func Events()
    Opt("GUIOnEventMode", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "justgo")
    GUICtrlSetOnEvent($BtnSend, "SendEvent")
    GUICtrlSetOnEvent($BtnSetPort, "SetPortEvent")
EndFunc   ;==>Events

Func SetPortEvent()
    setport();needed because a parameter is optional for setport so we can't use "setport" for the event
    GUICtrlSetState($Edit1, $GUI_FOCUS)
EndFunc   ;==>SetPortEvent

Func justgo()
    Exit
EndFunc   ;==>justgo

Func SendEvent();send the text in the inputand append CR
    _CommSendstring(GUICtrlRead($Input1) & @CR)
    GUICtrlSetData($Input1, '');clear the input
    ;GUICtrlSetState($edit1,$GUI_FOCUS);sets the caret back in the terminal screen
EndFunc   ;==>SendEvent


Func AllDone()
    ;MsgBox(0,'will close ports','')
    _Commcloseport()
    ;MsgBox(0,'port closed','')
    Exit
EndFunc   ;==>AllDone


; Function SetPort($mode=1)
; Creates a form for the port settings
;Parameter $mode sets the return value depending on whether the port was set
;Returns  0 if $mode <> 1
;          -1 If` the port not set and $mode is 1
Func SetPort($mode = 1);if $mode = 1 then returns -1 if settings not made

    Opt("GUIOnEventMode", 0);keep events for $Form2, use GuiGetMsg for $Form3

    #Region ### START Koda GUI section ### Form=d:\my documents\miscdelphi\commg\examplecommsetport.kxf
    $Form3 = GUICreate("COMMG Example - set Port", 422, 279, 329, 268, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS, $DS_MODALFRAME), BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
    $Group1 = GUICtrlCreateGroup("Set COM Port", 18, 8, 288, 252)
    $CmboPortsAvailable = GUICtrlCreateCombo("", 127, 28, 145, 25)
    $CmBoBaud = GUICtrlCreateCombo("9600", 127, 66, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $CBS_SORT, $WS_VSCROLL))
    GUICtrlSetData(-1, "10400|110|115200|1200|128000|14400|150|15625|1800|19200|2000|2400|256000|28800|3600|38400|4800|50|56000|57600|600|7200|75")
    $CmBoStop = GUICtrlCreateCombo("1", 127, 141, 145, 25)
    GUICtrlSetData(-1, "1|2|1.5")
    $CmBoParity = GUICtrlCreateCombo("none", 127, 178, 145, 25)
    GUICtrlSetData(-1, "odd|even|none")
    $Label2 = GUICtrlCreateLabel("Port", 94, 32, 23, 17)
    $Label3 = GUICtrlCreateLabel("baud", 89, 70, 28, 17)
    $Label4 = GUICtrlCreateLabel("No. Stop bits", 52, 145, 65, 17)
    $Label5 = GUICtrlCreateLabel("parity", 88, 182, 29, 17)
    $CmboDataBits = GUICtrlCreateCombo("8", 127, 103, 145, 25)
    GUICtrlSetData(-1, "7|8")
    $Label7 = GUICtrlCreateLabel("No. of Data Bits", 38, 107, 79, 17)
    $ComboFlow = GUICtrlCreateCombo("NONE", 127, 216, 145, 25)
    GUICtrlSetData(-1, "NONE|XOnXOff|Hardware (RTS, CTS)")
    $Label1 = GUICtrlCreateLabel("flow control", 59, 220, 58, 17)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $BtnApply = GUICtrlCreateButton("Apply", 315, 95, 75, 35, $BS_FLAT)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $BtnCancel = GUICtrlCreateButton("Cancel", 316, 147, 76, 35, $BS_FLAT)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###


    WinSetTitle($Form3, "", $settitle);ensure a change to Koda design doesn't stop script working
    $mainxy = WinGetPos($Form2)
    WinMove($Form3, "", $mainxy[0] + 20, $mainxy[1] + 20)
    ;$set = _CommSetport(1,$result,9600,8,0,1,0)
    ;help
    ;send /rcv
    ;
    $portlist = _CommListPorts(0);find the available COM ports and write them into the ports combo
    If @error = 1 Then
        MsgBox(0, 'trouble getting portlist', 'Program will terminate!')
        Exit
    EndIf


    For $pl = 1 To $portlist[0]
        GUICtrlSetData($CmboPortsAvailable, $portlist[$pl]);_CommListPorts())
    Next
    GUICtrlSetData($CmboPortsAvailable, $portlist[1]);show the first port found
    GUICtrlSetData($ComboFlow, $FlowType[$setflow])
    _GUICtrlComboBox_SetMinVisible($CmBoBaud, 10);restrict the length of the drop-down list

    $retval = 0

    While 1
        $msg = GUIGetMsg()
        If $msg = $BtnCancel Then
            If Not $mode Then $retval = -1
            ExitLoop
        EndIf


        If $msg = $BtnApply Then
            Local $sportSetError
            $comboflowsel = GUICtrlRead($ComboFlow)
            For $n = 0 To 2
                If $comboflowsel = $FlowType[$n] Then
                    $setflow = $n
                    ConsoleWrite("flow = " & $setflow & @CRLF)
                    ExitLoop
                EndIf

            Next
            $setport = StringReplace(GUICtrlRead($CmboPortsAvailable), 'COM', '')
            _CommSetPort($setport, $sportSetError, GUICtrlRead($CmBoBaud), GUICtrlRead($CmboDataBits), GUICtrlRead($CmBoParity), GUICtrlRead($CmBoStop), $setflow)
            if $sportSetError = '' Then
            MsgBox(262144, 'Connected ','to COM' & $setport)
            Else
            MsgBox(262144, 'Setport error = ', $sportSetError)
            EndIf
            $mode = 1;
            ExitLoop
        EndIf

        ;stop user switching back to $form2
        If WinActive($maintitle) Then
            ConsoleWrite('main is active' & @CRLF)
            If WinActivate($settitle) = 0 Then MsgBox(0, 'not found', $settitle)
        EndIf


    WEnd
    GUIDelete($Form3)
    WinActivate($maintitle)
    Events()
    Return $retval


EndFunc   ;==>SetPort

Share this post


Link to post
Share on other sites

To convince yourself hat a select can indeed do the job, just try this by entering phrases containing or not various keywords. Anything containing 'stop' will exit the loop.

While 1
    $instr = InputBox("Serial simulation", "Enter string as if it came from the serial device")
    If $instr <> '' Then;if we got something
        Select
            Case StringInStr($instr, 'ALARM')
                MsgBox(0, "Alarm message", $instr)
            Case StringInStr($instr, 'Trouble')
                MsgBox(0, "Trouble message", $instr)
            Case StringInStr($instr, 'SUPERVISORY')
                MsgBox(0, "Advisory message", $instr)
            Case StringInStr($instr, 'Restored')
                MsgBox(0, "Restored message", $instr)
            Case StringInStr($instr, 'STOP')
                ExitLoop
            Case Else
                MsgBox(0, "Other message", $instr)
        EndSelect

    ;following statement does not work yet
        
        ;; first parameter missing, wrong function
        
;~  if GUICtrlRead($Checkbox2) = $GUI_CHECKED Then
;~          _GuiCtrlRichEdit_StreamToFile($instr, @DesktopCommonDir & "\myfile.rtf") ; log it to a file as well ,
;~  endif

    EndIf
WEnd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

yes your code does work but the problem seems to reside in the assignment from _CommGetString to $instr when i pasted in your code and ran the program I would get a "a" in the message box followed by another msgbox with "larm" in it. See code below. One other thing is that I can't seem to find documentation on _CommGetString.I also just tried typing 4 T's into the input and I still get 2 message boxes one with t and one with ttt in it.

While 1

    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _CommGetString()

    If $instr <> '' Then;if we got something

        ;If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr,@CR,@CRLF)
        Select
            Case StringInStr($instr, 'ALARM')
                MsgBox(0, "Alarm message", $instr)
                GUICtrlSetColor($Edit1, 0xff0000)   ; Red
                GUICtrlSetData($Edit1, $instr,1)
            Case StringInStr($instr, 'Trouble')
                MsgBox(0, "Trouble message", $instr)
            Case StringInStr($instr, 'SUPERVISORY')
                MsgBox(0, "Advisory message", $instr)
            Case StringInStr($instr, 'Restored')
                MsgBox(0, "Restored message", $instr)
            Case StringInStr($instr, 'STOP')
                ExitLoop
            Case Else
                MsgBox(0, "Other message", $instr)
        EndSelect   
        #cs Select
        Case    StringInStr($instr, 'ALARM')
              MsgBox(0, "Alarm message", $instr)
              ; process alarm condition
                GUICtrlSetColor($Edit1, 0xff0000)   ; Red
                GUICtrlSetData($Edit1, $instr,1)
                SoundPlay(@WindowsDir & "\media\Whopfast.wav"); Alert user to alarm condtion can be silenced by button
                
          Case StringInStr($instr, 'Trouble')     ;; <<-- beware the case, could be important
              ;; process Trouble condition
                GUICtrlSetColor($Edit1, 0xffff00)   ; yellow
                ;SoundPlay(@WindowsDir & "\media\tada.wav",1); set this to an alert tone
                Beep(500, 1000)

          Case StringInStr($instr, 'SUPERVISORY')
              ;; process supervisory condition
                GUICtrlSetColor($Edit1, 0xffff00)   ; yellow
                GUICtrlSetData($Edit1, $instr,1)
                SoundPlay(@WindowsDir & "\media\tada.wav",1); just beep once
                
          Case StringInStr($instr, 'Restored')
              ;; process restored  condition
                GUICtrlSetColor($Edit1, 0x7FFF00)   ; Green
                GUICtrlSetData($Edit1, $instr,1) ; no sound here will drive user nutts
             
            Case Else
            GUICtrlSetColor($Edit1, 0xffffff); set text to white for all normal display
            GUICtrlSetData($Edit1, $instr,1)
            ;SoundPlay(@WindowsDir & "\media\Whopfast.wav"); just beep once
             ;SoundPlay(@WindowsDir & "\media\tada.wav",1); just beep once
             ;; received something else, comm error or unexpected response, or man-in-the-middle attack ;-)
      EndSelect 
      #ce
        ;GUICtrlSetData($Edit1, $instr,1)   
        
        ;following statement does not work yet
        ;if GUICtrlRead($Checkbox2) = $GUI_CHECKED Then 
        ;put writing to log file here
        ;endif
        
    EndIf

WEnd
Edited by rharwood

Share this post


Link to post
Share on other sites

The fact that you get strings broken in parts like 'a' then 'larm' probably has to do with your setup (two ports) and switching back and forth between the two, or something close.

To avoid such side effects, you could use a separate terminal (WindowsHyperTerminal will do) to send data thru (say) COM1, have a simple loopback from COM1 Tx to Com2 Rx (plus ground) set no handshake on both ports and receive your own input with your AutoIt program in "normal" configuration. Sometimes, more complex is simpler.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

ok last post for the night it's getting late and I got to crash , this code does seem to work as far as color is concerned, but the file output does not, anyone point out what I am doing wrong again. Note I changed _CommGetString to _CommGetLine and then the select seemed to work correctly.

While 1

    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _CommGetLine()
if GUICtrlRead($Checkbox2) = $GUI_CHECKED Then 
        $hnd = FileOpen ("NMEA.txt", 1)
                endif
    If $instr <> '' Then;if we got something

        If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr,@CR,@CRLF)
        
        Select
        Case    StringInStr($instr, 'ALARM')
                ; process alarm condition
                GUICtrlSetColor($Edit1, 0xff0000)   ; Red
                GUICtrlSetData($Edit1, $instr,1)
                FileWriteLine($hnd,$instr)
                ;SoundPlay(@WindowsDir & "\media\Whopfast.wav"); Alert user to alarm condtion can be silenced by button
                
          Case StringInStr($instr, 'Trouble')     ;; <<-- beware the case, could be important
              ;; process Trouble condition
                GUICtrlSetColor($Edit1, 0xffff00)   ; yellow
                GUICtrlSetData($Edit1, $instr,1)
                FileWriteLine($hnd,$instr)
                ;SoundPlay(@WindowsDir & "\media\tada.wav",1); set this to an alert tone
                ;Beep(500, 1000)

          Case StringInStr($instr, 'SUPERVISORY')
              ;; process supervisory condition
                GUICtrlSetColor($Edit1, 0xffff00)   ; yellow
                GUICtrlSetData($Edit1, $instr,1)
                FileWriteLine($hnd,$instr)
                ;SoundPlay(@WindowsDir & "\media\tada.wav",1); just beep once
                
          Case StringInStr($instr, 'Restored')
              ;; process restored  condition
                GUICtrlSetColor($Edit1, 0x7FFF00)   ; Green
                GUICtrlSetData($Edit1, $instr,1) ; no sound here will drive user nutts
                FileWriteline($hnd,$instr)
             
            Case Else
            GUICtrlSetColor($Edit1, 0xffffff); set text to white for all normal display
            GUICtrlSetData($Edit1, $instr,1)
            FileWriteLine($hnd,$instr)
            ;SoundPlay(@WindowsDir & "\media\Whopfast.wav"); just beep once
             ;SoundPlay(@WindowsDir & "\media\tada.wav",1); just beep once
             ;; received something else, comm error or unexpected response, or man-in-the-middle attack ;-)
      EndSelect 
      
        ;GUICtrlSetData($Edit1, $instr,1)   
        
        ;following statement does not work yet
        
        ;put writing to log file here
        
        
    EndIf

WEnd
FileClose($hnd)

Share this post


Link to post
Share on other sites

Just read your last post I am just using a standard loop back plug on my comm port (only have one comm port) but you may have something there it is possible that the send function may be doing some thing but the screen output is correct , the setdata statement I think????? but the filewrite and msgboxes don't deem to receive the correct data.

Oh well I am to tired to think right now so thanks for all the help, I'll have a go at it again when I get back to the hotel tomorrow night.

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  
Followers 0