deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

This app is used to just list the COM ports that are available. I cant get it to display the list of COM ports.
use strict; use Win32::SerialPort; use Win32::GUI(); my $comport = 1; my @list; #my $comlist; ######################## # Main Loop ######################## my $main = Win32::GUI::Window->new( -name => 'Main', -text => 'Serial Ports', -width => 110, -height => 175, ); my $desk = Win32::GUI::GetDesktopWindow(); my $dw = Win32::GUI::Width($desk); my $dh = Win32::GUI::Height($desk); my $x = $dw / 4 * 3.25; my $y = $dh / 4 * 2.5; $main->Move($x, $y); my $icon = new Win32::GUI::Icon('serialport.ico'); my $ni = $main->AddNotifyIcon( -name => "NI", -icon => $icon, -tip => "SerialPort Finder" ); $main->AddLabel (-text => 'COM Ports Active:'); my $comfield = $main->AddRichEdit( -name => "comfield", -left => 5, -top => 20, -text => "", -width => 100, -height => 100, -readonly => 1 ); $main->AddTimer( "com_list", 1000); Win32::GUI::Dialog(); ######################## # Main Window Subs ######################## sub Main_Terminate { return -1; #$main->Disable(); #$main->Hide(); #return 1; } sub Main_Minimize { $main->Disable(); $main->Hide(); return 1; #return -1; } ######################## # System Tray Events ######################## sub NI_Click { &serial_finder; #&label_create; $main->Enable(); $main->Show(); return 1; } ######################## # Make labels for # found COM ports ######################## sub com_list_Timer { print "entered\n"; &com_list_create; } sub com_list_create { print "running\n"; foreach my $comlist (@list) { print $comlist,"\n"; $comfield->SetFocus(); $comfield->ReplaceSel("COM$comlist",1); $comfield->Select(999999,999999); } } ######################## sub serial_finder { my @list; $comport=1; do { my $port = new Win32::SerialPort("COM$comport"); if (defined ($port)) { push(@list,$comport); $port->close; } $comport++; } while ($comport < 20); foreach my $comlist (@list) { print $comlist,"\n"; } }

Replies are listed 'Best First'.
Re: Win32::GUI COM Port app
by BrowserUk (Patriarch) on Aug 06, 2008 at 00:28 UTC
      The ports are returned correctly but when the tray is right clicked the widget is suppose to be populated by a list of the COM ports, but there is nothing.

        The array you populate in serial_finder() is private to that sub:

        sub serial_finder { my @list;

        The array you attempt to populate the edit field from is declared at the top of the program:

        my $comport = 1; my @list; ... sub com_list_create { print "running\n"; foreach my $comlist (@list) { ...

        Two different lists! The first is thrown away when you exit the sub because you never return it anywhere. You could just drop the my on the first one and they would then both use the same list and things would probably work.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.