我写了个用java 读电子称数据的程序,有点问题是main()中能够得到正确的重量,可在程序中按main中的代码一样调用却找不到端口main()中调用的代码:
public class CommTest {
public static void main(String[] args) {
HashMap <String, Comparable> params = new HashMap <String, Comparable>();
params.put(SerialReader.PARAMS_PORT, "COM1"); // 端口名称
params.put(SerialReader.PARAMS_RATE, 9600); // 波特率
params.put(SerialReader.PARAMS_TIMEOUT, 1000); // 设备超时时间 1秒
params.put(SerialReader.PARAMS_DELAY, 200); // 端口数据准备时间 1秒
params.put(SerialReader.PARAMS_DATABITS, SerialPort.DATABITS_8); // 数据位
params.put(SerialReader.PARAMS_STOPBITS, SerialPort.STOPBITS_1); // 停止位
params.put(SerialReader.PARAMS_PARITY, SerialPort.PARITY_NONE); // 无奇偶校验
SerialReader sr = new SerialReader(params);
String weight = sr.ReadPort(100);
System.out.println(weight);//此出能输出正确结果
sr.ClosePort();
}
} action中的代码:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 设置接收信息的字符集�
request.setCharacterEncoding("UTF-8");
// 接收浏览器端提交的信息
// String action = request.getParameter("action");
String productName = request.getParameter("productName");
String productColor = request.getParameter("productColor");
// 中文乱码处理,转为"UTF-8"编码
productColor = new String(productColor.getBytes("ISO-8859-1"), "UTF-8");
String productNumber = request.getParameter("productNumber");
// ���������设置输出信息的格式及字符集�
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
// 创建输出流对象�
PrintWriter out = response.getWriter();
// �依据验证结果输出不同的数据信息�
out.println("<response>");
try {
HashMap <String, Comparable> params = new HashMap <String, Comparable>();
params.put(SerialReader.PARAMS_PORT, "COM1"); // 端口名称
params.put(SerialReader.PARAMS_RATE, 9600); // 波特率
params.put(SerialReader.PARAMS_TIMEOUT, 1000); // 设备超时时间 1秒
params.put(SerialReader.PARAMS_DELAY, 200); // 端口数据准备时间 1秒
params.put(SerialReader.PARAMS_DATABITS, SerialPort.DATABITS_8); // 数据位
params.put(SerialReader.PARAMS_STOPBITS, SerialPort.STOPBITS_1); // 停止位
params.put(SerialReader.PARAMS_PARITY, SerialPort.PARITY_NONE); // 无奇偶校验
SerialReader sr = new SerialReader(params);
String weight = sr.ReadPort(100);
sr.ClosePort();
if (weight != null) {
out.println("<msg>" + weight + "</msg>");
} else {
out.println("<msg>" + "称重失败" + "</msg>");
}
}catch (Exception e) {
e.printStackTrace();
out.println("<msg>" + "称重出错!" + "</msg>");
}
out.println("</response>");
out.close();
}在action中调用时,在portId = CommPortIdentifier.getPortIdentifier(port);时会显示找不到端口
请各位大哥多多指点~~
public class CommTest {
public static void main(String[] args) {
HashMap <String, Comparable> params = new HashMap <String, Comparable>();
params.put(SerialReader.PARAMS_PORT, "COM1"); // 端口名称
params.put(SerialReader.PARAMS_RATE, 9600); // 波特率
params.put(SerialReader.PARAMS_TIMEOUT, 1000); // 设备超时时间 1秒
params.put(SerialReader.PARAMS_DELAY, 200); // 端口数据准备时间 1秒
params.put(SerialReader.PARAMS_DATABITS, SerialPort.DATABITS_8); // 数据位
params.put(SerialReader.PARAMS_STOPBITS, SerialPort.STOPBITS_1); // 停止位
params.put(SerialReader.PARAMS_PARITY, SerialPort.PARITY_NONE); // 无奇偶校验
SerialReader sr = new SerialReader(params);
String weight = sr.ReadPort(100);
System.out.println(weight);//此出能输出正确结果
sr.ClosePort();
}
} action中的代码:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 设置接收信息的字符集�
request.setCharacterEncoding("UTF-8");
// 接收浏览器端提交的信息
// String action = request.getParameter("action");
String productName = request.getParameter("productName");
String productColor = request.getParameter("productColor");
// 中文乱码处理,转为"UTF-8"编码
productColor = new String(productColor.getBytes("ISO-8859-1"), "UTF-8");
String productNumber = request.getParameter("productNumber");
// ���������设置输出信息的格式及字符集�
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
// 创建输出流对象�
PrintWriter out = response.getWriter();
// �依据验证结果输出不同的数据信息�
out.println("<response>");
try {
HashMap <String, Comparable> params = new HashMap <String, Comparable>();
params.put(SerialReader.PARAMS_PORT, "COM1"); // 端口名称
params.put(SerialReader.PARAMS_RATE, 9600); // 波特率
params.put(SerialReader.PARAMS_TIMEOUT, 1000); // 设备超时时间 1秒
params.put(SerialReader.PARAMS_DELAY, 200); // 端口数据准备时间 1秒
params.put(SerialReader.PARAMS_DATABITS, SerialPort.DATABITS_8); // 数据位
params.put(SerialReader.PARAMS_STOPBITS, SerialPort.STOPBITS_1); // 停止位
params.put(SerialReader.PARAMS_PARITY, SerialPort.PARITY_NONE); // 无奇偶校验
SerialReader sr = new SerialReader(params);
String weight = sr.ReadPort(100);
sr.ClosePort();
if (weight != null) {
out.println("<msg>" + weight + "</msg>");
} else {
out.println("<msg>" + "称重失败" + "</msg>");
}
}catch (Exception e) {
e.printStackTrace();
out.println("<msg>" + "称重出错!" + "</msg>");
}
out.println("</response>");
out.close();
}在action中调用时,在portId = CommPortIdentifier.getPortIdentifier(port);时会显示找不到端口
请各位大哥多多指点~~
SerialReader sr = new SerialReader(params);
params传到SerialReader类里时能读出SerialReader.PARAMS_PORT="COM1"吗
能读出 delayRead = Integer.parseInt(serialParams.get(PARAMS_DELAY)
.toString());
String port = serialParams.get(PARAMS_PORT).toString();
// 打开端口
portId = CommPortIdentifier.getPortIdentifier(port);
serialPort = (SerialPort) portId.open("SerialReader", timeout);
inputStream = serialPort.getInputStream();
// serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(rate, dataBits, stopBits, parity);
SB = new SerialBuffer();
RT = new ReadSerial(SB, inputStream);
RT.start();
在打开端口时找不到
但用main()中调用却找的到
serialParams = params;
init();
} private void init() {
try {
// 参数初始化
int timeout = Integer.parseInt(serialParams.get(PARAMS_TIMEOUT)
.toString());
int rate = Integer.parseInt(serialParams.get(PARAMS_RATE)
.toString());
int dataBits = Integer.parseInt(serialParams.get(PARAMS_DATABITS)
.toString());
int stopBits = Integer.parseInt(serialParams.get(PARAMS_STOPBITS)
.toString());
int parity = Integer.parseInt(serialParams.get(PARAMS_PARITY)
.toString());
delayRead = Integer.parseInt(serialParams.get(PARAMS_DELAY)
.toString());
String port = serialParams.get(PARAMS_PORT).toString();
// 打开端口
portId = CommPortIdentifier.getPortIdentifier(port);
serialPort = (SerialPort) portId.open("SerialReader", timeout);
inputStream = serialPort.getInputStream();
// serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(rate, dataBits, stopBits, parity);
SB = new SerialBuffer();
RT = new ReadSerial(SB, inputStream);
RT.start();
} catch (PortInUseException e) {
System.out.println("端口已经被占用!");
e.printStackTrace();
/* } catch (TooManyListenersException e) {
System.out.println("端口监听者过多!");
e.printStackTrace();*/
} catch (UnsupportedCommOperationException e) {
System.out.println("端口操作命令不支持!");
e.printStackTrace();
} catch (NoSuchPortException e) {
System.out.println("端口不存在!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Thread readThread = new Thread(this);
// readThread.start();
}
public String ReadPort(int Length) {
String Msg;
Msg = SB.GetMsg(Length);
System.out.println(Msg);
return Msg;
}
public void ClosePort() {
RT.stop();
serialPort.close();
}
/**
************2***************8
public class SerialBuffer {
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1; /**
*
* This function returns a string with a certain length from the incoming
* messages.
*
* @param Length
* The length of the string to be returned.
*
*/ public synchronized String GetMsg(int Length) {
LengthNeeded = Length;
notifyAll(); if (LengthNeeded > Content.length()) {
available = false;
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} //取得"+"的index
int indexOfAdd =new String(Content).indexOf("+");
//从"+"的后一位开始,前6位就为一个完整的重量数据
String weight = new String(Content).substring(indexOfAdd+1, indexOfAdd+7);
String weight1 = weight.substring(0,3); //获取数据前三位
String weight2 = weight.substring(3); //获取数据后三位
if(weight1.charAt(0)=='0' && weight1.charAt(1)=='0') {weight1= weight1.substring(2);} //前两位都为0则去掉前两位
else if(weight1.charAt(0)=='0') {weight1=weight1.substring(1); }//第一位为0则去掉第一位
weight = weight1+"."+weight2+"Kg"; //得到重量+单位
CurrentMsg =weight;
//CurrentMsg = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
} /**
*
* This function stores a character captured from the serial port to the
* buffer area.
*
* @param t
* The char value of the character to be stored.
*
*/ public synchronized void PutChar(int c) {
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (LengthNeeded < Content.length()) {
available = true;
}
notifyAll();
}
}
System.out.println("端口已经被占用!");
e.printStackTrace();
/* } catch (TooManyListenersException e) {
System.out.println("端口监听者过多!");
e.printStackTrace();*/
} catch (UnsupportedCommOperationException e) {
System.out.println("端口操作命令不支持!");
e.printStackTrace();
} catch (NoSuchPortException e) {
System.out.println("端口不存在!"); 错误时这里面的哪一个啊
String port = serialParams.get(PARAMS_PORT).toString();
// 打开端口
portId = CommPortIdentifier.getPortIdentifier(“COM1”);你试过在这里放“COM1”吗
你具体的应用是想在servlet里把串口起来吗
有试过
是的是想在servlet中得到重量,然后串到页面上去
现在我的疑惑是为什么在main()中能读到端口,而同样的代码,在servlet中就找不到端口
这个问题花了我一天多时间了,呵~~~郁闷啊
HashMap <String, Comparable> params = new HashMap <String, Comparable>();
params.put(SerialReader.PARAMS_PORT, "COM1"); // 端口名称
params.put(SerialReader.PARAMS_RATE, 9600); // 波特率
params.put(SerialReader.PARAMS_TIMEOUT, 1000); // 设备超时时间 1秒
params.put(SerialReader.PARAMS_DELAY, 200); // 端口数据准备时间 1秒
params.put(SerialReader.PARAMS_DATABITS, SerialPort.DATABITS_8); // 数据位
params.put(SerialReader.PARAMS_STOPBITS, SerialPort.STOPBITS_1); // 停止位
params.put(SerialReader.PARAMS_PARITY, SerialPort.PARITY_NONE); // 无奇偶校验
SerialReader sr = new SerialReader(params);
String weight = sr.ReadPort(100);
sr.ClosePort();
我现在改用rxtx包,还是同样的问题,就是在main()中运行正常,能得到重量
在servlet中就报错:java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
在main()能运行,就说明rxtxSerial.dll能找到啊,为什么在servlet中就找不到rxtxSerial.dll呢。
跟踪一 下,程序还是在portId = CommPortIdentifier.getPortIdentifier(port); 处停住了
也不知道后面会遇到什么问题,呵```
1.COMM.JAR;
2.javax.comm.properties;
3.win32com.dll;
然后把这三个文件放到如下目录中:
其中:JDKDIR是JDK的安装目录,JREDIR是JRE的安装目录。
假如你把JAVA开发包装在C:\Program Files\Java中,里面有两个文件夹,一个是JDK,一个是JRE。
COMM.JAR放到JDKDIR/jre/lib/ext;
javax.comm.properties放到JDKDIR/jre/lib。
win32com.dll放到JDKDIR/bin中,
如果用ECLIPSE可能需要你手动导入comm.JAR,相信这个大家都会。
配置到这里就完成了吗?
没有,有的机器会报错,如果说没有win32com.dll,那么请把win32com.dll放到JreDIR/bin中就可以了。
如果发现获得的端口列表中为空,那是因为机器没有找到javax.comm.properties文件,同样把javax.comm.properties放到JreDIR/jre/lib中就可以了,具体是放到JDK还是JRE中,我也不太明白。估计和机器和JDK版本有关系了,不过,肯定有效的方法是你把JDK还是JRE,全都放了。
结贴了,谢谢大家的指导~~