2

I am trying to communicate from my computer to a circuit board over a serial port using javax.comm. I wrote a program (based on the SimpleRead and SimpleWrite examples) that will take any data received from the device and print it to System.out and take any line typed into System.in and send it to the device, followed by \r\n.

The device has a help function which I am using to test communications (send hlp and the device replies with a list of commands it will take). When I first send hlp I get the expected response, but the 2nd time I send hlp I see no response.

Is this a known problem with javax.comm? Or is there some sort of start-of-new-message command I need to look up and send to my device between messages?

Further Details:

I'm using a Toshiba laptop running Windows XP. I've hooked the board up to an oscilloscope and the "sending data" test point changes voltage when the first hlp is sent (this is when I see a text response in my program. however the "sending data" test point does not change voltage in response to the second and subsequent messages (when I see no text response). Thus I am pretty sure that the issue is my program not sending the data properly (rather than my program not receiving the response properly).

The reading and writing methods of my program:

public void run() {
    try {
        InputStreamReader converter = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(converter);
        String s = in.readLine();
        s = s+"\r\n";
        
        serialPort.getOutputStream().write(s.getBytes());
        serialPort.getOutputStream().flush();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
    case SerialPortEvent.BI:
    case SerialPortEvent.OE:
    case SerialPortEvent.FE:
    case SerialPortEvent.PE:
    case SerialPortEvent.CD:
    case SerialPortEvent.CTS:
    case SerialPortEvent.DSR:
    case SerialPortEvent.RI:
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        break;
    case SerialPortEvent.DATA_AVAILABLE:
        byte[] readBuffer = new byte[100];
        byte[] actualBytes = null;
        try {
            while (inputStream.available() > 0) {
                int numBytes = inputStream.read(readBuffer);
                actualBytes = new byte[numBytes];
                for(int i =0; i < numBytes; i++){
                    actualBytes[i] = readBuffer[i];
                }
            }
            
            System.out.print(new String(actualBytes));
        } catch (IOException e) {}
        break;
    }
}
Community
  • 1
  • 1
  • Nevermind. This turns out to be an issue with my (limited) understanding of threading. I thought 'run()' was called continuously, but it is only called once (hence why I could only send one command). I surrounded everything in 'run()' with a 'while(true)' loop and now it works fine! – Matt Vernacchia Jul 25 '11 at 15:18

0 Answers0