Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Talking how to display input from one program to a gui

    I'm designing an application that reads data from an RS232 port (serial) and i would like it to display the data it reads to my GUI, how do i do that?

    please help me link the two.

    Both my source codes are shown below:

    Java Code:
    package com.data.files;
    
    // this works fine: 
    
    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.SerialPort;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    
    public class TwoWaySerialComm
    {
            StringBuffer allOutput;
            String output;
        public TwoWaySerialComm()
        {
            super();
    
        }
    
        void connect ( String portName ) throws Exception
        {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
            if ( portIdentifier.isCurrentlyOwned() )
            {
                System.out.println("Error: Port is currently in use");
            }
            else
            {
                CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
    
                if ( commPort instanceof SerialPort )
                {
                    SerialPort serialPort = (SerialPort) commPort;
                    serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
    
                    InputStream in = serialPort.getInputStream();
                    OutputStream out = serialPort.getOutputStream();
    
                    (new Thread(new SerialReader(in))).start();
                    (new Thread(new SerialWriter(out))).start();
    
                }
                else
                {
                    System.out.println("Error: Only serial ports are handled by this example.");
                }
            }
        }
    
        /** */
        public static class SerialReader implements Runnable
        {
            InputStream in;
    
            public SerialReader ( InputStream in )
            {
                this.in = in;
            }
    
            public void run ()
            {
                byte[] buffer = new byte[1024];
                int len = -1;
                try
                {
                    while ( ( len = this.in.read(buffer)) > -1 )
                    {
    
    // i would like the output from here to show on my GUI below
                    System.out.print(new String (buffer,0,len));
    
                    //com.data.files.TestPanels.text1 = System.out.print(new String (buffer,0,len));
    
                    }
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                }
            }
        }
    
        /** */
        public static class SerialWriter implements Runnable
        {
            OutputStream out;
    
            public SerialWriter ( OutputStream out )
            {
                this.out = out;
            }
    
            public void run ()
            {
                try
                {
                    int c = 0;
                    while ( ( c = System.in.read()) > -1 )
                    {
                        this.out.write(c);
                    }
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main ( String[] args )
        {
            try
            {
                (new TwoWaySerialComm()).connect("COM1");
            }
            catch ( Exception e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


    GUI that doesn't work:

    Java Code:
    package com.data.files;
    
    import java.awt.*;
      import javax.swing.*;
      import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    
    
      public class TestPanels extends JFrame implements ActionListener {
    
         public String text1 = "";
    
        public TestPanels() {
        // Create panel p1 for the buttons and set GridLayout
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(4, 3));
    
        // Add buttons to the panel
        for (int i = 1; i <= 9; i++) {
          p1.add(new JButton("" + i));
        }
    
        p1.add(new JButton("" + 0));
        p1.add(new JButton("ENTER"));
        p1.add(new JButton("CANCEL"));
    
        // Create panel p2 to hold a text field and p1
        JPanel p2 = new JPanel(new BorderLayout());
        p2.add (new JTextField("PLU Number"),
          BorderLayout.NORTH);
        p2.add (p1, BorderLayout.CENTER);
    
        // add contents into the frame
        add(p2, BorderLayout.EAST);
        add(new JButton("CHANGE COMM Port"),
        
                BorderLayout.CENTER);
        
    
    
    
    
        }
    
        
    
        /** Main method */
        public static void main(String[] args) {
          TestPanels frame = new TestPanels();
          frame.setTitle("AVOCET SOUTH AFRICA");
          frame.setLocationRelativeTo(null); // Center the frame
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(400, 250);
          frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
    
      }


    Any help will greatly be appreciated
    Last edited by Fubarable; 11-24-2010 at 10:24 PM. Reason: Moderator edit: code tags added

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    When posting code, make sure you use the code tags to preserve formatting. Otherwise nobody will want to read it.

    You have at least two separate problems here: designing a GUI and getting the input. Which one are you having trouble with? What specifically are you stuck on?

    You should provide an SSCCE that demonstrates the most specific piece you're working on.

  3. #3
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default The GUI is nt that important

    I need a simple gui that displays the reading(data/input) i'm getting from the serial port. The GUI is nt that important, i just need a basic display into the gui. thanx!

    [CODE] public static class SerialReader implements Runnable
    {
    InputStream in;

    public SerialReader ( InputStream in )
    {
    this.in = in;
    }

    public void run ()
    {
    byte[] buffer = new byte[1024];
    int len = -1;

    try

    {
    while ( ( len = this.in.read(buffer)) > -1 )
    {
    //System.out.print(new String (buffer,0,len));
    String text1 = new String (buffer,0,len); /*i want this (text1) to print to my gui*/
    //System.out.print(text1);

    }

    }
    catch ( IOException e )
    {
    e.printStackTrace();
    }
    }
    }CODE]

  4. #4
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    this is my code, it does not interact with the gui at all. I do not know how to get it to work together.
    help:
    Java Code:
    package com.data.classes;
    
    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.SerialPort;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    
    public class NewClass
    {
    
    
        public NewClass()
        {
            super();
            
        }
    
        void connect ( String portName ) throws Exception
        {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
            if ( portIdentifier.isCurrentlyOwned() )
            {
                System.out.println("Error: Port is currently in use");
            }
            else
            {
                CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
    
                if ( commPort instanceof SerialPort )
                {
                    SerialPort serialPort = (SerialPort) commPort;
                    serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
    
                    InputStream in = serialPort.getInputStream();
                    OutputStream out = serialPort.getOutputStream();
    
                    (new Thread(new SerialReader(in))).start();
                    (new Thread(new SerialWriter(out))).start();
    
                }
                else
                {
                    System.out.println("Error: Only serial ports are handled by this example.");
                }
            }
        }
    
        /** */
        public static class SerialReader implements Runnable
        {
            InputStream in;
    
            public SerialReader ( InputStream in )
            {
                this.in = in;
            }
    
            public void run ()
            {
                byte[] buffer = new byte[1024];
                int len = -1;
    
                try
    
                {
                    while ( ( len = this.in.read(buffer)) > -1 )
                    {
                    //System.out.print(new String (buffer,0,len));             
                    String text1 = new String (buffer,0,len);
                    System.out.print(text1);
    
                    }
    
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                }
            }
        }
    
        /** */
        public static class SerialWriter implements Runnable
        {
            OutputStream out;
    
            public SerialWriter ( OutputStream out )
            {
                this.out = out;
            }
    
            public void run ()
            {
                try
                {
                    int c = 0;
                    while ( ( c = System.in.read()) > -1 )
                    {
                        this.out.write(c);
                    }
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                }
            }
           
      }
    
        public static void main ( String[] args )
        {
             JFrame frame = new JFrame("MyFrame"); // Create a frame
            JTextArea displayOutput = new JTextArea();
            frame.add(displayOutput);
            //displayOutput.setText(allOutput.toString());
            displayOutput = new JTextArea(10, 20);
    
            //frame.set
            frame.setSize(400, 300); // Set the frame size
            frame.setLocationRelativeTo(null); // New since JDK 1.4
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true); // Display the frame
            try
            {
                (new NewClass()).connect("COM1");
            }
            catch ( Exception e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    Moderator Edit: Quote tags changed to code tags
    Last edited by Fubarable; 11-25-2010 at 12:59 PM. Reason: Moderator Edit: Quote tags changed to code tags

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Read on Concurrency in Swing, and create a SwingWorker for your background processes (reading and writing to your port), then when you read a line in from the port, pass the String into the SwingWorker's publish method, and in the process method, append it to your JTextArea. The tutorial and API will help you get started.

    Luck

Similar Threads

  1. Replies: 1
    Last Post: 10-18-2010, 07:14 AM
  2. GUI text display input to list array
    By berndog in forum New To Java
    Replies: 1
    Last Post: 08-08-2010, 06:35 PM
  3. Program doesn't display anything
    By Chasingxsuns in forum New To Java
    Replies: 14
    Last Post: 11-11-2009, 03:52 AM
  4. Display program
    By nhlfan in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 12:00 AM
  5. Help with program input
    By Daniel in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 05:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •