Page 1 of 1

Startup Flash during initialization

Posted: Fri Dec 13, 2024 4:17 am
by dfw1417
I have been unable to figure out how to eliminate the initial flashing of the Window creation during the procedures run at startup of the program I use for finding all the valid com ports on a computer. I check 32 comports and store either a 1 for valid or a 0 for invalid for the com ports. I have included the code I use, I know there is a command IsWindow but have not figured out how to apply it to my code to eliminate the initial flashing. Any help from others with more experience(most) would be appreciated.

Code: Select all

;12122024 - Find Valid Computer Com Ports 
;Tests Com Ports 1 to 32
;
;Compiler Directtives
EnableExplicit
;----- Variables
Global Event.i
Global Result.i
Global CP.s
;----- Array Variables
Global Dim VCP.i(32)		  ;Status Serial Port Numbers 1 = Valid 0 = Not Valid

Procedure Find_Valid_Com_Ports()
 ;======================================================== 
 ;PROCEDURE DESCRIPTION 
 ;Checks 31 Comports on Computer and mark them Valid if so.
 ;======================================================== 
Protected p.i:Protected s0.s
For p = 0 To 31
    s0 = "COM" + Str(p)
    If OpenSerialPort(#PB_Any, s0, 38400, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)      ;Open Serial Port		
     VCP(p) = 1   ;Valid 		
   Else    
     VCP(p) = 0   ;Not Valid
  EndIf
  CloseSerialPort(#PB_All)
Next 
EndProcedure

Procedure AddPorts2List()
 ;======================================================== 
 ;PROCEDURE DESCRIPTION 
 ;Makes a list of valid com ports available on computer
 ;======================================================== 
  Protected p.i
	For p = 0 To 32
   If VCP(p) = 1
   		AddGadgetItem(1105, -1, "COM" + Str(p)  )
   EndIf
  Next
EndProcedure

Procedure Load_Default_Fonts()
 Protected DFID.i                                          ;Status 
 DFID = LoadFont(0, "Arial Bold", 8)                        ;Font ID Default Font
 SetGadgetFont(#PB_Default, DFID)                           ;Assign Default Font to Gadgets - Note Cannot use SetGadgetFont later or wont work for that gadget. 
EndProcedure

;----- Example Use:
Load_Default_Fonts():InitMouse()
 If OpenWindow(0, #PB_Ignore, #PB_Ignore, 1024, 768, "Find Valid Com Port Procedure Test" , #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
   SetWindowColor(0, $9BB1C8);Tan            ;Initialize Mouse
   FrameGadget(1101, 15, 10, 150, 110, "")
   TextGadget(1102, 20, 10, 100, 25, " Com Settings")
   SetGadgetColor(1102, #PB_Gadget_BackColor, $9BB1C8)
   TextGadget(1103, 35, 48, 70, 70, " Com Port")
   SetGadgetColor(1103, #PB_Gadget_BackColor, $9BB1C8)
   ComboBoxGadget(1105, 100, 44, 60, 21, #PB_ComboBox_UpperCase )
   Find_Valid_Com_Ports()
   AddPorts2List()
 EndIf
 
 Repeat
   Event = WaitWindowEvent(20)
   If Event = #PB_Event_Gadget   
     Result = EventGadget()
       ;----- Com Selected by User                          - VF 
     If Result = 1105
       CP = GetGadgetText(1105)
       MessageRequester("Com Port Selected: ", "Port Selected is: " + CP, #PB_MessageRequester_Ok)
     EndIf
   EndIf
 Until Event = #PB_Event_CloseWindow

Re: Startup Flash during initialization

Posted: Fri Dec 13, 2024 8:06 am
by infratec
No flicker:

Code: Select all

;12122024 - Find Valid Computer Com Ports 
;Tests Com Ports 1 to 32
;
;Compiler Directtives
EnableExplicit

Enumeration Gadgets
  #Frame
  #Text1
  #Text2
  #Combo
EndEnumeration


Procedure Find_Valid_Com_Ports(List VCP.i())
  ;======================================================== 
  ;PROCEDURE DESCRIPTION 
  ;Checks 31 Comports on Computer and mark them Valid if so.
  ;======================================================== 
  Protected.i p, com
  
  For p = 1 To 31
    com = OpenSerialPort(#PB_Any, "COM" + Str(p), 38400, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1, 1)      ;Open Serial Port		
    If com
      AddElement(VCP())
      VCP() = p
      CloseSerialPort(com)
    EndIf
  Next 
EndProcedure

Procedure AddPorts2List(List VCP.i())
  ;======================================================== 
  ;PROCEDURE DESCRIPTION 
  ;Makes a list of valid com ports available on computer
  ;======================================================== 
  ForEach VCP()
    AddGadgetItem(#Combo, -1, "COM" + Str(VCP()))
  Next
EndProcedure

Procedure Load_Default_Fonts()
  Protected DFID.i                                          ;Status 
  DFID = LoadFont(#PB_Any, "Arial Bold", 8)                 ;Font ID Default Font
  SetGadgetFont(#PB_Default, FontID(DFID))                  ;Assign Default Font to Gadgets - Note Cannot use SetGadgetFont later or wont work for that gadget. 
EndProcedure

;----- Example Use:

Define.i Event, Result, Exit
Define CP$
NewList VCP.i()

Load_Default_Fonts()
If OpenWindow(0, 0, 0, 1024, 768, "Find Valid Com Port Procedure Test" , #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  SetWindowColor(0, $9BB1C8);Tan            ;Initialize Mouse
  FrameGadget(#Frame, 15, 10, 150, 110, "")
  TextGadget(#Text1, 20, 10, 80, 25, "Com Settings", #PB_Text_Center)
  SetGadgetColor(#Text1, #PB_Gadget_BackColor, $9BB1C8)
  TextGadget(#Text2, 35, 48, 50, 20, "Com Port")
  SetGadgetColor(#Text2, #PB_Gadget_BackColor, $9BB1C8)
  ComboBoxGadget(#Combo, 100, 44, 60, 21);, #PB_ComboBox_UpperCase)
  Find_Valid_Com_Ports(VCP())
  AddPorts2List(VCP())
EndIf

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_CloseWindow
      Exit = #True
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Combo
          ;----- Com Selected by User                          - VF 
          CP$ = GetGadgetText(#Combo)
          Debug "Port Selected is: " + CP$
      EndSelect
  EndSelect
Until Exit
But you can avoid any flicker by open the window hidden and show it after all gadget stuff is done.

Btw.

SetFont needs FontId()
Avoid globals
Close only what you have open
Use enumerations to get 'names' and not numbers
Why a timeout in WaitWindowEvent()?
...

Re: Startup Flash during initialization

Posted: Fri Dec 13, 2024 8:50 am
by HeX0R
There is a better way to find available com ports => viewtopic.php?p=515759#p515759

Re: Startup Flash during initialization

Posted: Fri Dec 13, 2024 8:54 am
by infratec
HeX0R wrote: Fri Dec 13, 2024 8:50 am There is a better way to find available com ports => viewtopic.php?p=515759#p515759
Maybe for windows, but in linux and macOS (check /dev/ttyS and ttyUSB ...) it tooks much longer time.
I still also prefer this 'brutforce' way, because it also avoid showing 'already in use' ports.

Re: Startup Flash during initialization

Posted: Fri Dec 13, 2024 10:35 am
by HeX0R
That might be true, but under Windows not all serial adapters are named COMxx, how would you find them with a bruteforce approach?