 |
|  |
 |
| Implementation of the RS-232 Serial Port |
 |
 |
 |
 |
 |
 |
 |
| Sending bytes to and receiving bytes from the RS-232 port on the aJile |
In this example we will show how to use the serial port A on the SaJe board.
On the hardware side:
Connect the serial Port A on the board to the serial port on your PC using the RS232 DCE serial adapter and a 10-conductor ribbon cable (5x2 header pinout).
On the software side:
You shall use the javax.com library which is a part of the aJile software, and on my PC it is placed at C:\aJile\Runtime_cldc\Rts\javax\comm. Here you find the class files, but no documentation for the classes. You can download the similar classes from Sun on their homepage for Java Communications API: http://java.sun.com/products/javacomm/. In this package you will find the javadoc documentation for the classes.
In the JEMBuilder:
When you come to Project Drivers: select com1.
Running the program:
On your PC use a terminal emulator, par example Tera Term, a free software you can download from the Tera Term Home Page. Configure the terminal emulator to 9600 baud, no parity, 8 data bits, 1 stop bit. Also make sure that "local echo" isn't enabled.
Implementation of the example program:
First I will show how to
- initialize a serial port
- send a byte from the aJile to the PC
- receive a byte from the PC
I have called the class SerialPortImpl: class SerialPortImpl
{
private SerialPort serialPort;
private CommPortIdentifier commPortId;
private InputStream inStream; // serial input stream
private OutputStream outStream; // serial output stream
public void initializeSerialPort()
{
.
commPortId = CommPortIdentifier.getPortIdentifier("com1");
serialPort = (SerialPort)commPortId.open("com1",0);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// Get output and input streams
outStream = serialPort.getOutputStream();
inStream = serialPort.getInputStream();
.
}
public void sendByte (byte b)
{
.
outStream.write (b);
.
}
public byte receiveByte()
{
.
return (byte)inStream.read ();
.
}
}
The aJile can now receive bytes from the Tera Term emulator and send bytes to the emulator by using an object of this class :
public static void main (String [] args)
{
SerialPortImpl serialP = new SerialPortImpl();
serialP.initializeSerialPort();
String startStr = "\r\nEnter characters. End with x: ";
String endStr = "\r\nReceived x. The session ends now.";
byte endByte = 'x';
byte b;
serialP.sendBytes(startStr.getBytes()); // this method is not shown above
do
{
b = serialP.receiveByte();
System.out.println("** Received byte: " + b + ". As char: " + (char)b);
serialP.sendByte (b); // echo the character back
} while (b != endByte);
serialP.sendBytes(endStr.getBytes());
}
Other places of interest:
- examples from the aJile package in: C:\aJile\Examples
- examples from Sun's Java Communications API homepage (look above)
- example from JStampU: Serial Echo Example.
You can download the whole program here:
Serial port example |
 |
 |
 |
 |
 |
 |
 |
|
 |
|
|  |
|
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
 |
Implementation of the RS-232 Serial Port |
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|