📄 serialportserver.java
字号:
/*---------------------------------------------------------------------------
* Copyright (C) 2001 Dallas Semiconductor Corporation, All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Dallas Semiconductor
* shall not be used except as stated in the Dallas Semiconductor
* Branding Policy.
*---------------------------------------------------------------------------
*/
package com.eta.etatcpcomm.server;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.comm.*;
import com.eta.etatcpcomm.*;
/* This class allows us to perform operations on the SerialPort object that this
* server is talking to. It also listens for Serial events and then notifies
* (through the event socket) the client that such an event occurred.
*/
public class SerialPortServer extends CommPortServer implements SerialPortEventListener, Constants
{
protected DataOutputStream eventOutputStream;
// Package only access. Instances can only be obtained
// from CommPortServer.createPortServer().
SerialPortServer( CommPortIdentifier commPortIdentifier, int tcpPort)
{
super(commPortIdentifier, tcpPort);
}
public int getPortType()
{
return CommPortIdentifier.PORT_SERIAL;
}
/* Interface method for SerialPortEventListener,
* just turn around and send the event.
*/
public void serialEvent(SerialPortEvent spev)
{
int eventType = spev.getEventType();
if (eventType != SerialPortEvent.DATA_AVAILABLE)
{
sendEvent(eventType,
(spev.getOldValue() ? 2 : 0) |
(spev.getNewValue() ? 1 : 0));
}
}
protected void dataAvailableNotification(int bytesAvailable)
{
// Only send DATA_AVAILABLE notification if bytesAvailable is
// greater than zero
if( bytesAvailable > 0)
{
if( DEBUG) System.out.println( "Sent DATA_AVAILABLE " + bytesAvailable);
sendEvent(SerialPortEvent.DATA_AVAILABLE, bytesAvailable);
}
}
// This method exists soley for subclasses that need to know when
// data has been read from the socket and written to the comm port
// so they can, for example, inform the other end that data was written.
protected void dataWrittenNotification(int bytesWritten)
{
//the data written event back to the client lets its output stream
//know that its data has been recieved
if( DEBUG) System.out.println( "Sent DATA_WRITTEN " + bytesWritten);
sendEvent(DATA_WRITTEN, bytesWritten);
}
/* We use a specially dedicated socket to send events from the server to
* the client. Events consist of two integers: an event type and a value.
*/
protected void sendEvent(int eventType, int values)
{
if (eventSocket != null)
{
// All access to event socket and output stream
// must be synchronized on event socket
synchronized (eventSocket)
{
if (eventOutputStream != null)
{
// Send event
try
{
eventOutputStream.writeInt(eventType);
eventOutputStream.writeInt(values);
eventOutputStream.flush();
if( DEBUG) System.out.println( "Event " + eventType + ", " + values);
}
catch (IOException ioe)
{
try
{
eventSocket.close();
}
catch (IOException ioe2) { /* Ignore */ }
eventOutputStream = null;
eventSocket = null;
}
}
}
}
}
public void addListeners()
{
/* this method is necessary because we want to add the listener ONLY once,
since we only open the serial port once */
try
{
((SerialPort)cp).addEventListener(this);
}
catch (TooManyListenersException tmle)
{
if( DEBUG) System.out.println( "Too many listeners!");
}
}
/* Called from the CommPortServer class after it has gotten a connection
* on its listening socket
*/
protected void initialize( ServerSocket ss) throws IOException, PortInUseException
{
super.initialize(ss);
/*
if we ever want to run this where we open the serial port only when
requested rather than just keeping it open, we will need to add
our listener here...we'll also have to remove the listener too...
try
{
((SerialPort)cp).addEventListener(this);
}
catch (TooManyListenersException tmle)
{
if( DEBUG) System.out.println( "Too many listeners!");
}*/
eventOutputStream = eventSocketOutputStream;
}
protected void handleRequest(int cmd, DataInputStream cis, DataOutputStream cos)
throws IOException, UnsupportedCommOperationException
{
int p0;
int retVal = 0;
SerialPort sp = (SerialPort)cp;
switch (cmd)
{
case GET_BAUD_RATE:
retVal = sp.getBaudRate();
break;
case GET_DATA_BITS:
retVal = sp.getDataBits();
break;
case GET_STOP_BITS:
retVal = sp.getStopBits();
break;
case GET_PARITY:
retVal = sp.getParity();
break;
case SEND_BREAK:
p0 = cis.readInt();
sp.sendBreak(p0);
break;
case SET_FLOW_CONTROL_MODE:
p0 = cis.readInt();
sp.setFlowControlMode(p0);
break;
case GET_FLOW_CONTROL_MODE:
retVal = sp.getFlowControlMode();
break;
case SET_SERIAL_PORT_PARAMS:
int p3 = cis.readInt();
int p2 = cis.readInt();
int p1 = cis.readInt();
p0 = cis.readInt();
sp.setSerialPortParams(p0, p1, p2, p3);
break;
case SET_DTR:
p0 = cis.readInt();
if (DEBUG) System.out.println("setDTR "+(p0==TRUE));
sp.setDTR(p0 == TRUE);
break;
case IS_DTR:
retVal = sp.isDTR() ? TRUE : FALSE;
break;
case SET_RTS:
p0 = cis.readInt();
sp.setRTS(p0 == TRUE);
break;
case IS_RTS:
retVal = sp.isRTS() ? TRUE : FALSE;
break;
case IS_CTS:
retVal = sp.isCTS() ? TRUE : FALSE;
break;
case IS_DSR:
retVal = sp.isDSR() ? TRUE : FALSE;
if (DEBUG) System.out.println("isDTR??? "+(retVal==TRUE));
break;
case IS_RI:
retVal = sp.isRI() ? TRUE : FALSE;
break;
case IS_CD:
retVal = sp.isCD() ? TRUE : FALSE;
break;
case NOTIFY_ON_DATA_AVAILABLE:
p0 = cis.readInt();
sp.notifyOnDataAvailable(p0 == TRUE);
break;
case NOTIFY_ON_OUTPUT_EMPTY:
p0 = cis.readInt();
sp.notifyOnOutputEmpty(p0 == TRUE);
break;
case NOTIFY_ON_CTS:
p0 = cis.readInt();
sp.notifyOnCTS(p0 == TRUE);
break;
case NOTIFY_ON_DSR:
p0 = cis.readInt();
sp.notifyOnDSR(p0 == TRUE);
break;
case NOTIFY_ON_RING_INDICATOR:
p0 = cis.readInt();
sp.notifyOnRingIndicator(p0 == TRUE);
break;
case NOTIFY_ON_CARRIER_DETECT:
p0 = cis.readInt();
sp.notifyOnCarrierDetect(p0 == TRUE);
break;
case NOTIFY_ON_OVERRUN_ERROR:
p0 = cis.readInt();
sp.notifyOnOverrunError(p0 == TRUE);
break;
case NOTIFY_ON_PARITY_ERROR:
p0 = cis.readInt();
sp.notifyOnParityError(p0 == TRUE);
break;
case NOTIFY_ON_FRAMING_ERROR:
p0 = cis.readInt();
sp.notifyOnFramingError(p0 == TRUE);
break;
case NOTIFY_ON_BREAK_INTERRUPT:
p0 = cis.readInt();
sp.notifyOnBreakInterrupt(p0 == TRUE);
break;
default:
super.handleRequest(cmd, cis, cos);
return;
}
cos.writeInt(OK);
cos.writeInt(retVal);
}
private static final boolean DEBUG = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -