package UHF09;
public class UHFReader {
static{
System.loadLibrary("lib/UHF_Reader09");
}
int[] Recv=new int[5000];
int[] SendBuff=new int[5000];
int result=0x30;
int FrmHandle=0;
int fComAddr=0;
public int CardNum=0;
UHF.Reader09 tnt = new UHF.Reader09();
int array_clear(int array_clear0[])
{
int clear_len=array_clear0.length;
for(int i=0;i<clear_len;i++)
{
array_clear0[i]=0;
}
return 0;
}
//打开设备;ComPort:串口号,ComAddr:读写器地址;baudRate:波特率;
public int OpenDevice(int ComPort,byte ComAddr,byte baudRate)
{
array_clear(SendBuff);
SendBuff[0]=ComPort;
SendBuff[1]=ComAddr;
SendBuff[2]=baudRate;
Recv=tnt.OpenComPort(SendBuff);
if(Recv[0]==0)
{
result=0;
fComAddr=Recv[1];
FrmHandle=Recv[2];
}
else
{
result=Recv[0];
fComAddr=255;
FrmHandle=-1;
}
return result;
}
//关闭设备,FrmHandle设备句柄;
public int CloseDevice(int FrmHandle)
{
return tnt.CloseSpecComPort(FrmHandle);
}
public static String byteToString(byte[] b){
StringBuffer sb = new StringBuffer("");
for(int i = 0; i < b.length; i++){
String temp=Integer.toHexString(b[i] & 0xff);
if(temp.length()==1)
{
temp="0"+temp;
}
sb.append(temp);
}
return sb.toString().toUpperCase();
}
public static byte[] stringToByte(String str){
byte[] b = new byte[str.length()/2];
for(int i = 0; i < str.length()/2; i++){
b[i] =(byte)(0xff & Integer.parseInt(str.substring(i*2, i*2+2),16));
}
return b;
}
//询查标签
public String[] Inventory()
{
CardNum=0;
array_clear(SendBuff);
SendBuff[0]=fComAddr;
SendBuff[1]=FrmHandle;
array_clear(Recv);
Recv=tnt.Inventory_G2(SendBuff);
if((Recv[0]!= 0x30)&&(Recv[1]>0))
{
CardNum=Recv[1];
if(CardNum==0) return null;
String[] EPC=new String[CardNum];
int index=0;
for(int m=0;m<CardNum;m++)
{
int len=Recv[3+index];
byte[] epc_arr=new byte[len];
for(int n=0;n<len;n++)
{
epc_arr[n]=(byte)Recv[4+index+n];
}
EPC[m]=byteToString(epc_arr);
index+=len+1;
}
if(CardNum>0)
return EPC;
}
return null;
}
//设置功率,Power功率
public int SetPower(byte Power)
{
array_clear(SendBuff);
SendBuff[0]=fComAddr;
SendBuff[1]=Power;
SendBuff[2]=FrmHandle;
return tnt.SetPowerDbm(SendBuff);
}
//设置读写器地址
public int SetAddress(byte Address)
{
array_clear(SendBuff);
SendBuff[0]=fComAddr;
SendBuff[1]=Address;
SendBuff[2]=FrmHandle;
return tnt.WriteComAdr(SendBuff);
}
//设置读写器工作频率
public int SetRegion(byte MaxFre,byte MinFre)
{
array_clear(SendBuff);
SendBuff[0]=fComAddr;
SendBuff[1]=MaxFre;
SendBuff[2]=MinFre;
SendBuff[3]=FrmHandle;
return tnt.Writedfre(SendBuff);
}
//设置读写器波特率
public int SetBaudRate(byte BaudRate)
{
array_clear(SendBuff);
SendBuff[0]=fComAddr;
SendBuff[1]=BaudRate;
SendBuff[2]=FrmHandle;
return tnt.Writebaud(SendBuff);
}
//读数据,EPC号,WordPtr读取地址,Num读取长度,Mem读取区域,Psd访问密码
public String ReadData(String EPC,byte WordPtr,byte Num,byte Mem,byte[] Psd)
{
String result="";
int len=EPC.length()/2;
byte[]epc_arr=new byte[len];
epc_arr=stringToByte(EPC);
array_clear(SendBuff);
array_clear(Recv);
SendBuff[0]=fComAddr;
SendBuff[1]=len;//EPC字节长度
for(int n=0;n<len;n++)
{
SendBuff[2+n]=epc_arr[n];
}
SendBuff[2+len]=Mem;
SendBuff[3+len]=WordPtr;
SendBuff[4+len]=Num;
SendBuff[5+len]=Psd[0];
SendBuff[6+len]=Psd[1];
SendBuff[7+len]=Psd[2];
SendBuff[8+len]=Psd[3];
SendBuff[9+len]=0;//掩码区域,
SendBuff[10+len]=0;
SendBuff[11+len]=0;
SendBuff[12+len]=FrmHandle;
Recv = tnt.ReadCard_G2(SendBuff);
if(Recv[0]==0)
{
byte[]data=new byte[Num*2];
for(int m=0;m<Num*2;m++ )
{
data[m]=(byte)Recv[2+m];
}
return byteToString(data);
}
else
return "";
}
//写数据,EPC号,WordPtr写入地址,Num写入长度,Data写入数据,Mem写入区域,Psd访问密码
public int WriteData(String EPC,byte WordPtr,byte Num,byte[] Data,byte Mem,byte[] Psd)
{
int result=0;
int len=EPC.length()/2;
byte[]epc_arr=new byte[len];
epc_arr=stringToByte(EPC);
array_clear(SendBuff);
array_clear(Recv);
SendBuff[0]=fComAddr;
SendBuff[1]=len;//EPC字节长度
for(int n=0;n<len;n++)
{
SendBuff[2+n]=epc_arr[n];
}
SendBuff[2+len]=Mem;
SendBuff[3+len]=WordPtr;
SendBuff[4+len]=Num*2;//写入字节数
int wnum= Num*2;
for(int p=0;p<wnum;p++)
{
SendBuff[5+len+p]=Data[p];
}
SendBuff[5+len+wnum]=Psd[0];
SendBuff[6+len+wnum]=Psd[1];
SendBuff[7+len+wnum]=Psd[2];
SendBuff[8+len+wnum]=Psd[3];
SendBuff[9+len+wnum]=0;//掩码区域
SendBuff[10+len+wnum]=0;
SendBuff[11+len+wnum]=0;
SendBuff[12+len+wnum]=FrmHandle;
Recv = tnt.WriteCard_G2(SendBuff);
result=Recv[0];
return result;
}
//写数据,EPC号,WordPtr写入地址,Num写入长度,Data写入数据,Mem写入区域,Psd访问密码
public int WriteEPC(String EPC,byte[] Psd)
{
int result=0;
int len=EPC.length()/2;
byte[]epc_arr=new byte[len];
epc_arr=stringToByte(EPC);
array_clear(SendBuff);
array_clear(Recv);
SendBuff[0]=fComAddr;
SendBuff[1]=len/2;//EPC字长度
for(int n=0;n<len;n++)
{
SendBuff[2+n]=epc_arr[n];
}
SendBuff[2+len]=Psd[0];
SendBuff[3+len]=Psd[1];
SendBuff[4+len]=Psd[2];
SendBuff[5+len]=Psd[3];
SendBuff[6+len]=FrmHandle;
Recv = tnt.WriteEPC_G2(SendBuff);
result=Recv[0];
return result;
}
}
报错
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at UHF09.UHFReader.OpenDevice(UHFReader.java:33)
at UHF09.Reader.bt_open_actionPerformed(Reader.java:330)
at UHF09.Reader$Bt_openActionListener.actionPerformed(Reader.java:291)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
求大神指教啊!!!!!!!!!!!!!!