| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
package gnu.io; |
| 59 |
import java.io.*; |
| 60 |
import java.util.*; |
| 61 |
import java.lang.Math; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
final class RS485 extends RS485Port { |
| 71 |
|
| 72 |
static |
| 73 |
{ |
| 74 |
System.loadLibrary( "rxtxRS485" ); |
| 75 |
Initialize(); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private native static void Initialize(); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public RS485( String name ) throws PortInUseException { |
| 88 |
fd = open( name ); |
| 89 |
} |
| 90 |
private native int open( String name ) throws PortInUseException; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private int fd; |
| 95 |
|
| 96 |
|
| 97 |
static boolean dsrFlag = false; |
| 98 |
|
| 99 |
|
| 100 |
private final RS485OutputStream out = new RS485OutputStream(); |
| 101 |
public OutputStream getOutputStream() { return out; } |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private final RS485InputStream in = new RS485InputStream(); |
| 106 |
public InputStream getInputStream() { return in; } |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void setRS485PortParams( int b, int d, int s, int p ) |
| 113 |
throws UnsupportedCommOperationException |
| 114 |
{ |
| 115 |
nativeSetRS485PortParams( b, d, s, p ); |
| 116 |
speed = b; |
| 117 |
dataBits = d; |
| 118 |
stopBits = s; |
| 119 |
parity = p; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
private native void nativeSetRS485PortParams( int speed, int dataBits, |
| 124 |
int stopBits, int parity ) throws UnsupportedCommOperationException; |
| 125 |
|
| 126 |
|
| 127 |
private int speed=9600; |
| 128 |
public int getBaudRate() { return speed; } |
| 129 |
|
| 130 |
|
| 131 |
private int dataBits=DATABITS_8; |
| 132 |
public int getDataBits() { return dataBits; } |
| 133 |
|
| 134 |
|
| 135 |
private int stopBits=RS485Port.STOPBITS_1; |
| 136 |
public int getStopBits() { return stopBits; } |
| 137 |
|
| 138 |
|
| 139 |
private int parity= RS485Port.PARITY_NONE; |
| 140 |
public int getParity() { return parity; } |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private int flowmode = RS485Port.FLOWCONTROL_NONE; |
| 145 |
public void setFlowControlMode( int flowcontrol ) { |
| 146 |
try { setflowcontrol( flowcontrol ); } |
| 147 |
catch( IOException e ) { |
| 148 |
e.printStackTrace(); |
| 149 |
return; |
| 150 |
} |
| 151 |
flowmode=flowcontrol; |
| 152 |
} |
| 153 |
public int getFlowControlMode() { return flowmode; } |
| 154 |
native void setflowcontrol( int flowcontrol ) throws IOException; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public void enableReceiveFraming( int f ) |
| 164 |
throws UnsupportedCommOperationException |
| 165 |
{ |
| 166 |
throw new UnsupportedCommOperationException( "Not supported" ); |
| 167 |
} |
| 168 |
public void disableReceiveFraming() {} |
| 169 |
public boolean isReceiveFramingEnabled() { return false; } |
| 170 |
public int getReceiveFramingByte() { return 0; } |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
private int timeout = 0; |
| 175 |
|
| 176 |
public native int NativegetReceiveTimeout(); |
| 177 |
public native boolean NativeisReceiveTimeoutEnabled(); |
| 178 |
public native void NativeEnableReceiveTimeoutThreshold(int time, int threshold,int InputBuffer); |
| 179 |
public void disableReceiveTimeout(){ |
| 180 |
enableReceiveTimeout(0); |
| 181 |
} |
| 182 |
public void enableReceiveTimeout( int time ){ |
| 183 |
if( time >= 0 ) { |
| 184 |
timeout = time; |
| 185 |
NativeEnableReceiveTimeoutThreshold( time , threshold, InputBuffer ); |
| 186 |
} |
| 187 |
else { |
| 188 |
System.out.println("Invalid timeout"); |
| 189 |
} |
| 190 |
} |
| 191 |
public boolean isReceiveTimeoutEnabled(){ |
| 192 |
return(NativeisReceiveTimeoutEnabled()); |
| 193 |
} |
| 194 |
public int getReceiveTimeout(){ |
| 195 |
return(NativegetReceiveTimeout( )); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
private int threshold = 0; |
| 201 |
|
| 202 |
public void enableReceiveThreshold( int thresh ){ |
| 203 |
if(thresh >=0) |
| 204 |
{ |
| 205 |
threshold=thresh; |
| 206 |
NativeEnableReceiveTimeoutThreshold(timeout, threshold, InputBuffer); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
System.out.println("Invalid Threshold"); |
| 211 |
} |
| 212 |
} |
| 213 |
public void disableReceiveThreshold() { |
| 214 |
enableReceiveThreshold(0); |
| 215 |
} |
| 216 |
public int getReceiveThreshold(){ |
| 217 |
return threshold; |
| 218 |
} |
| 219 |
public boolean isReceiveThresholdEnabled(){ |
| 220 |
return(threshold>0); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private int InputBuffer=0; |
| 231 |
private int OutputBuffer=0; |
| 232 |
public void setInputBufferSize( int size ) |
| 233 |
{ |
| 234 |
InputBuffer=size; |
| 235 |
} |
| 236 |
public int getInputBufferSize() |
| 237 |
{ |
| 238 |
return(InputBuffer); |
| 239 |
} |
| 240 |
public void setOutputBufferSize( int size ) |
| 241 |
{ |
| 242 |
OutputBuffer=size; |
| 243 |
} |
| 244 |
public int getOutputBufferSize() |
| 245 |
{ |
| 246 |
return(OutputBuffer); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
public native boolean isDTR(); |
| 251 |
public native void setDTR( boolean state ); |
| 252 |
public native void setRTS( boolean state ); |
| 253 |
private native void setDSR( boolean state ); |
| 254 |
public native boolean isCTS(); |
| 255 |
public native boolean isDSR(); |
| 256 |
public native boolean isCD(); |
| 257 |
public native boolean isRI(); |
| 258 |
public native boolean isRTS(); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public native void sendBreak( int duration ); |
| 263 |
private native void writeByte( int b ) throws IOException; |
| 264 |
private native void writeArray( byte b[], int off, int len ) |
| 265 |
throws IOException; |
| 266 |
private native void drain() throws IOException; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
private native int nativeavailable() throws IOException; |
| 271 |
private native int readByte() throws IOException; |
| 272 |
private native int readArray( byte b[], int off, int len ) |
| 273 |
throws IOException; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private RS485PortEventListener SPEventListener; |
| 278 |
|
| 279 |
|
| 280 |
private MonitorThread monThread; |
| 281 |
|
| 282 |
|
| 283 |
native void eventLoop(); |
| 284 |
private int dataAvailable=0; |
| 285 |
public void sendEvent( int event, boolean state ) { |
| 286 |
switch( event ) { |
| 287 |
case RS485PortEvent.DATA_AVAILABLE: |
| 288 |
dataAvailable=1; |
| 289 |
if( monThread.Data ) break; |
| 290 |
return; |
| 291 |
case RS485PortEvent.OUTPUT_BUFFER_EMPTY: |
| 292 |
if( monThread.Output ) break; |
| 293 |
return; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
case RS485PortEvent.CTS: |
| 312 |
if( monThread.CTS ) break; |
| 313 |
return; |
| 314 |
case RS485PortEvent.DSR: |
| 315 |
if( monThread.DSR ) break; |
| 316 |
return; |
| 317 |
case RS485PortEvent.RI: |
| 318 |
if( monThread.RI ) break; |
| 319 |
return; |
| 320 |
case RS485PortEvent.CD: |
| 321 |
if( monThread.CD ) break; |
| 322 |
return; |
| 323 |
case RS485PortEvent.OE: |
| 324 |
if( monThread.OE ) break; |
| 325 |
return; |
| 326 |
case RS485PortEvent.PE: |
| 327 |
if( monThread.PE ) break; |
| 328 |
return; |
| 329 |
case RS485PortEvent.FE: |
| 330 |
if( monThread.FE ) break; |
| 331 |
return; |
| 332 |
case RS485PortEvent.BI: |
| 333 |
if( monThread.BI ) break; |
| 334 |
return; |
| 335 |
default: |
| 336 |
System.err.println("unknown event:"+event); |
| 337 |
return; |
| 338 |
} |
| 339 |
RS485PortEvent e = new RS485PortEvent(this, event, !state, state ); |
| 340 |
if( SPEventListener != null ) SPEventListener.RS485Event( e ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
public void addEventListener( RS485PortEventListener lsnr ) |
| 345 |
throws TooManyListenersException |
| 346 |
{ |
| 347 |
if( SPEventListener != null ) throw new TooManyListenersException(); |
| 348 |
SPEventListener = lsnr; |
| 349 |
monThread = new MonitorThread(); |
| 350 |
monThread.start(); |
| 351 |
} |
| 352 |
|
| 353 |
public void removeEventListener() { |
| 354 |
SPEventListener = null; |
| 355 |
if( monThread != null ) { |
| 356 |
monThread.interrupt(); |
| 357 |
monThread = null; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
public void notifyOnDataAvailable( boolean enable ) { monThread.Data = enable; } |
| 362 |
|
| 363 |
public void notifyOnOutputEmpty( boolean enable ) { monThread.Output = enable; } |
| 364 |
|
| 365 |
public void notifyOnCTS( boolean enable ) { monThread.CTS = enable; } |
| 366 |
public void notifyOnDSR( boolean enable ) { monThread.DSR = enable; } |
| 367 |
public void notifyOnRingIndicator( boolean enable ) { monThread.RI = enable; } |
| 368 |
public void notifyOnCarrierDetect( boolean enable ) { monThread.CD = enable; } |
| 369 |
public void notifyOnOverrunError( boolean enable ) { monThread.OE = enable; } |
| 370 |
public void notifyOnParityError( boolean enable ) { monThread.PE = enable; } |
| 371 |
public void notifyOnFramingError( boolean enable ) { monThread.FE = enable; } |
| 372 |
public void notifyOnBreakInterrupt( boolean enable ) { monThread.BI = enable; } |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private native void nativeClose(); |
| 377 |
public void close() { |
| 378 |
setDTR(false); |
| 379 |
setDSR(false); |
| 380 |
nativeClose(); |
| 381 |
super.close(); |
| 382 |
fd = 0; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
protected void finalize() { |
| 388 |
if( fd > 0 ) close(); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
class RS485OutputStream extends OutputStream { |
| 394 |
public void write( int b ) throws IOException { |
| 395 |
writeByte( b ); |
| 396 |
} |
| 397 |
public void write( byte b[] ) throws IOException { |
| 398 |
writeArray( b, 0, b.length ); |
| 399 |
} |
| 400 |
public void write( byte b[], int off, int len ) throws IOException { |
| 401 |
writeArray( b, off, len ); |
| 402 |
} |
| 403 |
public void flush() throws IOException { |
| 404 |
drain(); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
class RS485InputStream extends InputStream { |
| 410 |
public int read() throws IOException { |
| 411 |
dataAvailable=0; |
| 412 |
return readByte(); |
| 413 |
} |
| 414 |
public int read( byte b[] ) throws IOException |
| 415 |
{ |
| 416 |
return read ( b, 0, b.length); |
| 417 |
} |
| 418 |
public int read( byte b[], int off, int len ) throws IOException |
| 419 |
{ |
| 420 |
dataAvailable=0; |
| 421 |
int i=0, Minimum=0; |
| 422 |
int intArray[] = |
| 423 |
{ |
| 424 |
b.length, |
| 425 |
InputBuffer, |
| 426 |
len |
| 427 |
}; |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
while(intArray[i]==0 && i < intArray.length) i++; |
| 435 |
Minimum=intArray[i]; |
| 436 |
while( i < intArray.length ) |
| 437 |
{ |
| 438 |
if(intArray[i] > 0 ) |
| 439 |
{ |
| 440 |
Minimum=Math.min(Minimum,intArray[i]); |
| 441 |
} |
| 442 |
i++; |
| 443 |
} |
| 444 |
Minimum=Math.min(Minimum,threshold); |
| 445 |
if(Minimum == 0) Minimum=1; |
| 446 |
int Available=available(); |
| 447 |
int Ret = readArray( b, off, Minimum); |
| 448 |
return Ret; |
| 449 |
} |
| 450 |
public int available() throws IOException { |
| 451 |
return nativeavailable(); |
| 452 |
} |
| 453 |
} |
| 454 |
class MonitorThread extends Thread { |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
private boolean CTS=false; |
| 459 |
private boolean DSR=false; |
| 460 |
private boolean RI=false; |
| 461 |
private boolean CD=false; |
| 462 |
private boolean OE=false; |
| 463 |
private boolean PE=false; |
| 464 |
private boolean FE=false; |
| 465 |
private boolean BI=false; |
| 466 |
private boolean Data=false; |
| 467 |
private boolean Output=false; |
| •468 |
MonitorThread() { |
| 469 |
setDaemon(true); |
| 470 |
} |
| 471 |
public void run() { |
| 472 |
eventLoop(); |
| 473 |
} |
| 474 |
} |
| 475 |
} |