Professional Documents
Culture Documents
into lib directory. Dll files are located in Windows/i368mingw32 directory of RXTX library.
Refresh JavaSerial project by hitting F5 key. Next click the
right mouse button on the JavaSerial project and go to the
Properties/Java Build Path. Select Libraries tab and click Add
JARs button. Select RXTXcomm.jar and just click OK button.
1. import gnu.io.CommPortIdentifier;
2.
3. import java.util.Enumeration;
4.
5. public class ListAvailablePorts {
6.
7.
8.
9.
10.
while(ports.hasMoreElements())
11.
System.out.println(((CommPortIdentifier)por
ts.nextElement()).getName());
12.
13.
14.
15.
new ListAvailablePorts().list();
16.
17.
}
}
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version
= RXTX-2.1-7
COM1
LPT1
Number of listed ports depends on number of available ports
in your operating system. If you do not see presented output
please repeat configuration process again.
PHYSICAL EQUIPMENT
The choise of physical equipment which you need to use
during development of RS232 communication application
depends on your needs and capabilities of your avialable
equipment. You should ask yourself following questions:
do I have a RS232 specification of communication
protocol between end-point device and application which I
develop?
1. import gnu.io.CommPortIdentifier;
2. import gnu.io.SerialPort;
3.
7.
8.
9.
if (portIdentifier.isCurrentlyOwned()) {
10.
11.
12.
imeout
System.out.println("Port in use!");
} else {
// points who owns the port and connection t
13.
SerialPort serialPort = (SerialPort) portIdenti
fier.open("RS232Example", 2000);
14.
15.
16.
serialPort.setSerialPortParams(
17.
38400, SerialPort.DATABITS_8, SerialPort.S
TOPBITS_1, SerialPort.PARITY_NONE);
18.
19.
20.
CommPortSender.setWriterStream(serialPort
.getOutputStream());
21.
22.
23.
new CommPortReceiver(serialPort.getInputS
tream()).start();
24.
25.
26.
27.
public static void main(String[] args) throws E
xception {
28.
29.
// connects to the port which name (e.g. COM1)
is in the first argument
30.
new RS232Example().connect(args[0]);
31.
32.
// send HELO message through serial port usin
g protocol implementation
33.
CommPortSender.send(new ProtocolImpl().get
Message("HELO"));
34.
35.
}
}
InputStream in;
7.
8.
9.
10.
11.
this.in = in;
}
12.
13.
14.
try {
15.
int b;
16.
while(true) {
17.
18.
returns -1
19.
20.
protocol.onReceive((byte) b);
21.
22.
protocol.onStreamClosed();
23.
24.
heck again
25.
sleep(10);
26.
27.
} catch (IOException e) {
28.
e.printStackTrace();
29.
} catch (InterruptedException e) {
30.
e.printStackTrace();
31.
32.
}
}
33.
Protocol
Protocol is used to handle all incoming bytes, segregate them
and create recognized messages. The interface of Protocol
implementation allows to inform protocol manager about
each incoming byte and about broken stream situation.
1. public interface Protocol {
2.
3.
4.
5.
6.
7.
void onStreamClosed();
8. }
In our case protocol implementation is simple: message is
always ended with new line character. Colleaction of received
bytes creates String message which could be recognized.
When new message is recognized (new line character
appears or stream closes) then onMessage() method is called
to handle recognized message. If you develop more
complicated example (and I am sure you will do) logic of
triggered actions depending on recognized message should
be placed in external class (some kind of interpreter).
4.
int tail = 0;
5.
6.
7.
8.
if (b=='\n') {
9.
onMessage();
10.
} else {
11.
buffer[tail] = b;
12.
tail++;
13.
14.
}
}
15.
16.
17.
18.
19.
20.
/*
21.
oked
22.
*/
23.
24.
25.
// constructing message
26.
27.
System.out.println("RECEIVED MESSAGE: " +
message);
28.
29.
30.
31.
if ("HELO".equals(message)) {
32.
);
33.
CommPortSender.send(getMessage("OK")
} else if ("OK".equals(message)) {
34.
CK"));
35.
CommPortSender.send(getMessage("OK A
}
36.
tail = 0;
37.
38.
39.
40.
// helper methods
41.
42.
return (message+"\n").getBytes();
43.
44.
45.
{
46.
47.
48.
}
}
7.
8.
9.
CommPortSender.out = out;
10.
11.
12.
13.
try {
14.
System.out.println("SENDING: " + new Strin
g(bytes, 0, bytes.length));
15.
16.
// sending through serial port is simply writin
g into OutputStream
17.
out.write(bytes);
18.
out.flush();
19.
} catch (IOException e) {
20.
e.printStackTrace();
21.
22.
23.
}
}
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version
= RXTX-2.1-7
SENDING: HELO
Next repeat above steps but type COM2 in Program
arguments text area. Switch between output consoles using
button with monitor icon and compare results. You should
have two output consolse with results as presented below.
= RXTX-2.1-7
SENDING: HELO
= RXTX-2.1-7
SENDING: HELO
RECEIVED MESSAGE: OK
SENDING: OK ACK
CONCLUSIONS
When you receive information about that Sun doesn't support
serial communication for Windows system environment you
starts desperately searching alternative. In consequence
there is very good free alternative which is used in several
projects (and works fine) - RXTX library. Moreover you can in