Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 8 → Rev 9

/JavaSerial/src/com/rm5248/serial/SerialPort.java
1,6 → 1,5
package com.rm5248.serial;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
27,6 → 26,43
B38400,
B115200
}
/**
* Represents the number of bits that the serial port uses.
* Typically, this is 8-bit characters.
*/
public enum DataBits{
DATABITS_5,
DATABITS_6,
DATABITS_7,
DATABITS_8
}
/**
* The number of stop bits for data. Typically this is 1.
*/
public enum StopBits{
STOPBITS_1,
STOPBITS_2
}
/**
* The parity bit for the data. Typically None.
*/
public enum Parity{
NONE,
EVEN,
ODD
}
/**
* The Flow control scheme for the data, typically None.
*/
public enum FlowControl{
NONE,
HARDWARE,
SOFTWARE
}
 
/* The handle to our internal data structure which keeps track of the port settings.
* We need a special structure, as on windows we have a HANDLE type, which is void*,
39,27 → 75,105
/* The output stream that user code uses to write to the serial port.
*/
private SerialOutputStream outputStream;
/* Make sure we don't close ourselves twice */
private boolean closed;
 
/**
* Open the specified port, 8 bit characters, 1 stop bit, 9600 baud.
* Open the specified port, 8 bit characters, 1 stop bit, no parity, no flow control, 9600 baud.
*
* @param portName The port to open, on Windows "COM1" on Linux "/dev/ttyS0"
* @throws NoSuchPortException If the port does not exist
*/
public SerialPort( String portName ) throws NoSuchPortException {
public SerialPort( String portName ) throws NoSuchPortException, NotASerialPortException {
this( portName, BaudRate.B9600 );
}
 
/**
* Open the specified port, 8 bit characters, 1 stop bit.
* Open the specified port, 8 bit characters, 1 stop bit, no parity, no flow control.
*
* @param portName The port to open, on Windows "COM1" on Linux "/dev/ttyS0"
* @param rate The Baud Rate to open this port at
* @throws NoSuchPortException
*/
public SerialPort( String portName, BaudRate rate )throws NoSuchPortException {
 
public SerialPort( String portName, BaudRate rate )throws NoSuchPortException, NotASerialPortException {
this( portName, rate, DataBits.DATABITS_8 );
}
/**
* Open the specified port, 1 stop bit, no parity, no flow control.
*
* @param portName The port to open
* @param rate The Baud Rate to open this port at
* @param data The number of data bits.
*/
public SerialPort( String portName, BaudRate rate, DataBits data ) throws NoSuchPortException, NotASerialPortException {
this( portName, rate, data, StopBits.STOPBITS_1 );
}
public SerialPort( String portName, BaudRate rate, DataBits data, StopBits stop ) throws NoSuchPortException, NotASerialPortException {
this( portName, rate, data, stop, Parity.NONE );
}
public SerialPort( String portName, BaudRate rate, DataBits data, StopBits stop, Parity parity ) throws NoSuchPortException, NotASerialPortException {
this( portName, rate, data, stop, parity, FlowControl.NONE );
}
public SerialPort( String portName, BaudRate rate, DataBits data, StopBits stop, Parity parity, FlowControl flow ) throws NoSuchPortException, NotASerialPortException {
int myRate = 0;
int myData = 0;
int myStop = 0;
int myParity = 0;
int myFlow = 0;
closed = false;
switch( rate ){
case B0 : myRate = 0; break;
case B50 : myRate = 50; break;
case B75 : myRate = 75; break;
case B110 : myRate = 110; break;
case B134 : myRate = 134; break;
case B150 : myRate = 150; break;
case B200 : myRate = 200; break;
case B300 : myRate = 300; break;
case B600 : myRate = 600; break;
case B1200 : myRate = 1200; break;
case B1800 : myRate = 1800; break;
case B2400 : myRate = 2400; break;
case B4800 : myRate = 4800; break;
case B9600 : myRate = 9600; break;
case B38400 : myRate = 38400; break;
case B115200: myRate = 115200; break;
}
switch( data ){
case DATABITS_5: myData = 5; break;
case DATABITS_6: myData = 6; break;
case DATABITS_7: myData = 7; break;
case DATABITS_8: myData = 8; break;
}
switch( stop ){
case STOPBITS_1: myStop = 1; break;
case STOPBITS_2: myStop = 2; break;
}
switch( parity ){
case NONE: myParity = 0; break;
case ODD: myParity = 1; break;
case EVEN: myParity = 2; break;
}
switch( flow ){
case NONE: myFlow = 0; break;
case HARDWARE: myFlow = 1; break;
case SOFTWARE: myFlow = 2; break;
}
handle = openPort(portName, myRate, myData, myStop, myParity, myFlow);
inputStream = new SerialInputStream( handle );
outputStream = new SerialOutputStream( handle );
}
 
/**
* Set the Baud Rate for this port.
67,27 → 181,32
* @param rate
*/
public void setBaudRate( BaudRate rate ){
int myRate = 0;
if( closed ){
throw new IllegalStateException( "Cannot set the BaudRate once the port has been closed." );
}
switch( rate ){
case B0:
setBaudRate( 0 );
break;
case B50:
case B75:
case B110:
case B134:
case B150:
case B200:
case B300:
case B600:
case B1200:
case B1800:
case B2400:
case B4800:
case B9600:
case B38400:
case B115200:
case B0 : myRate = 0; break;
case B50 : myRate = 50; break;
case B75 : myRate = 75; break;
case B110 : myRate = 110; break;
case B134 : myRate = 134; break;
case B150 : myRate = 150; break;
case B200 : myRate = 200; break;
case B300 : myRate = 300; break;
case B600 : myRate = 600; break;
case B1200 : myRate = 1200; break;
case B1800 : myRate = 1800; break;
case B2400 : myRate = 2400; break;
case B4800 : myRate = 4800; break;
case B9600 : myRate = 9600; break;
case B38400 : myRate = 38400; break;
case B115200: myRate = 115200; break;
}
setBaudRate( myRate );
}
 
/**
96,6 → 215,10
* @return The input stream.
*/
public InputStream getInputStream(){
if( closed ){
throw new IllegalStateException( "Cannot get the input stream once the port has been closed." );
}
return inputStream;
}
 
104,6 → 227,10
* @return The output stream.
*/
public OutputStream getOutputStream(){
if( closed ){
throw new IllegalStateException( "Cannot get the output stream once the port has been closed." );
}
return outputStream;
}
 
111,12 → 238,80
* Close the serial port, and all input streams
*/
public void close(){
if( closed ) return;
closed = true;
doClose();
}
/**
* See if the port has been closed already.
* @return
*/
public boolean isClosed(){
return closed;
}
 
public void finalize(){
close();
}
/**
* Set the stop bits of the serial port, after the port has been opened.
*
* @param stop
*/
public void setStopBits( StopBits stop ){
int myStop = 0;
if( closed ){
throw new IllegalStateException( "Cannot set the StopBits once the port has been closed." );
}
switch( stop ){
case STOPBITS_1: myStop = 1; break;
case STOPBITS_2: myStop = 2; break;
}
setStopBits( myStop );
}
/**
* Set the data bits size, after the port has been opened.
*
* @param data
*/
public void setDataSize( DataBits data ){
int myData = 0;
if( closed ){
throw new IllegalStateException( "Cannot set the DataBits once the port has been closed." );
}
switch( data ){
case DATABITS_5: myData = 5; break;
case DATABITS_6: myData = 6; break;
case DATABITS_7: myData = 7; break;
case DATABITS_8: myData = 8; break;
}
setCharSize( myData );
}
public void setParity( Parity parity ){
int myParity = 0;
if( closed ){
throw new IllegalStateException( "Cannot set the DataBits once the port has been closed." );
}
switch( parity ){
case NONE: myParity = 0; break;
case ODD: myParity = 1; break;
case EVEN: myParity = 2; break;
}
setParity( myParity );
}
 
/**
* Open the specified port, return an internal handle to the data structure for this port.
124,9 → 319,10
* @param portName
* @return
*/
private native int openPort( String portName ) throws NoSuchPortException;
private native int openPort( String portName, int baudRate, int dataBits, int stopBits, int parity, int flowControl ) throws NoSuchPortException;
 
/**
* Set the baud rate in the native code.
*
* @param baudRate
* @return
133,52 → 329,32
*/
private native boolean setBaudRate( int baudRate );
 
/**
* Close this port, release all native resources
*/
private native void doClose();
 
/**
* Set the number of stop bits, once the port has been opened.
*
* @param stopBits
* @return
*/
private native boolean setStopBits( int stopBits );
 
private native boolean setCharSize( int charSize );
 
/**
* Input stream for the serial port.
* Set the character size, once theport has been opened.
*
* @param charSize
* @return
*/
private class SerialInputStream extends InputStream{
/* The handle to read from */
private int handle;
private native boolean setCharSize( int charSize );
/** Set the parity once the port has been opened.
*
* @param parity
* @return
*/
private native boolean setParity( int parity );
 
SerialInputStream( int handle ){
this.handle = handle;
}
 
@Override
public int read() throws IOException{
return readByte();
}
 
@Override
public int available() throws IOException{
return getAvailable();
}
 
private native int readByte() throws IOException;
 
private native int getAvailable() throws IOException;
}
 
private class SerialOutputStream extends OutputStream{
/* The handle to write to */
private int handle;
 
SerialOutputStream( int handle ){
this.handle = handle;
}
 
@Override
public void write(int b) throws IOException {
writeByte( b );
}
 
private native void writeByte( int toWrite ) throws IOException;
}
 
}
/JavaSerial/src/com/rm5248/serial/NotASerialPortException.java
0,0 → 1,17
package com.rm5248.serial;
 
public class NotASerialPortException extends Exception {
 
/**
* Generated by Eclipse
*/
private static final long serialVersionUID = 5606767768689212862L;
 
/** Create a new exception if this is not a serial port
*
* @param message
*/
public NotASerialPortException( String message ){
super( message );
}
}
/JavaSerial/src/com/rm5248/serial/SerialInputStream.java
0,0 → 1,31
package com.rm5248.serial;
 
import java.io.IOException;
import java.io.InputStream;
 
/**
* Input stream for the serial port.
*/
class SerialInputStream extends InputStream{
/* The handle to read from. Needed for native implementation */
@SuppressWarnings("unused")
private int handle;
 
SerialInputStream( int handle ){
this.handle = handle;
}
 
@Override
public int read() throws IOException{
return readByte();
}
 
@Override
public int available() throws IOException{
return getAvailable();
}
 
private native int readByte() throws IOException;
 
private native int getAvailable() throws IOException;
}
/JavaSerial/src/com/rm5248/serial/SerialOutputStream.java
0,0 → 1,21
package com.rm5248.serial;
 
import java.io.IOException;
import java.io.OutputStream;
 
class SerialOutputStream extends OutputStream{
/* The handle to write to */
@SuppressWarnings("unused")
private int handle;
 
SerialOutputStream( int handle ){
this.handle = handle;
}
 
@Override
public void write(int b) throws IOException {
writeByte( b );
}
 
private native void writeByte( int toWrite ) throws IOException;
}