使用java的NFC读卡器进行串行通信



我有一个NFC阅读器,它附带了用于通信的SDK。我可以使用VB、VC++或C#与供应商提供的dll通过USB或RS232进行通信。读取器可以在不使用SDK的情况下通过RS 232使用C/C++进行通信。我正在尝试使用java进行串行通信。我可以使用RS 232发送命令,但我没有从读取器得到响应。例如,我发送一个命令来检测卡的存在。如果存在卡,读卡器应返回卡序列号,否则将返回一些错误代码。但我一无所获。这里附上的是我的样本代码检测卡。只要告诉我代码是否正确,或者我需要改变与读者沟通的方法。

package com.pramod.serialcomm;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;`enter code here`
public class TwoWaySerialComm {
InputStream in ;
OutputStream out;
void connect( String portName ) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier( portName );
if( portIdentifier.isCurrentlyOwned() ) {
System.out.println( "Error: Port is currently in use" );
} else {
int timeout = 2000;
CommPort commPort = portIdentifier.open( this.getClass().getName(), timeout );
System.out.println( "Success: Port currently in use : " + commPort );
if( commPort instanceof SerialPort ) {
SerialPort serialPort = ( SerialPort )commPort;
serialPort.setSerialPortParams( 115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE );
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
( new Thread( new SerialReader( in ) ) ).start();
( new Thread( new SerialWriter( out ) ) ).start();

} else {
System.out.println( "Error: Only serial ports are handled by this example." );
}
}
}
public static class SerialReader implements  SerialPortEventListener,Runnable {
InputStream in;
private byte[] buffer = new byte[1024];
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.println("reading : " +  new String( buffer, 0, len ) );
}
} catch( IOException e ) {
e.printStackTrace();
}
}
@Override
public void serialEvent(SerialPortEvent arg0) {

int data;
try {
int len = 0;
while ((data = in.read()) > -1) {
if (data == 'n') {
break;
}
buffer[len++] = (byte) data;
}
synchronized (this){
System.out.println(new String(buffer, 0, len)
);
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
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 ) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
public static void main( String[] args ) {
try {
TwoWaySerialComm commTwo = new TwoWaySerialComm() ;
commTwo.connect("COM10");
String str = "4C00000100";  // Command to detect card presence by the NFC reader
commTwo.out.write(str.getBytes());
System.out.println("writting : " + str);
} catch( Exception e ) {
e.printStackTrace();
}
}
}

谢谢Pramod

不确定您正在使用的API,但我认为您可能做错了两件事。

1.在中从系统读取

从文档来看,这主要用于键盘输入,因此在SerialWriter类中,这段代码是

while( ( c = System.in.read() ) > -1 ) {                
this.out.write( c );
}

实际上是在等待从键盘上读取数据,但我认为你正在使用USB设备将数据写入程序,因此你可能在错误的地方寻找输入。您实际上需要从读取数据

serialPort.getInputStream();

2.只使用1个线程进行读写

从你的示例代码来看,你正在创建一个简单的ping服务器,因此如果问题1真的是真的,那么你只需要启动1个线程来进行演示,它从读取数据

serialPort.getInputStream();

并将其写回

serialPort.getOutputStream();

也许像这个

public static class SerialPinger extends Thread {
InputStream in;
OutputStream out;
private byte[] buffer = new byte[1024];
public SerialPinger(InputStream in,OutputStream out )
{
this.in = in;
this.out=out;
}
@Override
public void run() {
try {
while( ( len = this.in.read( buffer ) ) > -1 ) {
System.out.println("reading : " +  new String( buffer, 0, len ) );
System.out.println("Writing Back");
this.out.write(buffer,0,len);
}
} catch( IOException e ) {
e.printStackTrace();
}
}

并且在您的主要方法中替换2个线程

( new Thread( new SerialReader( in ) ) ).start();
( new Thread( new SerialWriter( out ) )).start();

有了这个

new SerialPinger(in,out).start();

如果问题1为false,请传入serialPort输入流或System.in。

看起来你是从这个链接得到代码的。

最新更新