SerialEcho

Javaxcomm example Last Modified:30 Sep 2002

Summary:
Use javxcomm to receive serial data on any JStamp/JStik/SaJe com port RXD line, echo it back out the same com port TXD line, and print the hex value and ASCII char in Charade. This is a good serial I/O test program. All you need is a JStamp/JStik/SaJe system, serial cable, and a PC with any common terminal program (to communicate with your target system).

Tutorial:
1- Get the Project Files
SerialEcho.jar Download this JAR file, extract and read the javadocs for SerialEcho. It's a good idea to have a \projects root folder and expand the SerialEcho files as a subfolder of that: \projects\SerialEcho
systronix_utils.jar I store the systronix_utils jar on our server in \javalib\utils where "javalib" is the root folder for all our common shared Java libraries such as javaxcomm, 1-Wire, XML, etc.
Sun javaxcomm package See the quickstart tutorials for where to get javaxcomm and how to install and configure it.

2- Open the SerialEcho source file and change the com port and baud rate

If you are using JBuilder, a project definition will be in your SerialEcho root folder. You will need to change the project properties output files location and required libraries location to match your system paths. See the JBuilder tutorial for details.

SerialEcho.java contains the method

public void initializeSerialPort()

which contains these lines:

commPortId = CommPortIdentifier.getPortIdentifier("com1");

The string "com1" identifies one of two onboard JStamp/JStik/SaJe UARTs, the other is "com2". The SaJe/SBX2 combination also has "com3" externally on the SBX2 board. Change this string to match the com port you wish to use.

and a bit later:

serialPort.setSerialPortParams(115200,
javax.comm.SerialPort.DATABITS_8,
javax.comm.SerialPort.STOPBITS_1,
javax.comm.SerialPort.PARITY_NONE);

Here the baud rate is 115200, you can change it to any of the javaxcomm baud rates (see the javaxcomm javadocs for a list).

Now rebuild the project to generate a class file with your chose com port ID and baud rate in it.


3- Configure and Link in JemBuilder
Several JemBuilder projects are already included. For example, SerialEchoJStampRAM creates a RAM build for JStamp. Open the project closest to your target. Here I chose a JStamp RAM project. You can double check or change your project setting in the Project Properties menu. Here we are selecting a JStampRamConfiguration project:
Now we need to tell JemBuilder where to find the .class file we just created with JBuilder. This is the JVM0 Classpath setting shown here. Browse to the folder where your SerialEcho.class file is. (Don't worry about the "systronix utils" library shown here, we'll add that in just a moment).

You'll need to modify the Output Files to match your paths: Expand the left pane "Project" settings and change this as needed:

Be sure the correct Project Drivers are loaded. Use the Project Drivers menu:
Select the desired driver in the "available drivers" window and then click on the right arrow to add it to the "Installed Drivers" window as we've done here for the com1 driver. Then click OK.
Now add the systronix_utils jar as a JemBuilder library. Use the Project->libraries menu:
Click new, create a name for this library ("systronix utils" is an obvious choice ), and then browse to the location of the systronix_utils jar file. Click OK when done.
The other settings such as the PLL (sets the speed of the target as a multiple of the processor crystal) should be OK. Double-check that on SaJe and JStik the Clock Out pin is enabled. (This setting doesn't matter on JStamp)
Now we should be ready to build. Click the JemBuilder build button, and you should see some output indicating the build succeeded.
 

4- Download and execute, using Charade

Connect the JTAG adapter to your target. Also attach a serial cable to the test com port on your target from your PC. Apply power. Start Charade. Select the correct device (aJ80 for JStamp, aJ100 for SaJe and JStik). Copy and paste the output path from the JemBuilder output window (here it is J:\PROJECTS\JavaCode\SerialEcho\rambuild). On the Charade command line, type "CD", a space, and then Ctrl-C to copy the path from Windows' clipboard into the Charade command line. This should perform a "Change Directory" to the folder where JemBuilder created our Java binary file.

Press enter to execute this command. Charade should respond with confirmation in its output window (just above the command line):

-> cd J:\PROJECTS\JavaCode\SerialEcho\rambuild
j:\projects\javacode\serialecho\rambuild

You can type any DOS command on this line, so you could do a "DIR" command to see a directory listing of all the files and verify that you changed to the correct folder.

For a RAM build be sure your target's "swap memory" jumper is installed to locate RAM at the boot up address.
Now click the Charade load button. The program should load with a progress bar.
If you wish you can perform a "verify build.bin" to veridy that the program did get successfully loaded into your device.
Now click the Charade "GO" button. You should see some Charade output such as this:
JStamp is running, waiting for serial input. If we start a terminal program and set the baud rate and have the proper cable (remember we need a null modem between the PC and another "DTE" port such as the JStamp development station or SaJe). If we type "JStamp Rocks!" followed by two Enter keys in the terminal emulator program we should see this output in Charade:
For other examples of serial I/O with javaxcomm see the LEGO® arm example at http://jcx.systronix.com/appnotes/lego-arm.htm