所以,我有一个卡锁系统设备,通过RS-232 DB9串口连接。 这是我第一次使用它来处理外部设备。所以我阅读了手册 它说传输过程文本格式定义如下:
- 文本必须包含STX和ETC之间最多500个字符
-LRC计算区域范围从STX到ETX的第一个字符
还有一个控制字符列表(STX,ETX,ACK,NAK)及其十六进制代码。
我对此一无所知。请赐教。 哦,我是否可以检测设备是否连接到特定端口?
我已设法使用以下代码连接到通讯端口:
public class TwoWaySerialComm
{
protected InputStream inputStream;
protected OutputStream outputStream;
public TwoWaySerialComm()
{
super();
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_7,SerialPort.STOPBITS_1,SerialPort.PARITY_ODD);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
(new Thread(new SerialReader(inputStream))).start();
(new Thread(new SerialWriter(outputStream))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public OutputStream getOutputStream() {
return outputStream;
}
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
/** */
public static class SerialReader implements Runnable
{
InputStream in;
public SerialReader ( InputStream in )
{
this.in = in;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 )
{
System.out.print(new String(buffer,0,len));
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
/** */
public static class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public void run ()
{
try
{
int c = 0;
while ( ( c = System.in.read()) > -1 )
{
this.out.write(c);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
public static void main ( String[] args )
{
try
{
TwoWaySerialComm comm = new TwoWaySerialComm();
comm.connect("COM3");
}
catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我发现的用于从字节数组中获取LRC的代码:
public byte calculateLRC(byte[] data)
{
byte checksum = 0;
for (int i = 0; i <= data.length - 1; i++) {
checksum = (byte) ((checksum + data[i]) & 0xFF);
}
checksum = (byte) (((checksum ^ 0xFF) + 1) & 0xFF);
return checksum;
}
现在我必须正确地将“CES01”文本发送到设备,我该怎么做?
答案 0 :(得分:1)
STX :start character
ETX: end character
ACK : Acknowledgement
char STX = 0x02;
char ETX = 0x03;
char EOT = 0x04;
char ENQ = 0x05;
char ACK = 0x06;
另请参阅LIS / RIS消息格式的链接
发送确认
public void sendAcknowledgement()throws IOException{
String ack= ASCIITable.ACK + "";
outputStream.write(ack.getBytes());
}
答案 1 :(得分:1)
如果您对串行通信完全没有经验,我将从研究现有的已知工作实现开始。 John Ellinwood对this SO question的回答包含XModem协议的Java源代码,这是一种旧的,简单的调制解调器文件传输协议。这将向您展示如何处理通信本身(您必须为超时和其他错误条件做好准备)和控制字符。
你不能忘记的Java的一种特殊性是Java中没有无符号类型,如果您拥有的唯一变量类型都已签名,那么这些低级协议几乎总是变得有点复杂。但正如XModem代码所示,它是完全可行的。
答案 2 :(得分:0)
OutputStream写入字节,因此您必须先将字符串转换为字节数组:
outputStream.write("CES01".getBytes());