這個是一個JAVA代碼的解決方案,但我測試后發現讀不到數據,不知道那里有問題,現在還沒有解決。
package com;
/*
程序文件名稱:ReadComm.java
功能:從串行口COM1中接收數據
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.comm.*;
class R_Frame extends Frame implements Runnable,ActionListener,SerialPortEventListener
{
/* 檢測系統中可用的通訊端口類 */
static CommPortIdentifier portId;
/* Enumeration 為枚舉型類,在java.util中 */
static Enumeration portList;
InputStream inputStream;
/* 聲明RS-232串行端口的成員變量 */
SerialPort serialPort;
Thread readThread;
String str="";
TextField out_message=new TextField("上面文本框顯示接收到的數據");
TextArea in_message=new TextArea();
Button btnOpen=new Button("打開串口");
/*建立窗體*/
R_Frame()
{
super("串口接收數據");
setSize(200,200);
setVisible(true);
btnOpen.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(btnOpen,"North");
/*獲取系統中所有的通訊端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循環結構找出串口 */
while (portList.hasMoreElements()){
/*強制轉換為通訊端口類型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
out_message.setText("已打開端口COM1 ,正在接收數據..... ");
}
catch (PortInUseException e) { }
/*設置串口監聽器*/
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) { }
/* 偵聽到串口有數據,觸發串口事件*/
serialPort.notifyOnDataAvailable(true);
} //if end
} //if end
} //while end
readThread = new Thread(this);
readThread.start(); //線程負責每接收一次數據休眠20秒鐘
} //R_Frame() end
/*點擊按扭所觸發的事件:打開串口,并監聽串口. */
public void actionPerformed(ActionEvent event)
{
/*獲取系統中所有的通訊端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循環結構找出串口 */
while (portList.hasMoreElements()){
/*強制轉換為通訊端口類型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
out_message.setText("已打開端口COM1 ,正在接收數據..... ");
}
catch (PortInUseException e) { }
/*設置串口監聽器*/
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) { }
/* 偵聽到串口有數據,觸發串口事件*/
serialPort.notifyOnDataAvailable(true);
} //if end
} //if end
} //while end
readThread = new Thread(this);
readThread.start(); //線程負責每接收一次數據休眠20秒鐘
} //actionPerformed() end
/*接收數據后休眠20秒鐘*/
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e) { }
} //run() end
/*串口監聽器觸發的事件,設置串口通訊參數,讀取數據并寫到文本區中*/
public void serialEvent(SerialPortEvent event) {
/*設置串口通訊參數:波特率、數據位、停止位、奇偶校驗*/
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {}
byte[] readBuffer = new byte[20];
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e){
}
try {
while (inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
} //while end
str=new String(readBuffer);
/*接收到的數據存放到文本區中*/
in_message.append(str+"\n");
}
catch (IOException e){
}
} //serialEvent() end
} //類R_Frame end
public class ReadComm
{
public static void main(String args[])
{
/* 實例化接收串口數據的窗體類 */
R_Frame R_win=new R_Frame();
/* 定義窗體適配器的關閉按鈕功能 */
R_win.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
R_win.pack();
}
}
這個是在web頁面上的實現,修改了下現在可以讀到數據,但這個需要注冊MSCOMM組件,還算比較理想吧!
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JavaScript MSCOMM32.OCX </title>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
//重寫 mscomm 控件的唯一事件處理代碼
function MSComm1_OnComm()
{
var len=0;
if(MSComm1.CommEvent==1)//如果是發送事件
{
window.alert("請讀條碼");//這句正常,說明發送成功了
}
else if(MSComm1.CommEvent==2)//如果是接收事件
{
document.form1.txtReceive.value=document.form1.txtReceive.value + MSComm1.Input;
}
}
</SCRIPT>
<SCRIPT LANGUAGE=javascript FOR=MSComm1 EVENT=OnComm>
// MSComm1控件每遇到 OnComm 事件就調用 MSComm1_OnComm()函數
MSComm1_OnComm()
</SCRIPT>
<script language="JavaScript" type="text/JavaScript">
//打開端口并發送命令程序
function OpenPort()
{
if(MSComm1.PortOpen==false)
{
MSComm1.PortOpen=true;
MSComm1.Output="R";//發送命令
//window.alert("成功發出命令!");
}
else
{
window.alert ("已經開始接收數據!");
}
}
</script>
</head>
<OBJECT CLASSID="clsid:648A5600-2C6E-101B-82B6-000000000014" id=MSComm1 codebase="MSCOMM32.OCX" type="application/x-oleobject"
style="LEFT: 54px; TOP: 14px" >
<PARAM NAME="CommPort" VALUE="1">
<PARAM NAME="DTREnable" VALUE="1">
<PARAM NAME="Handshaking" VALUE="0">
<PARAM NAME="InBufferSize" VALUE="1024">
<PARAM NAME="InputLen" VALUE="0">
<PARAM NAME="NullDiscard" VALUE="0">
<PARAM NAME="OutBufferSize" VALUE="512">
<PARAM NAME="ParityReplace" VALUE="?">
<PARAM NAME="RThreshold" VALUE="1">
<PARAM NAME="RTSEnable" VALUE="1">
<PARAM NAME="SThreshold" VALUE="2">
<PARAM NAME="EOFEnable" VALUE="0">
<PARAM NAME="InputMode" VALUE="0">
<PARAM NAME="DataBits" VALUE="8">
<PARAM NAME="StopBits" VALUE="1">
<PARAM NAME="BaudRate" VALUE="9600">
<PARAM NAME="Settings" VALUE="9600,N,8,1">
</OBJECT>
<body>
<form name="form1">
<input type="button" name="Submit" value="提交" onClick="OpenPort()">
<input type="text" name="txtReceive" size=50 value="">
</form>
</body>
</html>