import java.util.Enumeration;import javax.comm.CommPortIdentifier; import javax.comm.NoSuchPortException; import javax.comm.PortInUseException; import javax.comm.SerialPort;public class RTS { CommPortIdentifier port; SerialPort sp; Enumeration en; SerialParameters parameters; /** * @param args */ public static void main(String[] args) { RTS rtsTest = new RTS(); out("comport: " + commPort); rtsTest.listPort(); rtsTest.openPort("COM1"); //set RTS true rtsTest.getSp().setRTS(true); //out("rts is: " + rtsTest.getSp().isRTS()); //out("baud rate is: " + rtsTest.getSp().getBaudRate()); try { // out("before 0.5s time is: " + System.currentTimeMillis()); Thread.sleep(500); // out("after 0.5s time is: " + System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } //set RTS false rtsTest.getSp().setRTS(false); //out("rts is: " + rtsTest.getSp().isRTS()); //out("baud rate is: " + rtsTest.getSp().getBaudRate()); //out("port is owned: " + rtsTest.getPort().isCurrentlyOwned()); //close the port out("close the port: " + rtsTest.getSp().getName()); rtsTest.getSp().close(); //out("port is owned: " + rtsTest.getPort().isCurrentlyOwned()); } public static void out(Object o) { System.out.println(o); } public void openPort(String commPort) { this.parameters = new SerialParameters(commPort, 9600, SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); try { this.port = CommPortIdentifier.getPortIdentifier(this.parameters .getPortName()); out("current opened port: " + this.parameters.getPortName()); } catch (NoSuchPortException e1) { e1.printStackTrace(); } try { this.sp = (SerialPort) this.port.open("PvsClient", 60); } catch (PortInUseException e) { e.printStackTrace(); } } public void listPort() { this.en = CommPortIdentifier.getPortIdentifiers(); out("usable com port is: "); while (this.en.hasMoreElements()) { this.port = (CommPortIdentifier) this.en.nextElement(); if (this.port.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println(this.port.getName()); } } } public SerialPort getSp() { return this.sp; } public void setSp(SerialPort sp) { this.sp = sp; } public CommPortIdentifier getPort() { return this.port; } public void setPort(CommPortIdentifier port) { this.port = port; }}
import javax.comm.SerialPort;/** A class that stores parameters for serial ports. */ public class SerialParameters { private String portName; private int baudRate; private int flowControlIn; private int flowControlOut; private int databits; private int stopbits; private int parity; /** Default constructer. Sets parameters to no port, 9600 baud, no flow control, 8 data bits, 1 stop bit, no parity. */ public SerialParameters() { this("", 9600, SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } /** Paramaterized constructer. @param portName The name of the port. @param baudRate The baud rate. @param flowControlIn Type of flow control for receiving. @param flowControlOut Type of flow control for sending. @param databits The number of data bits. @param stopbits The number of stop bits. @param parity The type of parity. */ public SerialParameters(String portName, int baudRate, int flowControlIn, int flowControlOut, int databits, int stopbits, int parity) { this.portName = portName; this.baudRate = baudRate; this.flowControlIn = flowControlIn; this.flowControlOut = flowControlOut; this.databits = databits; this.stopbits = stopbits; this.parity = parity; } /** Sets port name. @param portName New port name. */ public void setPortName(String portName) { this.portName = portName; } /** Gets port name. @return Current port name. */ public String getPortName() { return this.portName; } /** Sets baud rate. @param baudRate New baud rate. */ public void setBaudRate(int baudRate) { this.baudRate = baudRate; } /** Sets baud rate. @param baudRate New baud rate. */ public void setBaudRate(String baudRate) { this.baudRate = Integer.parseInt(baudRate); } /** Gets baud rate as an <code>int</code>. @return Current baud rate. */ public int getBaudRate() { return this.baudRate; } /** Gets baud rate as a <code>String</code>. @return Current baud rate. */ public String getBaudRateString() { return Integer.toString(this.baudRate); } /** Sets flow control for reading. @param flowControlIn New flow control for reading type. */ public void setFlowControlIn(int flowControlIn) { this.flowControlIn = flowControlIn; } /** Sets flow control for reading. @param flowControlIn New flow control for reading type. */ public void setFlowControlIn(String flowControlIn) { this.flowControlIn = this.stringToFlow(flowControlIn); } /** Gets flow control for reading as an <code>int</code>. @return Current flow control type. */ public int getFlowControlIn() { return this.flowControlIn; } /** Gets flow control for reading as a <code>String</code>. @return Current flow control type. */ public String getFlowControlInString() { return this.flowToString(this.flowControlIn); } /** Sets flow control for writing. @param flowControlIn New flow control for writing type. */ public void setFlowControlOut(int flowControlOut) { this.flowControlOut = flowControlOut; } /** Sets flow control for writing. @param flowControlIn New flow control for writing type. */ public void setFlowControlOut(String flowControlOut) { this.flowControlOut = this.stringToFlow(flowControlOut); } /** Gets flow control for writing as an <code>int</code>. @return Current flow control type. */ public int getFlowControlOut() { return this.flowControlOut; } /** Gets flow control for writing as a <code>String</code>. @return Current flow control type. */ public String getFlowControlOutString() { return this.flowToString(this.flowControlOut); } /** Sets data bits. @param databits New data bits setting. */ public void setDatabits(int databits) { this.databits = databits; } /** Sets data bits. @param databits New data bits setting. */ public void setDatabits(String databits) { if (databits.equals("5")) { this.databits = SerialPort.DATABITS_5; } if (databits.equals("6")) { this.databits = SerialPort.DATABITS_6; } if (databits.equals("7")) { this.databits = SerialPort.DATABITS_7; } if (databits.equals("8")) { this.databits = SerialPort.DATABITS_8; } } /** Gets data bits as an <code>int</code>. @return Current data bits setting. */ public int getDatabits() { return this.databits; } /** Gets data bits as a <code>String</code>. @return Current data bits setting. */ public String getDatabitsString() { switch (this.databits) { case SerialPort.DATABITS_5: return "5"; case SerialPort.DATABITS_6: return "6"; case SerialPort.DATABITS_7: return "7"; case SerialPort.DATABITS_8: return "8"; default: return "8"; } } /** Sets stop bits. @param stopbits New stop bits setting. */ public void setStopbits(int stopbits) { this.stopbits = stopbits; } /** Sets stop bits. @param stopbits New stop bits setting. */ public void setStopbits(String stopbits) { if (stopbits.equals("1")) { this.stopbits = SerialPort.STOPBITS_1; } if (stopbits.equals("1.5")) { this.stopbits = SerialPort.STOPBITS_1_5; } if (stopbits.equals("2")) { this.stopbits = SerialPort.STOPBITS_2; } }
/** Gets stop bits setting as an <code>int</code>. @return Current stop bits setting. */ public int getStopbits() { return this.stopbits; } /** Gets stop bits setting as a <code>String</code>. @return Current stop bits setting. */ public String getStopbitsString() { switch (this.stopbits) { case SerialPort.STOPBITS_1: return "1"; case SerialPort.STOPBITS_1_5: return "1.5"; case SerialPort.STOPBITS_2: return "2"; default: return "1"; } } /** Sets parity setting. @param parity New parity setting. */ public void setParity(int parity) { this.parity = parity; } /** Sets parity setting. @param parity New parity setting. */ public void setParity(String parity) { if (parity.equals("None")) { this.parity = SerialPort.PARITY_NONE; } if (parity.equals("Even")) { this.parity = SerialPort.PARITY_EVEN; } if (parity.equals("Odd")) { this.parity = SerialPort.PARITY_ODD; } } /** Gets parity setting as an <code>int</code>. @return Current parity setting. */ public int getParity() { return this.parity; } /** Gets parity setting as a <code>String</code>. @return Current parity setting. */ public String getParityString() { switch (this.parity) { case SerialPort.PARITY_NONE: return "None"; case SerialPort.PARITY_EVEN: return "Even"; case SerialPort.PARITY_ODD: return "Odd"; default: return "None"; } } /** Converts a <code>String</code> describing a flow control type to an <code>int</code> type defined in <code>SerialPort</code>. @param flowControl A <code>string</code> describing a flow control type. @return An <code>int</code> describing a flow control type. */ private int stringToFlow(String flowControl) { if (flowControl.equals("None")) { return SerialPort.FLOWCONTROL_NONE; } if (flowControl.equals("Xon/Xoff Out")) { return SerialPort.FLOWCONTROL_XONXOFF_OUT; } if (flowControl.equals("Xon/Xoff In")) { return SerialPort.FLOWCONTROL_XONXOFF_IN; } if (flowControl.equals("RTS/CTS In")) { return SerialPort.FLOWCONTROL_RTSCTS_IN; } if (flowControl.equals("RTS/CTS Out")) { return SerialPort.FLOWCONTROL_RTSCTS_OUT; } return SerialPort.FLOWCONTROL_NONE; } /** Converts an <code>int</code> describing a flow control type to a <code>String</code> describing a flow control type. @param flowControl An <code>int</code> describing a flow control type. @return A <code>String</code> describing a flow control type. */ String flowToString(int flowControl) { switch (flowControl) { case SerialPort.FLOWCONTROL_NONE: return "None"; case SerialPort.FLOWCONTROL_XONXOFF_OUT: return "Xon/Xoff Out"; case SerialPort.FLOWCONTROL_XONXOFF_IN: return "Xon/Xoff In"; case SerialPort.FLOWCONTROL_RTSCTS_IN: return "RTS/CTS In"; case SerialPort.FLOWCONTROL_RTSCTS_OUT: return "RTS/CTS Out"; default: return "None"; } } }
import java.util.Enumeration;import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;public class RTS
{ CommPortIdentifier port; SerialPort sp; Enumeration en; SerialParameters parameters; /**
* @param args
*/
public static void main(String[] args)
{
RTS rtsTest = new RTS();
out("comport: " + commPort);
rtsTest.listPort();
rtsTest.openPort("COM1"); //set RTS true
rtsTest.getSp().setRTS(true);
//out("rts is: " + rtsTest.getSp().isRTS());
//out("baud rate is: " + rtsTest.getSp().getBaudRate()); try
{
// out("before 0.5s time is: " + System.currentTimeMillis());
Thread.sleep(500);
// out("after 0.5s time is: " + System.currentTimeMillis());
}
catch (InterruptedException e)
{
e.printStackTrace();
} //set RTS false
rtsTest.getSp().setRTS(false);
//out("rts is: " + rtsTest.getSp().isRTS());
//out("baud rate is: " + rtsTest.getSp().getBaudRate());
//out("port is owned: " + rtsTest.getPort().isCurrentlyOwned()); //close the port
out("close the port: " + rtsTest.getSp().getName());
rtsTest.getSp().close();
//out("port is owned: " + rtsTest.getPort().isCurrentlyOwned());
} public static void out(Object o)
{
System.out.println(o);
} public void openPort(String commPort)
{
this.parameters = new SerialParameters(commPort, 9600,
SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE); try
{
this.port = CommPortIdentifier.getPortIdentifier(this.parameters
.getPortName());
out("current opened port: " + this.parameters.getPortName());
}
catch (NoSuchPortException e1)
{
e1.printStackTrace();
} try
{
this.sp = (SerialPort) this.port.open("PvsClient", 60);
}
catch (PortInUseException e)
{
e.printStackTrace();
}
} public void listPort()
{
this.en = CommPortIdentifier.getPortIdentifiers(); out("usable com port is: ");
while (this.en.hasMoreElements())
{
this.port = (CommPortIdentifier) this.en.nextElement(); if (this.port.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(this.port.getName());
}
}
} public SerialPort getSp()
{
return this.sp;
} public void setSp(SerialPort sp)
{
this.sp = sp;
} public CommPortIdentifier getPort()
{
return this.port;
} public void setPort(CommPortIdentifier port)
{
this.port = port;
}}
A class that stores parameters for serial ports.
*/
public class SerialParameters
{ private String portName; private int baudRate; private int flowControlIn; private int flowControlOut; private int databits; private int stopbits; private int parity; /**
Default constructer. Sets parameters to no port, 9600 baud, no flow
control, 8 data bits, 1 stop bit, no parity.
*/
public SerialParameters()
{
this("", 9600, SerialPort.FLOWCONTROL_NONE,
SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } /**
Paramaterized constructer. @param portName The name of the port.
@param baudRate The baud rate.
@param flowControlIn Type of flow control for receiving.
@param flowControlOut Type of flow control for sending.
@param databits The number of data bits.
@param stopbits The number of stop bits.
@param parity The type of parity.
*/
public SerialParameters(String portName, int baudRate, int flowControlIn,
int flowControlOut, int databits, int stopbits, int parity)
{
this.portName = portName;
this.baudRate = baudRate;
this.flowControlIn = flowControlIn;
this.flowControlOut = flowControlOut;
this.databits = databits;
this.stopbits = stopbits;
this.parity = parity;
} /**
Sets port name.
@param portName New port name.
*/
public void setPortName(String portName)
{
this.portName = portName;
} /**
Gets port name.
@return Current port name.
*/
public String getPortName()
{
return this.portName;
} /**
Sets baud rate.
@param baudRate New baud rate.
*/
public void setBaudRate(int baudRate)
{
this.baudRate = baudRate;
} /**
Sets baud rate.
@param baudRate New baud rate.
*/
public void setBaudRate(String baudRate)
{
this.baudRate = Integer.parseInt(baudRate);
} /**
Gets baud rate as an <code>int</code>.
@return Current baud rate.
*/
public int getBaudRate()
{
return this.baudRate;
} /**
Gets baud rate as a <code>String</code>.
@return Current baud rate.
*/
public String getBaudRateString()
{
return Integer.toString(this.baudRate);
} /**
Sets flow control for reading.
@param flowControlIn New flow control for reading type.
*/
public void setFlowControlIn(int flowControlIn)
{
this.flowControlIn = flowControlIn;
} /**
Sets flow control for reading.
@param flowControlIn New flow control for reading type.
*/
public void setFlowControlIn(String flowControlIn)
{
this.flowControlIn = this.stringToFlow(flowControlIn);
} /**
Gets flow control for reading as an <code>int</code>.
@return Current flow control type.
*/
public int getFlowControlIn()
{
return this.flowControlIn;
} /**
Gets flow control for reading as a <code>String</code>.
@return Current flow control type.
*/
public String getFlowControlInString()
{
return this.flowToString(this.flowControlIn);
} /**
Sets flow control for writing.
@param flowControlIn New flow control for writing type.
*/
public void setFlowControlOut(int flowControlOut)
{
this.flowControlOut = flowControlOut;
} /**
Sets flow control for writing.
@param flowControlIn New flow control for writing type.
*/
public void setFlowControlOut(String flowControlOut)
{
this.flowControlOut = this.stringToFlow(flowControlOut);
} /**
Gets flow control for writing as an <code>int</code>.
@return Current flow control type.
*/
public int getFlowControlOut()
{
return this.flowControlOut;
} /**
Gets flow control for writing as a <code>String</code>.
@return Current flow control type.
*/
public String getFlowControlOutString()
{
return this.flowToString(this.flowControlOut);
} /**
Sets data bits.
@param databits New data bits setting.
*/
public void setDatabits(int databits)
{
this.databits = databits;
} /**
Sets data bits.
@param databits New data bits setting.
*/
public void setDatabits(String databits)
{
if (databits.equals("5"))
{
this.databits = SerialPort.DATABITS_5;
}
if (databits.equals("6"))
{
this.databits = SerialPort.DATABITS_6;
}
if (databits.equals("7"))
{
this.databits = SerialPort.DATABITS_7;
}
if (databits.equals("8"))
{
this.databits = SerialPort.DATABITS_8;
}
} /**
Gets data bits as an <code>int</code>.
@return Current data bits setting.
*/
public int getDatabits()
{
return this.databits;
} /**
Gets data bits as a <code>String</code>.
@return Current data bits setting.
*/
public String getDatabitsString()
{
switch (this.databits)
{
case SerialPort.DATABITS_5:
return "5";
case SerialPort.DATABITS_6:
return "6";
case SerialPort.DATABITS_7:
return "7";
case SerialPort.DATABITS_8:
return "8";
default:
return "8";
}
} /**
Sets stop bits.
@param stopbits New stop bits setting.
*/
public void setStopbits(int stopbits)
{
this.stopbits = stopbits;
} /**
Sets stop bits.
@param stopbits New stop bits setting.
*/
public void setStopbits(String stopbits)
{
if (stopbits.equals("1"))
{
this.stopbits = SerialPort.STOPBITS_1;
}
if (stopbits.equals("1.5"))
{
this.stopbits = SerialPort.STOPBITS_1_5;
}
if (stopbits.equals("2"))
{
this.stopbits = SerialPort.STOPBITS_2;
}
}
/**
Gets stop bits setting as an <code>int</code>.
@return Current stop bits setting.
*/
public int getStopbits()
{
return this.stopbits;
} /**
Gets stop bits setting as a <code>String</code>.
@return Current stop bits setting.
*/
public String getStopbitsString()
{
switch (this.stopbits)
{
case SerialPort.STOPBITS_1:
return "1";
case SerialPort.STOPBITS_1_5:
return "1.5";
case SerialPort.STOPBITS_2:
return "2";
default:
return "1";
}
} /**
Sets parity setting.
@param parity New parity setting.
*/
public void setParity(int parity)
{
this.parity = parity;
} /**
Sets parity setting.
@param parity New parity setting.
*/
public void setParity(String parity)
{
if (parity.equals("None"))
{
this.parity = SerialPort.PARITY_NONE;
}
if (parity.equals("Even"))
{
this.parity = SerialPort.PARITY_EVEN;
}
if (parity.equals("Odd"))
{
this.parity = SerialPort.PARITY_ODD;
}
} /**
Gets parity setting as an <code>int</code>.
@return Current parity setting.
*/
public int getParity()
{
return this.parity;
} /**
Gets parity setting as a <code>String</code>.
@return Current parity setting.
*/
public String getParityString()
{
switch (this.parity)
{
case SerialPort.PARITY_NONE:
return "None";
case SerialPort.PARITY_EVEN:
return "Even";
case SerialPort.PARITY_ODD:
return "Odd";
default:
return "None";
}
} /**
Converts a <code>String</code> describing a flow control type to an
<code>int</code> type defined in <code>SerialPort</code>.
@param flowControl A <code>string</code> describing a flow control type.
@return An <code>int</code> describing a flow control type.
*/
private int stringToFlow(String flowControl)
{
if (flowControl.equals("None"))
{
return SerialPort.FLOWCONTROL_NONE;
}
if (flowControl.equals("Xon/Xoff Out"))
{
return SerialPort.FLOWCONTROL_XONXOFF_OUT;
}
if (flowControl.equals("Xon/Xoff In"))
{
return SerialPort.FLOWCONTROL_XONXOFF_IN;
}
if (flowControl.equals("RTS/CTS In"))
{
return SerialPort.FLOWCONTROL_RTSCTS_IN;
}
if (flowControl.equals("RTS/CTS Out"))
{
return SerialPort.FLOWCONTROL_RTSCTS_OUT;
}
return SerialPort.FLOWCONTROL_NONE;
} /**
Converts an <code>int</code> describing a flow control type to a
<code>String</code> describing a flow control type.
@param flowControl An <code>int</code> describing a flow control type.
@return A <code>String</code> describing a flow control type.
*/
String flowToString(int flowControl)
{
switch (flowControl)
{
case SerialPort.FLOWCONTROL_NONE:
return "None";
case SerialPort.FLOWCONTROL_XONXOFF_OUT:
return "Xon/Xoff Out";
case SerialPort.FLOWCONTROL_XONXOFF_IN:
return "Xon/Xoff In";
case SerialPort.FLOWCONTROL_RTSCTS_IN:
return "RTS/CTS In";
case SerialPort.FLOWCONTROL_RTSCTS_OUT:
return "RTS/CTS Out";
default:
return "None";
}
}
}
http://www.programmerstudy.com/programme/java/20084/97.html