Announcement

Collapse
No announcement yet.

PBForms&controll algorithm

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBForms&controll algorithm

    Dear PB fans, please could you advise me how to join - enable the transfer of values of process variables between the graphical interface created with PBform2.0 and already written and verified basic program for control of laboratory chemical reactor which should operate for hours. The graphical interface should show what is happening inside the reactor and enable me to change the desired operating parameters. I would like to know if it is possible to insert old modified code (partially rewritten due to new reactor set-up) into/beside the code generated by PBForms using PBwin10. I will be very thankful for any suggestions/examples. The attached figure shows the created interface. Click image for larger version

Name:	SIM_TBR.jpg
Views:	107
Size:	70.1 KB
ID:	782503 Best regards, Gorazd

  • #2
    As PBForms only codes for the dialog and controls, and a skeleton callback; not only can you insert code, you must insert the operation code.

    See comments at beginning of PBForms generated source code about how the "#PBFORMS" lines mark where to (and not to) add code.

    Cheers,
    Dale

    Comment


    • #3
      Howdy, Gorazd!

      Typically, algorithms are started either by the user or automatically in sections of your code, such as these approaches:

      1. Under the %WM_InitDIalog section of the Callback function, call the function(s) that begins your algorithms
      2. Have a button that the users presses to begin you algorithms
      3. Create a timer, where the algorithm code is called out under the %WM_Timer section of the Callback function.

      If the approach is to run long-running algorithms in the background, where you want the dialog to be responsive to user inputs, the algorithm code probably needs to be run in a thread.function.

      So, yes, you can insert your algorithm into code generated by PBForms, within the constraint (that Dale points out) that PBForms requires that you do not place the code in certain areas. It has that constraint because it may re-generate those sections of code, overwriting your own code.

      Comment


      • #4
        What others already have said - first textblock in PBForms code explains. To get you started - here a sample using a timer to check a checkbox status and set textbox text according to if it's checked or not. All added code exist in CALLBACK FUNCTION ShowDIALOG1Proc, first a variable for check results is declared, then timer code between '---------- .. etc. has been added. Click the checkbox and see text in textbox change depending on checked / not checked.
        Code:
        #PBFORMS CREATED V2.01
        '------------------------------------------------------------------------------
        ' The first line in this file is a PB/Forms metastatement.
        ' It should ALWAYS be the first line of the file. Other
        ' PB/Forms metastatements are placed at the beginning and
        ' end of "Named Blocks" of code that should be edited
        ' with PBForms only. Do not manually edit or delete these
        ' metastatements or PB/Forms will not be able to reread
        ' the file correctly.  See the PB/Forms documentation for
        ' more information.
        ' Named blocks begin like this:    #PBFORMS BEGIN ...
        ' Named blocks end like this:      #PBFORMS END ...
        ' Other PB/Forms metastatements such as:
        '     #PBFORMS DECLARATIONS
        ' are used by PB/Forms to insert additional code.
        ' Feel free to make changes anywhere else in the file.
        '------------------------------------------------------------------------------
        
        #COMPILE EXE
        #DIM ALL
        
        '------------------------------------------------------------------------------
        '   ** Includes **
        '------------------------------------------------------------------------------
        #PBFORMS BEGIN INCLUDES
        #RESOURCE "PbFormsTest.pbr"
        %USEMACROS = 1
        #INCLUDE ONCE "WIN32API.INC"
        #INCLUDE ONCE "COMMCTRL.INC"
        #INCLUDE ONCE "PBForms.INC"
        #PBFORMS END INCLUDES
        '------------------------------------------------------------------------------
        
        '------------------------------------------------------------------------------
        '   ** Constants **
        '------------------------------------------------------------------------------
        #PBFORMS BEGIN CONSTANTS
        %IDD_DIALOG1   =  101
        %IDC_GRAPHIC1  = 1001
        %IDC_LABEL1    = 1002
        %IDC_CHECKBOX1 = 1003
        %IDC_TEXTBOX1  = 1004
        #PBFORMS END CONSTANTS
        %ID_Timer      = 5000
        '------------------------------------------------------------------------------
        
        '------------------------------------------------------------------------------
        '   ** Declarations **
        '------------------------------------------------------------------------------
        DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
        DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
        #PBFORMS DECLARATIONS
        '------------------------------------------------------------------------------
        
        '------------------------------------------------------------------------------
        '   ** Main Application Entry Point **
        '------------------------------------------------------------------------------
        FUNCTION PBMAIN()
            PBFormsInitComCtls (%ICC_WIN95_CLASSES OR %ICC_DATE_CLASSES OR _
                %ICC_INTERNET_CLASSES)
        
            ShowDIALOG1 %HWND_DESKTOP
        END FUNCTION
        '------------------------------------------------------------------------------
        
        '------------------------------------------------------------------------------
        '   ** CallBacks **
        '------------------------------------------------------------------------------
        CALLBACK FUNCTION ShowDIALOG1Proc()
            LOCAL lRes AS LONG  '< declare a long variable for checkbox check and whatever purpose
        
            SELECT CASE AS LONG CB.MSG
                CASE %WM_INITDIALOG
                    '------------------------------------------------------------------------
                    ' Initialization handler
                    SetTimer CB.HNDL, %ID_Timer, 20, 0  ' Create a Timer for continous action
        
                CASE %WM_TIMER  ' < this one will be called every 20 ms
                    SLEEP 0     ' < to allow other threads some time to work too
                    CONTROL GET CHECK CB.HNDL, %IDC_CHECKBOX1 TO lRes
                    IF lRes THEN CONTROL SET TEXT CB.HNDL, %IDC_TEXTBOX1, TIME$
        
                CASE %WM_DESTROY
                    KillTimer CB.HNDL, %ID_Timer  ' < must kill timers at exit
                    '------------------------------------------------------------------------
        
                CASE %WM_NCACTIVATE
                    STATIC hWndSaveFocus AS DWORD
                    IF ISFALSE CB.WPARAM THEN
                        ' Save control focus
                        hWndSaveFocus = GetFocus()
                    ELSEIF hWndSaveFocus THEN
                        ' Restore control focus
                        SetFocus(hWndSaveFocus)
                        hWndSaveFocus = 0
                    END IF
        
                CASE %WM_COMMAND
                    ' Process control notifications
                    SELECT CASE AS LONG CB.CTL
                        CASE %IDC_CHECKBOX1
        
                        CASE %IDC_TEXTBOX1
        
                    END SELECT
            END SELECT
        END FUNCTION
        '------------------------------------------------------------------------------
        
        '------------------------------------------------------------------------------
        '   ** Dialogs **
        '------------------------------------------------------------------------------
        FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
            LOCAL lRslt  AS LONG
        
        #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
            LOCAL hDlg   AS DWORD
            LOCAL hFont1 AS DWORD
        
            DIALOG NEW hParent, "Trickle bed reactor", 189, 50, 390, 317, %WS_POPUP _
                OR %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
                %WS_MINIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME _
                OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, _
                %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
                %WS_EX_RIGHTSCROLLBAR, TO hDlg
            CONTROL ADD GRAPHIC,  hDlg, %IDC_GRAPHIC1, "", 5, 5, 199, 274, %WS_CHILD _
                OR %WS_VISIBLE, %WS_EX_CLIENTEDGE
            CONTROL ADD LABEL,    hDlg, %IDC_LABEL1, "Operating parameters", 215, 5, _
                150, 10, %WS_CHILD OR %WS_VISIBLE OR %SS_CENTER, %WS_EX_LEFT OR _
                %WS_EX_LTRREADING
            CONTROL ADD CHECKBOX, hDlg, %IDC_CHECKBOX1, "Operating pressure", 215, _
                25, 80, 10
            CONTROL ADD TEXTBOX,  hDlg, %IDC_TEXTBOX1, "<< Click Op_pressure", 295, _
                25, 80, 13
        
            FONT NEW "MS Sans Serif", 8, 1, %ANSI_CHARSET TO hFont1
        
            CONTROL SET FONT hDlg, %IDC_LABEL1, hFont1
        #PBFORMS END DIALOG
        
            DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
        
        #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
            FONT END hFont1
        #PBFORMS END CLEANUP
        
            FUNCTION = lRslt
        END FUNCTION
        '------------------------------------------------------------------------------

        Comment


        • #5
          This demonstrates saving all 43 data control values to an array.
          It creates the controls manually outside of the PBForms control blocks.
          It is not a typical PBForms program, but sure makes getting/saving easy.

          The data array is only needed for getting/putting to disk.
          It is not being updated in real-time.
          The 43 control values are placed into array and saved when dialog is closed.
          FOR x = 1 TO %NumberOfControls
          CONTROL GET TEXT ghDlg,%TEXT_TEXTBOX1 +x-1 TO MyData(x)
          NEXT



          Code:
          #PBFORMS CREATED V2.01
          
          #COMPILE EXE
          #DIM ALL
          %KillAll = 0
          $FileName = "MyData"
          %NumberOfControls=43 'number of controls
          GLOBAL ghDlg    AS DWORD
          GLOBAL MyData() AS STRING
          
          #PBFORMS BEGIN INCLUDES
          #INCLUDE ONCE "WIN32API.INC"
          #PBFORMS END INCLUDES
          
          #PBFORMS BEGIN CONSTANTS
          %TEXT_TEXTBOX1 = 1001
          
          #PBFORMS END CONSTANTS
          
          DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
          DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
          #PBFORMS DECLARATIONS
          
          FUNCTION PBMAIN()
           DIM MyData(1 TO %NumberOfControls)
           IF %KillAll THEN KILL $FileName
           GetArrayFromDisk
           ShowDIALOG1 %HWND_DESKTOP
          END FUNCTION
          
          CALLBACK FUNCTION ShowDIALOG1Proc()
          
            SELECT CASE AS LONG CB.MSG
              CASE %WM_INITDIALOG
                ' Initialization handler
          
              CASE %WM_NCACTIVATE
                STATIC hWndSaveFocus AS DWORD
                IF ISFALSE CB.WPARAM THEN
                  ' Save control focus
                  hWndSaveFocus = GetFocus()
                ELSEIF hWndSaveFocus THEN
                  ' Restore control focus
                  SetFocus(hWndSaveFocus)
                  hWndSaveFocus = 0
                END IF
          
              CASE %WM_CLOSE
                PutControlsToDisk
          
              CASE %WM_COMMAND
                SELECT CASE AS LONG CB.CTL
                  CASE %TEXT_TEXTBOX1
                END SELECT
            END SELECT
          END FUNCTION
          
          FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
            LOCAL x AS LONG
            LOCAL lRslt AS LONG
          
          #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
            LOCAL hDlg  AS DWORD
          
            DIALOG NEW hParent, "43 controls", 407, 22, 500, 561, %WS_POPUP OR _
              %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
              %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR _
              %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, _
              %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
              %WS_EX_RIGHTSCROLLBAR, TO hDlg
          #PBFORMS END DIALOG
            ghDlg = hDlg
            CreateControls
            DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
          
          #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
          #PBFORMS END CLEANUP
          
            FUNCTION = lRslt
          END FUNCTION
          
          FUNCTION GetArrayFromDisk AS LONG
           LOCAL x AS LONG
          
           IF ISFILE($Filename) = 0 THEN
            FOR x = 1 TO %NumberOfControls
             MyData(x) = USING$("Control #",x)
            NEXT
            EXIT FUNCTION 'no data
           END IF
          
           LOCAL hFile AS LONG
           hFile=FREEFILE
           OPEN $FileName FOR BINARY AS hFile
           GET #hFile,1,MyData()
           IF ERR THEN ? "Get error" + STR$(ERR)
           CLOSE #hFile
          END FUNCTION
          
          FUNCTION PutControlsToDisk AS LONG
           LOCAL x AS LONG
           LOCAL hfile AS LONG
           hFile = FREEFILE
           FOR x = 1 TO %NumberOfControls
            CONTROL GET TEXT ghDlg,%TEXT_TEXTBOX1 +x-1 TO MyData(x)
           NEXT
           KILL $FileName  'added this later
           OPEN $FileName FOR BINARY AS hFile
           PUT #hFile,1,MyData()
           IF ERR THEN ? "Put error" + STR$(ERR)
           CLOSE #hFile
          END FUNCTION
          
          SUB CreateControls
           LOCAL x AS LONG
           FOR x = 1 TO %NumberOfControls
            CONTROL ADD TEXTBOX, ghDlg, %TEXT_TEXTBOX1+x-1, MyData(x), 5, 12*x, 300, 13
           NEXT
          END SUB
          Last edited by Mike Doty; 2 Jul 2019, 01:07 PM. Reason: KILL $FileName 'added this later
          https://duckduckgo.com instead of google

          Comment


          • #6
            Dear Borje, Gary, Mike, and Dale, thank you very much for prompt and illustrative instructions. I hope that, due to your help, I see the light at the end of the current PB programming tunnel.

            Comment

            Working...
            X