Wybór portu COM za pomocą JComboBox

0

Witam napisałem program który po podłączeniu modułu GSM do USB za pomocą prostego gui wysyła sms. Problem jest taki,że potrzebuję zrobić wybór portu COM takze z gui chce to zrobić za pomocą JComboBox. Stworzyłem zmienna String która zawiera wybór z naszego komboboxa jednak nie wiem kompletnie jak to zainicjować w programie. Pewnie błąd leży w tym ze program zaczyna otwierać port zanim zdaże wybrać Port.


import java.io.*;
import java.util.*;

import javax.swing.JFrame;

import gnu.io.*;

public class SMSsender  extends oknosender {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString1 = "AT";
static String messageString2 = "AT+CPIN=\"";
static String messageString3 = "AT+CMGF=1";
static String messageString4 = "AT+CMGS=\"";
static String messageString5 = "TEST";
static String coma = "\"";
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static boolean smsinfo;
static char enter = 13; 
static char CTRLZ = 26; 
 


public static void main(String[] args) {
	
	oknosender okienko = new oknosender(); 
	okienko.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	okienko.setVisible(true);
	
	portList = CommPortIdentifier.getPortIdentifiers();
  
    while (portList.hasMoreElements()) {

        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

             if (portId.getName().equals("Nazwaportu")) {

                try {
                    serialPort = (SerialPort)
                        portId.open("Nazwaportu", 2000);
                } catch (PortInUseException e) {System.out.println("err");}
                try {
                    outputStream = serialPort.getOutputStream();
                    inputStream = serialPort.getInputStream();
                } catch (IOException e) {e.printStackTrace();}
                try {
                    serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {e.printStackTrace();}
                try {
                	String tryb = "[65, 84, 43, 67, 80, 73, 78, 61, 34, 34, 13, 13, 10, 69, 82, 82]";  
                	String sender = "[79, 82, 13, 10, 65, 84, 43, 67, 77, 71, 70, 61, 49, 13, 13, 10, 79, 75, 13, 10, 65, 84, 43, 67, 77, 71, 83]";
                	int i;
                	
                	for (i=1; i<10; i++) { 
                	Thread.sleep(10000);	
                	
                	if (znacznik2==12 && validatePhoneNumber==true && validatePINNumber==true) {
                	outputStream.write((messageString1 + enter).getBytes());                                 
                    Thread.sleep(200); 
                    outputStream.flush();  
                    
                    
                    byte buffer[] = new byte[16];                    
                    inputStream.read(buffer);                                        
                	
                    outputStream.write((messageString2 + wPin + coma + enter).getBytes());                                       
                    Thread.sleep(100); 
                    outputStream.flush();
                     
                	outputStream.write((messageString3 + enter).getBytes());                 
                    Thread.sleep(200); 
                    outputStream.flush(); 
                    
                    byte buffer1[] = new byte[16];                  
                    inputStream.read(buffer1); 
                    
                    String tryb1 = Arrays.toString(buffer1); 
                    
                     if (tryb1.equals(tryb)){  
                    	 System.out.println("Przejscie w tryb tekstowy");
                    	 
                     } else { 
                    	 System.out.println("Błąd przejscia w tryb tekstowy");
                     }
                    
                    outputStream.write((messageString4 + wNumer + coma + enter).getBytes());                    
                   Thread.sleep(200);  
                    outputStream.flush();
                    
                   outputStream.write((messageString5 + CTRLZ).getBytes());                   
                    outputStream.flush();  
                    
                    byte buffer2[] = new byte[27];                  
                    inputStream.read(buffer2);  
                    
                    String sender1 = Arrays.toString(buffer2);  
                    
                    if (sender1.equals(sender)){  
                   	 System.out.println("Wiadomosc wyslana"); 
                   	smsinfo=true;
                   	 
                    } else { 
                   	 System.out.println("Wiadomosc nie wyslana");
                   	smsinfo=false;
                    }
                    
       
        Thread.sleep(3000); 
        
        
        
                	}
                	
        if (validatePhoneNumber==true && validatePINNumber==true){

        outputStream.close();
        serialPort.close(); 
        System.out.println("Port COM Zamkniety");  
                	}
                	}

                } catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace(); 
				}
            }
        }
    }
//}



private static int Array(int i, int j, int k, int l, int m, int n, int o, int p, int q, int r, int s, int t, int u,
		int v, int w, int x) {
	// TODO Auto-generated method stub
	return 0;
}
} 

 

Nazwaportu to zmienna od JKomboboxa.

0

Sformatuj kod i zamieść na forum poprawny (tzn. kompilowalny) kod.
Wyrzuć kod z metody main, w niej zostaw tylko wywołanie konstruktora okna.
Zrób porządne GUI, z JComboBoxem wyboru portu, polami tekstowymi do wpisania numeru telefonu i treści SMS-a oraz przyciskiem wysyłającym.,

0

Dobra problem rozwiązany proszę o usunięcie tematu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1