1.动态加载dll文件
(1)在util中放入UhfReader.java和UhfReaderAPI.java文件
(2)放入ina jar包 lib下和Bulid Path中各加一份
(3)在src中放入UhfReader_API.dll文件,调用saomacontroller包中的SaomaController.java文件,内容如下:
package com.fh.controller.saomacontroller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fh.controller.base.BaseController;
import com.fh.service.Depot.depot.DepotService;
import com.fh.service.Depot.depot.OutDepotService;
import com.fh.service.Depot.depot.StocktakingService;
import com.fh.service.product.priductId.ProductIdService;
import com.fh.util.Const;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;
import com.fh.util.UhfReader;
import com.sun.jna.ptr.ByteByReference;
@Controller
@RequestMapping(value="/saomainfo")
public class SaomaController extends BaseController{
String menuUrl = "saomainfo/list.do"; //菜单地址(权限用)
@Resource(name = "depotService")
private DepotService depotService;
@Resource(name="OutDepotService")
private OutDepotService outDepotService;
@Resource(name = "productIdService")
private ProductIdService productIdService;
@Resource(name="StocktakingService")
private StocktakingService stocktakingService;
public Listlist=new ArrayList();
public boolean readFinished = true;
public int size=0;
public UhfReader mReader=new UhfReader();
private boolean readingEPCData=false;
enum COL_DATA{
COL_DATA_PC,COL_DATA_EPC,COL_DATA_RSSI,COL_DATA_SESSION,COL_DATA_DATA,COL_DATA_ERROR
};
@RequestMapping(value="/list")
public @ResponseBody PageData list(HttpServletRequest request)throws Exception{
System.out.println("开始扫码啦!");
logBefore(logger, "扫码Info");
PageData pd = new PageData();
ModelAndView mv = this.getModelAndView();
String path = request.getServletContext().getRealPath("/");
System.out.println("path:"+path);
//扫码
tmp.IniReader reader = new tmp.IniReader(path+"uploadFiles\\file\\1.ini");//D:\\1.ini
String port=reader.getValue("Port", "port1");
System.out.println("port "+port);
String count=reader.getValue("Count", "count1");
System.out.println("count "+count);
mReader.Connect("COM3",6);//注意波特率为115200,否则会出现无法打开端口
System.out.println("扫码进行中");
if(!mReader.isConnect())
{
System.out.println("请先连接串口");
return pd;
}
else{
mReader.ReadInventory();//启动快速读
new Thread(){
public void run()
{
for (int i = 0; i < 20 ; i++)
{
System.out.println("list list"+list);
System.out.println("读取次数 !----"+i);
StringBuffer PC=new StringBuffer();
StringBuffer EPC=new StringBuffer();
StringBuffer RSSI=new StringBuffer();
if(mReader.RecvData(PC, EPC, RSSI))
{
String EPCStr = EPC.toString();
boolean ExistEPCStr = false;
for (int j =0;j
{
if (list.get(0) == EPCStr)
{
ExistEPCStr = true;
break;
}
}
if (ExistEPCStr == true) continue;
if(!list.contains(EPCStr))
{
list.add(EPCStr);
}
}
}
mReader.StopOperation();//注意用完后一定要记得停止操作
mReader.Disconnect();//断开连接
readFinished = false;
System.out.println("1---"+list);
}
}.start();
while (readFinished) {} //waiting for reading complete
System.out.println("2---"+list);
pd.put("list", list);
size = list.size();
pd.put("size", size);
}
list=new ArrayList();
System.out.println("pdpd"+pd);
return pd;
}
2.动态加载ini文件
(1)在D盘放入1.ini文件,ini文件内容如下:
[Port]
port1=COM3
[Baudrate]
baudrate1=9600
baudrate2=19200
baudrate3=57600
baudrate4=115200
[Connect]
connect=true
disconnect=false
[Count]
count1=1000
[Power]
power1=10
power2=11
power3=12
power4=13
power5=14
power6=15
power7=16
[Frequecy]
frequecy1=647
frequecy2=45
frequecy3=86
frequecy4=23
[LabelWorkMode]
mode1=10
mode2=80
mode3=86
mode4=25
[LblQ]
Q1=339
Q2=82
Q3=42
Q4=21
(2)在src目录下放入tmp包,包中含有IniReader.java,
这个文件是加载ini文件用的,是固定的,内容如下:
package tmp;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
public class IniReader {
protected HashMap sections = new HashMap();
private transient String currentSecion;
private transient Properties current;
public IniReader(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
read(reader);
reader.close();
}
protected void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}
protected void parseLine(String line) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
currentSecion = line.replaceFirst("\\[(.*)\\]", "$1");
current = new Properties();
sections.put(currentSecion, current);
} else if (line.matches(".*=.*")) {
if (current != null) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
current.setProperty(name, value);
}
}
}
public String getValue(String section, String name) {
Properties p = (Properties) sections.get(section);
if (p == null) {
return null;
}
String value = p.getProperty(name);
return value;
}
}
(3)再写个测试类,IniTest.java,内容如下:
import tmp.IniReader;
import java.io.IOException;
public class IniTest {
public static void main(String[] args) throws IOException {
tmp.IniReader reader = new tmp.IniReader("D:\\1.ini");
System.out.println(reader.getValue("LblQ", "Q1"));
}
}
这时在控制台打印结果为339,说明调用1.ini文件成功