1818import os
1919from model2450lib import searchmodel
2020from model2450lib .model2450 import Model2450
21+ import time
2122from uiGlobal import *
23+ import serial
24+ import serial .tools .list_ports
2225
2326#======================================================================
2427# COMPONENTS
2528#======================================================================
2629
30+ def send_packets_command_to_all_ports ():
31+ ports = serial .tools .list_ports .comports ()
32+ for port in ports :
33+ try :
34+ ser = serial .Serial (port .device , baudrate = 115200 , timeout = 1 )
35+ # print(f"Sending 'packets' to {port.device}")
36+ ser .write (b'packets\r \n ' )
37+ time .sleep (0.1 )
38+ ser .close ()
39+ except Exception as e :
40+ print (f"Failed to send on { port .device } : { e } " )
41+
2742class ComDialog (wx .Dialog ):
2843 """
2944 A dialog for discovering and connecting to MCCI Model2450 devices.
@@ -45,7 +60,6 @@ def __init__(self, parent):
4560 self .SetIcon (wx .Icon (os .path .join (os .path .abspath (os .path .dirname (__file__ )), "icons" , IMG_ICON )))
4661
4762 self .device = None
48-
4963 vbox = wx .BoxSizer (wx .VERTICAL )
5064 hbox = wx .BoxSizer (wx .HORIZONTAL )
5165 search_btn = wx .Button (self , label = "Search" )
@@ -56,38 +70,43 @@ def __init__(self, parent):
5670 hbox .Add (self .port_text , 1 , wx .ALL | wx .CENTER , 5 )
5771 hbox .Add (connect_btn , 0 , wx .ALL | wx .CENTER , 5 )
5872
59- self .result_text = wx .StaticText (self , label = "Device not connected" )
60-
73+ # self.result_text = wx.StaticText(self)
6174 vbox = wx .BoxSizer (wx .VERTICAL )
6275 vbox .Add (hbox , 0 , wx .EXPAND )
63- vbox .Add (self .result_text , 0 , wx .ALL , 10 )
64-
76+ # vbox.Add(self.result_text, 0, wx.ALL, 10)
6577 search_btn .Bind (wx .EVT_BUTTON , self .on_search )
6678 connect_btn .Bind (wx .EVT_BUTTON , self .on_connect )
6779 self .SetSizer (vbox )
68-
80+
6981 def on_search (self , event ):
7082 """
7183 Event handler for the Search button.
7284
73- Uses `searchmodel.get_models()` to scan for available devices,
74- then populates the ComboBox with results. Displays a message
75- if no devices are found .
85+ - Scans for available Model2450 devices.
86+ - If no devices are found initially, sends the 'packets' command to all ports.
87+ - Then scans again and updates the ComboBox with available devices .
7688 """
77- dev_list = searchmodel .get_models () # Fetch devices using the searchmodel
78- print (dev_list )
79- if dev_list and "models" in dev_list :
80- dev_list = dev_list ["models" ]
81- self .port_text .Clear () # Clear existing items in the ComboBox
82- if len (dev_list ) > 0 :
83- for dev in dev_list :
89+ def try_search_and_update ():
90+ dev_list = searchmodel .get_models ()
91+ if dev_list and "models" in dev_list and len (dev_list ["models" ]) > 0 :
92+ self .port_text .Clear ()
93+ for dev in dev_list ["models" ]:
8494 display_text = f"{ dev ['model' ]} ({ dev ['port' ]} )"
8595 self .port_text .Append (display_text )
86- self .port_text .Select (0 ) # Select the first port in the list
87- else :
88- self .result_text .SetLabel ("No devices found" )
89- else :
90- self .result_text .SetLabel ("No devices found" )
96+ self .port_text .Select (0 )
97+ # self.result_text.SetLabel("Devices found.")
98+ return True
99+ return False
100+
101+ found = try_search_and_update ()
102+ if not found :
103+ # self.result_text.SetLabel("No devices found. Sending 'packets' command...")
104+ send_packets_command_to_all_ports ()
105+ time .sleep (1.0 ) # Wait for devices to respond
106+ found = try_search_and_update ()
107+ if not found :
108+ # self.result_text.SetLabel("No devices found even after sending packets.")
109+ pass
91110
92111 def on_connect (self , event ):
93112 """
@@ -103,16 +122,17 @@ def on_connect(self, event):
103122 try :
104123 port = selection .split ('(' )[1 ].strip (')' )
105124 except IndexError :
106- self .result_text .SetLabel ("Invalid selection format." )
125+ # self.result_text.SetLabel("Invalid selection format.")
126+ pass
107127 return
108-
109128 try :
110129 self .device = Model2450 (port )
111130 self .device .connect ()
112131 sn = self .device .read_sn ()
113-
114- # Set the status bar text
115- self .GetParent ().SetStatusText (f"{ sn } " )
132+ self .device .sn = sn # Set serial number to device object
133+ self .GetParent ().SetStatusText (port , 0 )
134+ self .GetParent ().SetStatusText ("Connected" , 1 )
135+ self .GetParent ().SetStatusText (f"SN: { sn } " , 2 )
116136
117137 # Pass the connected device to the ControlPanel
118138 self .GetParent ().control_tab .set_device (self .device )
@@ -121,13 +141,15 @@ def on_connect(self, event):
121141
122142 self .EndModal (wx .ID_OK ) # Close the dialog with success
123143 # Display a popup dialog confirming the connection
124- wx .MessageBox (f"Device connected successfully. \n Serial Number: { sn } " ,
144+ wx .MessageBox (f"Device Connected Successfully. \n { sn } " ,
125145 "Connection Successful" , wx .OK | wx .ICON_INFORMATION )
126146
127147 # Close the dialog after successful connection
128148 self .Close ()
129149 self .EndModal (wx .ID_OK ) # Close the dialog with success
130150 except Exception as e :
131- self .result_text .SetLabel (f"Connection failed: { str (e )} " )
151+ # self.result_text.SetLabel(f"Connection failed: {str(e)}")
152+ pass
132153 else :
133- self .result_text .SetLabel ("Please select a device from the list." )
154+ # self.result_text.SetLabel("Please select a device from the list.")
155+ pass
0 commit comments