Hi Guys,

I was trying to write a code to send AT command to a GSM modem. But i am facing problem while sending the AT command from UI.
Below is the code:

SEND.addActionListener(new ActionListener() {

@Override
        public void actionPerformed(ActionEvent e) {
                Thread runThread = new Thread(new Runnable() {  
                        @Override
                        public void run() {
                            try{
                                  sp.getOutputStream().write((textField.toString()+"\r\n").getBytes());
                                  System.out.println("Written..........");                              
                                }catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                            }
                            });
                             runThread.start();
                            } 
        });

sp is a Serialport intialized with a particular portID and it is opened. Code to write AT command is correct since i have checked it in non-UI application and it is working. But only in UI app it does not work. Please help me resolve this.

Recommended Answers

All 4 Replies

That is a problem if this is all of your code. Please be more complete... There is nothing regarding to opening the port, writing/reading data to/from it, etc.

Here is the code for opening the port using a button from my UI.

btnOpen.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    if(isOpen == false){
                    portIdin = CommPortIdentifier.getPortIdentifier(comboBox.getSelectedItem().toString());
                    sp = (SerialPort) portIdin.open("ATT", 2000);
                    isOpen = true;
                    btnOpen.setEnabled(false);
                    sp.notifyOnDataAvailable(true);
                    sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    System.out.println("Successfully opened " + sp.getName());
                    }
                } catch (NoSuchPortException e1) {
                    e1.printStackTrace();
                } catch (PortInUseException e1) {
                    e1.printStackTrace();
                } catch (UnsupportedCommOperationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }
        });

And this is the code to add ports to ComboBox.

Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        CommPortIdentifier cpi = null;

        while(ports.hasMoreElements()){
            cpi = (CommPortIdentifier) ports.nextElement();
            COM.addElement(cpi.getName());
        }
        comboBox.setModel(COM);

9: ... textField.toString() ...

If textField is a JTextField or similar then this is a mistake. The toString() method gives you a String description of the text field object and all it's parameters. Use getText() to get the text that's displayed in the text field.

  • most of GUI APIs, frameworks are single threaded

  • Swing/AWT GUI doesn't care that Workers Thread update Swing/AWT methods (from =>Java4), or freeze until heavy and long background task ended, without repainting programatically to Swing GUI by using invokeLater (just methods implemented in Swing/AWT APIs)

  • your code snippeds isn't clear somehow, issue could be elsewhere, anywhere too

  • more in Oracle tutorial Event Dispatch Thread

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.