It appears to me that the default "simple" serial will not work with Java on Windows. It only works with fdserial, so here is an example I made work.
Here is the Propeller C code for the Activity Board:
#include "simpletools.h"
#include "fdserial.h"
fdserial *term;
int main()
{
simpleterm_close(); // Close simplex serial terminal
term = fdserial_open(31, 30, 0, 115200); // Open Full Duplex serial connection
while(1)
{
dprint(term, "Enter a string of 50 characters or less:\n");
char buf[51]; // A Buffer long enough to hold the longest line you may send.
int count = 0;
while (count < 51) {
buf[count] = fdserial_rxChar(term);
if (buf[count] == '\r' || buf[count] == '\n')
break;
count++;
}
buf[count] = '\0';
dprint(term, "%s\n", buf);
}
}
Please test that first in SimpleIDE. It will not echo your typing, but after you hit return it will send your line back to you.
Download jSSC-2.6.0-Release.zip from https://code.google.com/p/java-simple-serial-connector/downloads/list (Or use the latest version, I did this a while ago) and extract it.
Put the extracted folder "jSSC-2.6.0-Release" into that "lib" folder in your Eclipse project in the workspace.
In Eclipse right click on your project and go to properties. Select "Java Build Path" and the "Libraries" tab Click on "Add JARs..." and drill down to the jssc.jar file and select it. Click OK to get back to your Eclipse workspace
Right click on src and add a new package called "jssc_test" Right click on the package and a new class called Test1
package jssc_test;
import java.util.Scanner;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class Test1 {
static SerialPort serialPort;
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("You must include the COM port!");
System.out.println("Like this:");
System.out.println("Test1 COM4");
System.out.println("Test1 /dev/ttyUSB0");
System.out.println("Test1 /dev/ttyAMA0");
System.out.println("etc.");
System.exit(1);
}
Scanner input = new Scanner(System.in);
//serialPort = new SerialPort(args[0]); // Use this to get the COM port form the command line when you bild a JAR file.
serialPort = new SerialPort("COM4");
try {
//System.out.print("Opening " + args[0] + " at");
System.out.print("Opening COM4 at");
serialPort.openPort();
System.out.print(" 115200, 8, 1, 0 and ");
serialPort.setParams(115200, 8, 1, 0);
//Preparing a mask. In a mask, we need to specify the types of events that we want to track.
//Well, for example, we need to know what came some data, thus in the mask must have the
//following value: MASK_RXCHAR. If we, for example, still need to know about changes in states
//of lines CTS and DSR, the mask has to look like this: SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
int mask = SerialPort.MASK_RXCHAR;
//Set the prepared mask
serialPort.setEventsMask(mask);
//Add an interface through which we will receive information about events
System.out.println("waiting for data . . .");
serialPort.addEventListener(new SerialPortReader());
}
catch (SerialPortException ex) {
System.out.println("Serial Port Opening Exception: " + ex);
}
while(true) {
String s = input.nextLine();
//System.out.println(s + "\n");
try {
serialPort.writeString(s + "\n");
} catch (SerialPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
//Object type SerialPortEvent carries information about which event occurred and a value.
//For example, if the data came a method event.getEventValue() returns us the number of bytes in the input buffer.
/* For debugging, this should always be 1 unless we are
* waiting for more than one byte, otherwise it just junks up the output :)
*/
//System.out.println("Bytes: " + event.getEventType());
if(event.isRXCHAR()){
/* See original code,
* it waited for a certain number of bytes,
* but if I want the characters, why do that?
*/
//if(event.getEventValue() == 10){
try {
String data= serialPort.readString();
//System.out.println("Data: " + data); // For debugging
System.out.print(data);
}
catch (SerialPortException ex) {
System.out.println("Serial Port Reading Exception: " + ex);
}
//}
}
//If the CTS line status has changed, then the method event.getEventValue() returns 1 if the line is ON and 0 if it is OFF.
else if(event.isCTS()){
if(event.getEventValue() == 1){
System.out.println("CTS - ON");
}
else {
System.out.println("CTS - OFF");
}
}
else if(event.isDSR()){
if(event.getEventValue() == 1){
System.out.println("DSR - ON");
}
else {
System.out.println("DSR - OFF");
}
}
}
}
}
I am able to send a line to the Activity Board and it will parrot it back to me, this is using the USB based serial port on Windows.
It will allow you to connect directly to the serial port. While SimpleIDE does this too, you are probably aware that sometimes it feels like it is doing something under the covers that you cannot see. You can be sure that puTTY is not doing anything special, so if it works, your program should work.
I typically test first in SimpleIDE, then I test the same code with puTTY before trying to test it with my own code. That way if my code fails I know whose fault it is.
Comments
Here is the Propeller C code for the Activity Board:
Please test that first in SimpleIDE. It will not echo your typing, but after you hit return it will send your line back to you.
And here is the Java code, that makes use of jSSC from https://code.google.com/p/java-simple-serial-connector/
Eclipse Instructions:
Open up Eclipse and start a new Project for testing.
Add a folder under the Project called "lib".
I'm following the instructions here roughly, although they are for NetBeans.
https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_Start_Working
Download jSSC-2.6.0-Release.zip from https://code.google.com/p/java-simple-serial-connector/downloads/list (Or use the latest version, I did this a while ago)
and extract it.
Put the extracted folder "jSSC-2.6.0-Release" into that "lib" folder in your Eclipse project in the workspace.
In Eclipse right click on your project and go to properties.
Select "Java Build Path" and the "Libraries" tab
Click on "Add JARs..." and drill down to the jssc.jar file and select it.
Click OK to get back to your Eclipse workspace
Right click on src and add a new package called "jssc_test"
Right click on the package and a new class called Test1
Paste in this test code:
Sources:
https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples
http://stackoverflow.com/questions/16303585/java-simple-serial-connector-read-write-server
I am able to send a line to the Activity Board and it will parrot it back to me, this is using the USB based serial port on Windows.
On Windows I use puTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/
It will allow you to connect directly to the serial port. While SimpleIDE does this too, you are probably aware that sometimes it feels like it is doing something under the covers that you cannot see. You can be sure that puTTY is not doing anything special, so if it works, your program should work.
I typically test first in SimpleIDE, then I test the same code with puTTY before trying to test it with my own code. That way if my code fails I know whose fault it is.
Thanks for the help. I am at school, so I will have to test this out tonight or tomorrow. I will post the results of my experiment ASAP.
Your solution worked very well, and I had no trouble getting it to work.
I am thinking that as of now, I am just going to skip over using the RN-42 Bluetooth module, and plan to control the telepresence device over USB.