最近帮一个老师收尾一个射频卡项目,主要是应用层的,就是使用串口和单片机通信。但是这个要求很变态,需要使用Web来读写串口,但是我考虑自己写一个 ActiveX控件,但是用C++写一个纯COM+麻烦,后来看到有任用.NET来写ActiveX,但是要求客户端要装.NET FX,抓狂之下,找到了 MSCOMM32.OCX
简单的HTML+JS代码:
Code
1<html>
2<head>
3 <title>JavaScript串口测试</title>
4 <meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
5
6 <SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
7 <!--
8 function MSComm1_OnComm()
9 {
10 switch(MSComm1.CommEvent)
11 {
12 case 1:{ window.alert("Send OK!"); break;} //发送事件
13 case 2: { Receive();break;} //接收事件
14 default: alert("Event Raised!"+MSComm1.CommEvent);;
15 }
16 }
17 //-->
18
19 </SCRIPT>
20
21 <SCRIPT LANGUAGE=javascript FOR=MSComm1 EVENT=OnComm>
22 <!--
23 // MSComm1控件每遇到 OnComm 事件就调用 MSComm1_OnComm()函数
24 MSComm1_OnComm()
25 //-->
26 </SCRIPT>
27
28 <script language="JavaScript" type="text/JavaScript">
29 <!--
30
31String.prototype.Blength = function(){
32 var arr = this.match(/[^\x00-\xff]/ig);
33 return arr == null ? this.length : this.length + arr.length;
34}
35
36 function OperatePort()
37 {
38 if(MSComm1.PortOpen==true)
39 {
40 try{MSComm1.PortOpen=false;
41 document.getElementById("OperateButton").value="打开串口";
42 }catch(ex)
43 {alert(ex.message);}
44 }
45 else{
46 try{ MSComm1.PortOpen=true;
47 document.getElementById("OperateButton").value="关闭串口";
48 }catch(ex)
49 {alert(ex.message);}
50 }
51 }
52
53 function ConfigPort()
54 {
55 if(MSComm1.PortOpen==false)
56 {
57 try{
58 MSComm1.CommPort=document.getElementById("ComName").value;
59 MSComm1.Settings=document.getElementById("BaudRate").value.toString()+
60 ","+document.getElementById("CheckBit").value.toString()+
61 ","+document.getElementById("DataBits").value.toString()+
62 ","+document.getElementById("StopBits").value.toString();
63 MSComm1.OutBufferCount =0; //清空发送缓冲区
64 MSComm1.InBufferCount = 0; //滑空接收缓冲区
65 alert("已配置串口COM"+MSComm1.CommPort+"\n 参数:"+MSComm1.Settings);
66 }catch(ex){alert(ex.message);}
67 }
68 else{ alert("请先关闭串口后再设置!");}
69 }
70
71 function Send()
72 {
73 //alert(document.getElementById("txtSend").value);
74 var orgstr=document.getElementById("txtSend").value;
75 var newstr="";
76 var hexflag=document.getElementById("isSendHex").checked;
77 if(hexflag&&orgstr!="")
78 {
79 if(orgstr.substr(0,2)=="0x"||orgstr.substr(0,2)=="0X") orgstr=orgstr.substring(2,orgstr.length);
80 if(orgstr.length%2!=0) orgstr="0"+orgstr;
81 if((newstr=str2hex(orgstr,16))=="") {alert("错误的16进制数");return false;}
82 }
83 try{
84 MSComm1.Output=hexflag?newstr:orgstr;
85 }catch(ex)
86 {alert(ex.message);}
87 }
88
89 function Receive()
90 {
91 //alert("InBufferCount::"+MSComm1.InBufferCount);
92 document.getElementById("txtReceive").value += MSComm1.Input;
93 //alert("InBufferCount::"+MSComm1.InBufferCount);
94 }
95
96 function ClearReceived()
97 {
98 document.getElementById("txtReceive").innerText="";
99 }
100
101 -->
102
103</script>
104
105</head>
106<body>
107
108<form name="form1">
109
110
111<fieldset style="width:200px;height:250px;text-align:center;">
112<legend>配置串口</legend>
113 <div style="float:left;width:200px">
114 <br/>
115 <span>串口号:</span>
116 <select name="ComName" id="ComName" style="width:75px" >
117 <option value="1" >COM1</option>
118 <option value="2" >COM2</option>
119 <option value="3" selected >COM3</option>
120 <option value="4" >COM4</option>
121 </select>
122 <br/>
123 <span>波特率:</span>
124 <select name="BaudRate" id="BaudRate" style="width:75px" >
125 <option value="9600" selected >9600</option>
126 <option value="57600" >57600</option>
127 <option value="115200" >115200</option>
128 </select>
129 <br/>
130
131 <span>校验位:</span>
132 <select name="CheckBit" id="CheckBit" style="width:75px" >
133 <option value="N" selected >无NONE</option>
134 <option value="O" >奇ODD</option>
135 <option value="E" >偶EVEN</option>
136 </select>
137 <br/>
138
139 <span>数据位:</span>
140 <input type=text id="DataBits" name="DataBits" value=8 style="width:75px;height:20px">
141 <br/>
142
143 <span>停止位:</span>
144 <input type=text id="StopBits" name="StopBits" value=1 style="width:75px;height:20px">
145 <br/>
146 <br/>
147 <input type="button" id="ConfigButton" style="width:80px;height:30px;font-size:13px" name="ConfigButton" value="配置串口" onClick="ConfigPort()">
148 <input type="button" id="OperateButton" style="width:80px;height:30px;font-size:13px" name="OperateButton" value="打开串口" onClick="OperatePort()">
149 </div>
150</fieldset>
151
152
153
154<fieldset style="width:200px;height:250px;text-align:center;">
155<legend>发送区域</legend>
156<div style="float:left;">
157 <textarea id="txtSend" name="txtSend" style="width:200px;height:160px"></textarea>
158 <br/>
159 <span><input id="isSendHex" name="isSendHex" type="checkbox" checked="0" />16进制</span>
160 <input type="button" id="SendButton" style="width:100px;height:30px" name="SendButton" value="发送" onClick="Send()">
161</div>
162</fieldset>
163
164<fieldset style="width:200px;height:250px;text-align:center;">
165<legend>接收区域</legend>
166<div style="float:left;">
167 <textarea id="txtReceive" READONLY=TRUE name="txtReceive" style="width:200px;height:160px"></textarea>
168 <br/>
169 <span><input id="isReceiveHex" name="isReceiveHex" type="checkbox" />16进制</span>
170 <input type="button" id="ClearButton" style="width:100px;height:30px" name="ClearButton" value="清空" onClick="ClearReceived()">
171</div>
172</fieldset>
173
174
175
176
177</form>
178
179 <OBJECT id=MSComm1 CLASSID="clsid:648A5600-2C6E-101B-82B6-000000000014"
180 codebase="MSCOMM32.OCX" type="application/x-oleobject" style="LEFT:54px;TOP:14px" >
181 <PARAM NAME="CommPort" VALUE="3"/>
182 <PARAM NAME="DataBits" VALUE="8"/>
183 <PARAM NAME="StopBits" VALUE="1"/>
184 <PARAM NAME="BaudRate" VALUE="9600"/>
185 <PARAM NAME="Settings" VALUE="9600,N,8,1"/>
186
187 <PARAM NAME="RTSEnable" VALUE="1"/>
188 <PARAM NAME="DTREnable" VALUE="1"/>
189 <PARAM NAME="Handshaking" VALUE="0"/>
190 <PARAM NAME="NullDiscard" VALUE="0"/>
191 <PARAM NAME="ParityReplace" VALUE="?"/>
192
193 <PARAM NAME="EOFEnable" VALUE="0"/>
194 <PARAM NAME="InputMode" VALUE="0"/>
195 <PARAM NAME="InBufferSize" VALUE="1024"/>
196 <PARAM NAME="InputLen" VALUE="0"/>
197 <PARAM NAME="OutBufferSize" VALUE="512"/>
198
199 <PARAM NAME="SThreshold" VALUE="0"/>
200 <PARAM NAME="RThreshold" VALUE="1"/>
201
202</OBJECT>
203
204
205</body>
206</html>
1<html>
2<head>
3 <title>JavaScript串口测试</title>
4 <meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
5
6 <SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
7 <!--
8 function MSComm1_OnComm()
9 {
10 switch(MSComm1.CommEvent)
11 {
12 case 1:{ window.alert("Send OK!"); break;} //发送事件
13 case 2: { Receive();break;} //接收事件
14 default: alert("Event Raised!"+MSComm1.CommEvent);;
15 }
16 }
17 //-->
18
19 </SCRIPT>
20
21 <SCRIPT LANGUAGE=javascript FOR=MSComm1 EVENT=OnComm>
22 <!--
23 // MSComm1控件每遇到 OnComm 事件就调用 MSComm1_OnComm()函数
24 MSComm1_OnComm()
25 //-->
26 </SCRIPT>
27
28 <script language="JavaScript" type="text/JavaScript">
29 <!--
30
31String.prototype.Blength = function(){
32 var arr = this.match(/[^\x00-\xff]/ig);
33 return arr == null ? this.length : this.length + arr.length;
34}
35
36 function OperatePort()
37 {
38 if(MSComm1.PortOpen==true)
39 {
40 try{MSComm1.PortOpen=false;
41 document.getElementById("OperateButton").value="打开串口";
42 }catch(ex)
43 {alert(ex.message);}
44 }
45 else{
46 try{ MSComm1.PortOpen=true;
47 document.getElementById("OperateButton").value="关闭串口";
48 }catch(ex)
49 {alert(ex.message);}
50 }
51 }
52
53 function ConfigPort()
54 {
55 if(MSComm1.PortOpen==false)
56 {
57 try{
58 MSComm1.CommPort=document.getElementById("ComName").value;
59 MSComm1.Settings=document.getElementById("BaudRate").value.toString()+
60 ","+document.getElementById("CheckBit").value.toString()+
61 ","+document.getElementById("DataBits").value.toString()+
62 ","+document.getElementById("StopBits").value.toString();
63 MSComm1.OutBufferCount =0; //清空发送缓冲区
64 MSComm1.InBufferCount = 0; //滑空接收缓冲区
65 alert("已配置串口COM"+MSComm1.CommPort+"\n 参数:"+MSComm1.Settings);
66 }catch(ex){alert(ex.message);}
67 }
68 else{ alert("请先关闭串口后再设置!");}
69 }
70
71 function Send()
72 {
73 //alert(document.getElementById("txtSend").value);
74 var orgstr=document.getElementById("txtSend").value;
75 var newstr="";
76 var hexflag=document.getElementById("isSendHex").checked;
77 if(hexflag&&orgstr!="")
78 {
79 if(orgstr.substr(0,2)=="0x"||orgstr.substr(0,2)=="0X") orgstr=orgstr.substring(2,orgstr.length);
80 if(orgstr.length%2!=0) orgstr="0"+orgstr;
81 if((newstr=str2hex(orgstr,16))=="") {alert("错误的16进制数");return false;}
82 }
83 try{
84 MSComm1.Output=hexflag?newstr:orgstr;
85 }catch(ex)
86 {alert(ex.message);}
87 }
88
89 function Receive()
90 {
91 //alert("InBufferCount::"+MSComm1.InBufferCount);
92 document.getElementById("txtReceive").value += MSComm1.Input;
93 //alert("InBufferCount::"+MSComm1.InBufferCount);
94 }
95
96 function ClearReceived()
97 {
98 document.getElementById("txtReceive").innerText="";
99 }
100
101 -->
102
103</script>
104
105</head>
106<body>
107
108<form name="form1">
109
110
111<fieldset style="width:200px;height:250px;text-align:center;">
112<legend>配置串口</legend>
113 <div style="float:left;width:200px">
114 <br/>
115 <span>串口号:</span>
116 <select name="ComName" id="ComName" style="width:75px" >
117 <option value="1" >COM1</option>
118 <option value="2" >COM2</option>
119 <option value="3" selected >COM3</option>
120 <option value="4" >COM4</option>
121 </select>
122 <br/>
123 <span>波特率:</span>
124 <select name="BaudRate" id="BaudRate" style="width:75px" >
125 <option value="9600" selected >9600</option>
126 <option value="57600" >57600</option>
127 <option value="115200" >115200</option>
128 </select>
129 <br/>
130
131 <span>校验位:</span>
132 <select name="CheckBit" id="CheckBit" style="width:75px" >
133 <option value="N" selected >无NONE</option>
134 <option value="O" >奇ODD</option>
135 <option value="E" >偶EVEN</option>
136 </select>
137 <br/>
138
139 <span>数据位:</span>
140 <input type=text id="DataBits" name="DataBits" value=8 style="width:75px;height:20px">
141 <br/>
142
143 <span>停止位:</span>
144 <input type=text id="StopBits" name="StopBits" value=1 style="width:75px;height:20px">
145 <br/>
146 <br/>
147 <input type="button" id="ConfigButton" style="width:80px;height:30px;font-size:13px" name="ConfigButton" value="配置串口" onClick="ConfigPort()">
148 <input type="button" id="OperateButton" style="width:80px;height:30px;font-size:13px" name="OperateButton" value="打开串口" onClick="OperatePort()">
149 </div>
150</fieldset>
151
152
153
154<fieldset style="width:200px;height:250px;text-align:center;">
155<legend>发送区域</legend>
156<div style="float:left;">
157 <textarea id="txtSend" name="txtSend" style="width:200px;height:160px"></textarea>
158 <br/>
159 <span><input id="isSendHex" name="isSendHex" type="checkbox" checked="0" />16进制</span>
160 <input type="button" id="SendButton" style="width:100px;height:30px" name="SendButton" value="发送" onClick="Send()">
161</div>
162</fieldset>
163
164<fieldset style="width:200px;height:250px;text-align:center;">
165<legend>接收区域</legend>
166<div style="float:left;">
167 <textarea id="txtReceive" READONLY=TRUE name="txtReceive" style="width:200px;height:160px"></textarea>
168 <br/>
169 <span><input id="isReceiveHex" name="isReceiveHex" type="checkbox" />16进制</span>
170 <input type="button" id="ClearButton" style="width:100px;height:30px" name="ClearButton" value="清空" onClick="ClearReceived()">
171</div>
172</fieldset>
173
174
175
176
177</form>
178
179 <OBJECT id=MSComm1 CLASSID="clsid:648A5600-2C6E-101B-82B6-000000000014"
180 codebase="MSCOMM32.OCX" type="application/x-oleobject" style="LEFT:54px;TOP:14px" >
181 <PARAM NAME="CommPort" VALUE="3"/>
182 <PARAM NAME="DataBits" VALUE="8"/>
183 <PARAM NAME="StopBits" VALUE="1"/>
184 <PARAM NAME="BaudRate" VALUE="9600"/>
185 <PARAM NAME="Settings" VALUE="9600,N,8,1"/>
186
187 <PARAM NAME="RTSEnable" VALUE="1"/>
188 <PARAM NAME="DTREnable" VALUE="1"/>
189 <PARAM NAME="Handshaking" VALUE="0"/>
190 <PARAM NAME="NullDiscard" VALUE="0"/>
191 <PARAM NAME="ParityReplace" VALUE="?"/>
192
193 <PARAM NAME="EOFEnable" VALUE="0"/>
194 <PARAM NAME="InputMode" VALUE="0"/>
195 <PARAM NAME="InBufferSize" VALUE="1024"/>
196 <PARAM NAME="InputLen" VALUE="0"/>
197 <PARAM NAME="OutBufferSize" VALUE="512"/>
198
199 <PARAM NAME="SThreshold" VALUE="0"/>
200 <PARAM NAME="RThreshold" VALUE="1"/>
201
202</OBJECT>
203
204
205</body>
206</html>
但是有一个问题,使用脚本的方式调用MSCOMM32 ,发送单字节的 数据不能超过 7FH,这个问题如果在强类型的 VB中,可以以字节数组的方式发送,凡是在script中,都是弱类型的,尝试了各种办法,甚至还Javascript和VBsvript混合使用,使用 VBS的各种 字符串操作函数,最终没有解决。
如果大家找到方法,请告诉我啊!
测试代码:
Code
1 <script language="JavaScript">
2
3 var share1=String.fromCharCode(0xFF);
4
5 </script>
6
7
8
9 <script language="VBScript">
10
11 sub vbsend
12
13 Dim s(0)
14
15 s(0)=&HEE
16
17 MSComm1.Output=CHRW(&HFF)
18
19 end sub
20
21 </script>
22
23 <input type="button" onclick="vbscript:vbsend" value="VBTest" >
1 <script language="JavaScript">
2
3 var share1=String.fromCharCode(0xFF);
4
5 </script>
6
7
8
9 <script language="VBScript">
10
11 sub vbsend
12
13 Dim s(0)
14
15 s(0)=&HEE
16
17 MSComm1.Output=CHRW(&HFF)
18
19 end sub
20
21 </script>
22
23 <input type="button" onclick="vbscript:vbsend" value="VBTest" >