@@ -15,6 +15,8 @@ public class Main implements ActionListener {
1515 private Color background = new Color (28 , 28 , 31 );
1616 private final String primaryFont = "Stencil" ;
1717 private Float frequency = 89.9f ;
18+ private String commName = "COM6" ;
19+ private boolean comboBoxesPopulated = false ;
1820
1921 // Declare the components as private instance variables
2022 private JFrame frame ;
@@ -33,14 +35,19 @@ public class Main implements ActionListener {
3335 private TargetDataLine microphone ;
3436 private SourceDataLine speakers ;
3537 private final AudioFormat format = new AudioFormat (44100 , 16 , 2 , true , true );
38+ private JComboBox <String > portComboBox ;
3639 private JComboBox <String > inputComboBox ;
3740 private JComboBox <String > outputComboBox ;
3841
3942 public Main () {
4043 // Set up serial communication
44+ for (SerialPort port : SerialPort .getCommPorts ()) {
45+ System .out .println (port .getSystemPortName ());
46+ }
4147 System .out .println (Arrays .toString (SerialPort .getCommPorts ()));
4248
4349 sp = SerialPort .getCommPort ("COM6" );
50+ System .out .println (sp .getPortDescription ());
4451
4552 sp .setComPortParameters (9600 ,8 ,1 ,0 );
4653 sp .setComPortTimeouts (SerialPort .TIMEOUT_WRITE_BLOCKING |SerialPort .TIMEOUT_READ_BLOCKING ,10000 ,10000 );
@@ -127,18 +134,26 @@ public Main() {
127134 // Set up the right panel and add it to the main panel
128135 rightPanel = new JPanel ();
129136 rightPanel .setBorder (BorderFactory .createEmptyBorder (5 , 5 , 5 , 5 ));
130- rightPanel .setLayout (new GridLayout (4 , 1 )); // Adding grid layout to accommodate combo boxes
137+ rightPanel .setLayout (new GridLayout (6 , 1 )); // Adding grid layout to accommodate combo boxes
131138 rightPanel .setBackground (background );
132139 mainPanel .add (rightPanel );
133140
134141 // Create the combo boxes for input and output device selection
142+ portComboBox = new JComboBox <>();
143+ portComboBox .addActionListener (this );
135144 inputComboBox = new JComboBox <>();
136145 outputComboBox = new JComboBox <>();
137146
138147 // Populate the combo boxes with available mixers
139148 populateComboBoxes ();
140149
141150 // Add the combo boxes to the right panel
151+ JLabel portLabel = new JLabel ("Select port:" );
152+ portLabel .setForeground (Color .WHITE );
153+ portLabel .setFont (new Font (primaryFont , Font .PLAIN , 14 ));
154+ rightPanel .add (portLabel );
155+ rightPanel .add (portComboBox );
156+
142157 JLabel inputDeviceLabel = new JLabel ("Select Input Device:" );
143158 inputDeviceLabel .setForeground (Color .WHITE );
144159 inputDeviceLabel .setFont (new Font (primaryFont , Font .PLAIN , 14 ));
@@ -195,13 +210,26 @@ private void populateComboBoxes() {
195210 }
196211 }
197212
213+ for (SerialPort port : SerialPort .getCommPorts ()) {
214+ portComboBox .addItem (port .getSystemPortName ());
215+ }
216+
198217 // Set default selections (system default)
199218 try {
200219 inputComboBox .setSelectedItem ("Microphone (High Definition Audio Device)" );
201220 } catch (Exception e ) {
202221 inputComboBox .setSelectedItem (AudioSystem .getMixer (AudioSystem .getMixerInfo ()[0 ]).getMixerInfo ().getName ());
203222 }
223+
224+ try {
225+ portComboBox .setSelectedItem (commName );
226+ } catch (Exception e ) {
227+ System .out .println ("Default port not available." );
228+ }
229+
204230 outputComboBox .setSelectedItem (AudioSystem .getMixer (AudioSystem .getMixerInfo ()[0 ]).getMixerInfo ().getName ());
231+
232+ comboBoxesPopulated = true ;
205233 }
206234
207235 private void setupAudioLoopback () {
@@ -311,7 +339,29 @@ public void actionPerformed(ActionEvent e) {
311339 frequencyInput .setText ("" );
312340 } catch (Exception ex ) {
313341 frequencyInput .setText ("Invalid" );
342+ System .out .println ("Exception while setting frequency: " + ex .getMessage ());
343+ }
344+ } else if (Objects .equals (e .getActionCommand (), "comboBoxChanged" )
345+ && e .getSource () == portComboBox && comboBoxesPopulated ) {
346+ try {
347+ System .out .println (e .getSource () == portComboBox );
348+ commName = (String ) portComboBox .getSelectedItem ();
349+ sp .closePort ();
350+ SerialPort new_sp = SerialPort .getCommPort (commName );
351+ assert new_sp != null ;
352+ if (!new_sp .openPort ()) {
353+ System .out .println ("\n COM port not available\n " );
354+ return ;
355+ }
356+ new_sp .setComPortParameters (9600 , 8 , 1 , 0 );
357+ new_sp .setComPortTimeouts (SerialPort .TIMEOUT_WRITE_BLOCKING | SerialPort .TIMEOUT_READ_BLOCKING , 10000 , 10000 );
358+ sp = new_sp ;
359+ } catch (Exception ex ) {
360+ System .out .println ("Switching comm port failed: " + ex .getMessage ());
314361 }
362+ System .out .println (commName );
363+ } else {
364+ System .out .println (e .getActionCommand ());
315365 }
316366 }
317367
0 commit comments