2

I edited an already-made example from the RXTX website. I am new in Java and Serial comm programming too.

The app runs perfectly one time. It reads the buffer from the PIC and sends the number I enter. The LEDs light and the PIC sends back the same buffer, asking for a number. But when I enter it, the LEDs turn off, nothing lights up again and I get again the message asking for a number.

There is nothing wrong with the micro controller's soft as it works perfectly with HyperTerminal.

The code is below:

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * This version of the TwoWaySerialComm example makes use of the 
 * SerialPortEventListener to avoid polling.
 *
 */
public class Test
{
    public Test()
    {
        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();

                serialPort.addEventListener(new SerialReader(in));
                serialPort.notifyOnDataAvailable(true);

                (new Thread(new SerialWriter(out))).start();

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }

    /**
     * Handles the input coming from the serial port. A new line character
     * is treated as the end of a block in this example. 
     */
    public static class SerialReader implements SerialPortEventListener 
    {
        private InputStream in;
        private byte[] buffer = new byte[1024];

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

        public void serialEvent(SerialPortEvent arg0) {
            int data;

            try
            {
                int len = 0;
                while ( ( data = in.read()) > -1 )
                {
                    if ( data == '\n' ) {
                        break;
                    }
                    buffer[len++] = (byte) data;
                }
                System.out.print(new String(buffer,0,len));
            }
            catch ( IOException e )
            {
                e.printStackTrace();
                System.exit(-1);
            }             
        }

    }

    /** */
    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();
                System.exit(-1);
            }            
        }
    }



    public static void main ( String[] args )
    {
        try
        {
            (new Test()).connect("COM3");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
  • I found out that from the second run the Java app adds a new line to the string sent to the PIC. For example instead of `2`, it sends `[new line]2` – Dani din Astra Apr 05 '12 at 18:01
  • You should probably specify exactly which version of RXTX you are using and on what OS you are running. There have been issues related to non-blocking receive events. Example: http://stackoverflow.com/questions/1391946/is-constant-polling-in-rxtx-necessary – kaliatech Apr 05 '12 at 18:17
  • After reading your 1st comment, it occurred to me that perhaps you don't realize the `this.out.write(c);` will be sending the enter key press (\n) in addition to the character that you type. As a result and depending on how your PIC is coded, perhaps that is messing up the second "run". (And by "run", I think you mean the second loop.) – kaliatech Apr 05 '12 at 18:31
  • And what can I do to stop sending the enter key? – Dani din Astra Apr 05 '12 at 18:55
  • 1
    Modify the character output loop. For example: `if (c == '\n') continue;` ...or something similar. – kaliatech Apr 05 '12 at 20:52

1 Answers1

0

Your question is not clear (to me) because it's not clear what you've programmed the pic to do exactly. My guess though, is that your expectations around the \n character as a terminator is what is causing a problem.

kaliatech
  • 17,579
  • 5
  • 72
  • 84
  • Not the PIC is sending the `\n` but the computer. – Dani din Astra Apr 05 '12 at 18:00
  • Again, it's not clear to me what your PIC is doing. Your question states "_The app runs perfectly one time. It reads the buffer from the PIC and sends the number I enter._". That doesn't make sense though unless your PIC is sending a predefined message as soon as it receives power. Similarly, your comment doesn't make sense. As coded, the java/rtx program (the computer) expects to receive a message from the PIC with \n at the end. Perhaps you are saying that the PIC simply echoes whatever the computer sends it? – kaliatech Apr 05 '12 at 18:26
  • The PIC takes the input number, transforms it into binary and displays it using the LEDs. – Dani din Astra Apr 05 '12 at 18:55