Sign in to follow this  
Followers 0
enaiman

Question about Edit control ...

7 posts in this topic

I'm working on a serial communication between my computer and a switch over COM1.

I'm using a big edit control for displaying the sent and received text.

It works fine except when I need to receive like 100 characters in 50 seconds (the time between characters is around 0.5 sec) - e.g. when I'm performing a save and the switch shows the progress by sending 1 ">" character every .5 sec.

The problem is that the only way to refresh the information in the edit box is to read content & add received characters & set control data using the new string. It can handle that but it looks like an inefficient way to do it and i can see some "flickering" during this process.

Question: Is there another way to "add" text to an edit without using GUICtrlSetData?

Here is the code so far:

#include <GUIConstants.au3>
#include <commmg.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)
$Form1_1 = GUICreate("Form1", 705, 468, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_1Close")
$log = GUICtrlCreateEdit("", 16, 24, 671, 330, BitOR($ES_MULTILINE,$ES_WANTRETURN, $ES_READONLY,$WS_VSCROLL,$WS_BORDER))
GUICtrlSetFont(-1, 10, 400, 0, "Courier")
GUICtrlSetColor(-1, 0x00FF00)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetOnEvent(-1, "logChange")
$cur_line = GUICtrlCreateEdit("", 16, 352, 671, 25, BitOR($ES_AUTOVSCROLL,$ES_WANTRETURN,$ES_AUTOHSCROLL,$WS_BORDER))
GUICtrlSetFont(-1, 10, 400, 0, "Courier")
GUICtrlSetColor(-1, 0x00FF00)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetOnEvent(-1, "cur_lineChange")
$Button1 = GUICtrlCreateButton("Connect COM", 16, 416, 129, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click")
GUISetState(@SW_SHOW)
Dim $errstr=""
    _CommSetPort(1,$errstr,9600,8,0,1,0)
    _CommPortConnection()
While 1
    Sleep(100)
    $temp = _Commgetstring()
    If $temp <>"" Then
        If StringInStr ($temp, @LF&@CR)  Then   ;just string formating ... I can't handle StringRegExp yet ...
            $temp = StringReplace($temp, @LF&@CR, @LF)
        EndIf
        If StringInStr ($temp, @LF&@LF)  Then
            $temp = StringReplace($temp, @LF&@LF, @LF)
        EndIf
        $content = GUICtrlRead($log)   ;here starts all ...
        $content &= $temp
        GUICtrlSetData($log, $content)
        _GUICtrlEditLineScroll($log, 0, _GUICtrlEditGetLineCount($log))
        $temp = ""
    EndIf
    If StringInStr(GUICtrlRead($cur_line), @CR) Then
        _CommSendString(StringStripCR(GUICtrlRead($cur_line)),0)
        GUICtrlSetData($cur_line, "")
    EndIf
WEnd

Func Button1Click()
    _CommSendString(@CR,0)
    Sleep(2000)
    $temp = _Commgetstring()
    GUICtrlSetData($log,$temp)
EndFunc
Func cur_lineChange()
    
EndFunc
Func Form1_1Close()
    Exit
EndFunc
Func logChange()

EndFunc

A big "Thanks" for any help :)


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well the only way I know of is like as follows:

Func _GUICtrlEditAddText ($ctrlid, $text)
    $oldtext = GUICtrlRead ($ctrlid)
    $newtext = GUICtrlSetData ($ctrlid, $oldtext & $text)
    If $newtext = 1 Then
        Return 0
    ElseIf $newtext = 0 Then
        Return 1
    ElseIf $oldtext = 0 Then
        Return 2
EndFunc

Returns 0 on success, 1 on failure on adding it, and 2 if read fails...

Would control send work?

$content = GUICtrlRead($log)  ;here starts all ...
        ControlSend ("", "", $log, $temp, 1)
        _GUICtrlEditLineScroll($log, 0, _GUICtrlEditGetLineCount($log))
        $temp = ""

Just a thought...

Edited by Bert

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

probably earlier examples by others on the forum but I found this solution by

Saunders and Arcker in this post

Append text to the last line of an editbox, without scrolling down to it

always pastes text to end of last line, auto scrolls to last line

#include <GUIConstants.au3>
#include <commmg.au3>
#include <GuiEdit.au3>

#include <A3LWinApi.au3> ; need AutoLib3 for _API_SendMessage and constants
;Global Const $WM_GETTEXTLENGTH = 0x000E
;Global Const $EM_SETSEL = 0xB1

Opt("GUIOnEventMode", 1)
$Form1_1 = GUICreate("Form1", 705, 468, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_1Close")
$log = GUICtrlCreateEdit("", 16, 24, 671, 330, BitOR($ES_MULTILINE,$ES_WANTRETURN, $ES_READONLY,$WS_VSCROLL,$WS_BORDER))
GUICtrlSetFont(-1, 10, 400, 0, "Courier")
GUICtrlSetColor(-1, 0x00FF00)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetOnEvent(-1, "logChange")
$cur_line = GUICtrlCreateEdit("", 16, 352, 671, 25, BitOR($ES_AUTOVSCROLL,$ES_WANTRETURN,$ES_AUTOHSCROLL,$WS_BORDER))
GUICtrlSetFont(-1, 10, 400, 0, "Courier")
GUICtrlSetColor(-1, 0x00FF00)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetOnEvent(-1, "cur_lineChange")
$Button1 = GUICtrlCreateButton("Connect COM", 16, 416, 129, 25, 0)
GUICtrlSetOnEvent(-1, "Button1Click")
GUISetState(@SW_SHOW)
Dim $errstr=""
    _CommSetPort(1,$errstr,9600,8,0,1,0)
    _CommPortConnection()
While 1
    Sleep(100)
    $temp = _Commgetstring()
    If $temp <>"" Then
        If StringInStr ($temp, @LF&@CR)  Then   ;just string formating ... I can't handle StringRegExp yet ...
            $temp = StringReplace($temp, @LF&@CR, @LF)
        EndIf
        If StringInStr ($temp, @LF&@LF)  Then
            $temp = StringReplace($temp, @LF&@LF, @LF)
        EndIf
         ;------------------------------------------------
         $end = StringLen(GUICtrlRead($log))
         _GUICtrlEditSetSel ($log, $end, $end) ; code idea by Saunders
         ; either one of these next two lines work
         _RichEdit_AddLine($log, $temp&@LF, False)
         ;ControlCommand ($Form1_1, "", $log, "EditPaste", $temp&@LF) ; My suggestion, seems to work same as RichEdit_AddLine
         ;------------------------------------------------
        $temp = ""
    EndIf
    If StringInStr(GUICtrlRead($cur_line), @CR) Then
        _CommSendString(StringStripCR(GUICtrlRead($cur_line)),0)
        GUICtrlSetData($cur_line, "")
    EndIf
WEnd

Func Button1Click()
    _CommSendString(@CR,0)
    Sleep(2000)
    $temp = _Commgetstring()
    GUICtrlSetData($log,$temp)
EndFunc
Func cur_lineChange()
   
EndFunc
Func Form1_1Close()
    Exit
EndFunc
Func logChange()

EndFunc


; Function from A3LRichEdit.au3 by Yoan Roblet (Arcker)
;=================================================================================================
; Description ..: Append some text to the control
; Parameters ...: $hWnd         - Handle to the control
;                 $iText        - Text to append
;                 $hLine         - insert the @crlf at the end (default = True)
; Return values : Returns the index of the first new image if successful, or -1 otherwise
; Author .......: Yoan Roblet (Arcker)
; Notes ........:
; =================================================================================================
Func _RichEdit_AddLine($hWnd, $iText, $hLine = True)
    Local $hTextLengh
    $hTextLengh = _API_SendMessage ($hWnd, $WM_GETTEXTLENGTH, 0, 0);
    _API_SendMessage ($hWnd, $EM_SETSEL, $hTextLengh, $hTextLengh);
    If $hLine Then
        _GUICtrlEditReplaceSel($hWnd, 1, $iText & @CRLF);
    Else
        _GUICtrlEditReplaceSel($hWnd, 1, $iText);
    EndIf
EndFunc   ;==>_RichEdit_AddLine
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

The problem is that the only way to refresh the information in the edit box is to read content & add received characters & set control data using the new string. It can handle that but it looks like an inefficient way to do it and i can see some "flickering" during this process.

Question: Is there another way to "add" text to an edit without using GUICtrlSetData?

If you include guiedit.au3 then the function _GUICtrlEditReplaceSel can be used to insert text at the cursor, so you just need to ensure the cursor is at the end of the text.

BTW you have a line

_CommPortConnection()

This function returns the com port currently connected but since you are doing nothing with it it might as well be removed.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thank you very much for help.

Haven't tested yet anything but I'll do it soon.

Thanks,

Tested rover's solution - it works fine after I've got rid of @LF

_RichEdit_AddLine($log, $temp, False)

The previous version actually added an @LF to any character received over COM1.

@martin - thank you for pointing out about _CommPortConnection() - indeed it does nothing in this test version and I've included it by mistake.

Thank you gentlemen for all your help :)

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

should be able to cut out a lot of the code with the latest beta, look at _GUICtrlEdit_AppendText in the beta


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

should be able to cut out a lot of the code with the latest beta, look at _GUICtrlEdit_AppendText in the beta

Thanks - I will check it out :)


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

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