質問

I'm trying to make a Java interface to send some AT commands to a GPRS Module.

I already had a working interface on Processing, but I migrated to pure Java because I find it easier to make graphic interfaces.

The Processing program sends data over serial to COM#. COM# is an Arduino with a GPRS Shield. All the Arduino Code does is pass the data received to the GPRS module and viceversa:

void loop(){
    if (GPRS.available())
    {
        while(GPRS.available())
        {
            buffer[count++]=GPRS.read();
            if(count == 64)break;
        }
        Serial.write(buffer,count);
        clearBufferArray();
        count = 0;
    }
    if (Serial.available()){
                delay(100);
                while(Serial.available()){
                  GPRS.write(Serial.read());
                }
        }
}

So I know that works fine because I've tested it using the Processing interface, and an external tool called SSCOM and all the comands are interpreted correctly.

Now the problem is, that when I tried to make the interface on java, using RXTX, it's not working at all. I'm not getting any errors on the console, and the only data I'm receiving on the Arduino is ÿ (char 255) each time I run java, and it's being sent when opening the port, not when writing to the serial port.

Here's the SerialHandlerclass I'm using. I found it around the web.

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialPortHandler {
    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;

    public void connect(String portName) throws IOException, PortInUseException, NoSuchPortException {
        try {
            // Obtain a CommPortIdentifier object for the port you want to open
            CommPortIdentifier portId =
                    CommPortIdentifier.getPortIdentifier(portName);

            // Get the port's ownership
            serialPort =
                    (SerialPort) portId.open("Demo application", 5000);

            // Set the parameters of the connection.
            setSerialPortParameters();

            // Open the input and output streams for the connection. If they won't
            // open, close the port before throwing an exception.
            outStream = serialPort.getOutputStream();
            inStream = serialPort.getInputStream();
        } catch (NoSuchPortException e) {
            throw new IOException(e.getMessage());
        } catch (PortInUseException e) {
            throw new IOException(e.getMessage());
        } catch (IOException e) {
            serialPort.close();
            throw e;
        }
    }

    /**
     * Get the serial port input stream
     * @return The serial port input stream
     */
    public InputStream getSerialInputStream() {
        return inStream;
    }

    /**
     * Get the serial port output stream
     * @return The serial port output stream
     */
    public OutputStream getSerialOutputStream() {
        return outStream;
    }

    /**
     * Sets the serial port parameters
     */
    private void setSerialPortParameters() throws IOException {
        int baudRate = 19200;

        try {
            serialPort.setSerialPortParams(
                    baudRate,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            serialPort.setFlowControlMode(
                    SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex) {
            throw new IOException("Unsupported serial port parameter");
        }
    }
}

And here's my main class:

public class Main {
        SerialPortHandler serial = new SerialPortHandler();
        serial.connect("COM3");
        OutputStream serialOut = serial.getSerialOutputStream();
        String s = "AT+CPOWD=0\r\n";
        serialOut.write(s.getBytes());
        serialOut.flush();
    }

}

rxtxSerial.dll and RXTXcomm.jar are in jre\bin and jre\lib\ext respectively.

I can't find the problem, like I said, I don't get any errors or warnings anywhere.

I'm using NetBeans 7.3.1, JDK 1.7 and Windows 8.1 x64.

I also tried using jSSC but I get the same results.

役に立ちましたか?

解決

I solved the problem. It wasn't the jSSC or RXTX library.

PeterMmm was right, I needed a delay, but I needed it on my java program. When I was testing communication, I was opening the port and sending data right away. The problem is Arduino by default resets when a connection is established, so the Arduino wasn't ready to process it.

A couple days ago, I decided to try again, with jSSC, but this time, the java program asked for an input to send through the serial port, leaving time for Arduino to boot. Arduino successfully received anything I typed.

Now that I implemented serial communication on the user interface I was developing, everything runs smoothly.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top