qq_42770175
_Ronin_
2018-11-13 07:15

求大神指教,程序不报错,运行的时候不能识别串口

  • html串口通信

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://

www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   



JavaScript串口测试

<br> &lt;!--<br> function MSComm1_OnComm()<br><br> {<br><br> switch(MSComm1.CommEvent)<br> {<br> case 1:{ window.alert(&quot;Send OK!&quot;); break;} //发送事件<br> case 2: { Receive();break;} //接收事件<br> default: alert(&quot;Event Raised!&quot;+MSComm1.CommEvent);;<br> }<br><br> }<br><br> //--&gt; </p> <p>

<br> &lt;!--<br> // MSComm1控件每遇到 OnComm 事件就调用 MSComm1_OnComm()函数<br> MSComm1_OnComm();<br> //--&gt; <br>

<br> &lt;!-- </p> <p>String.prototype.Blength = function(){<br><br> var arr = this.match(/[^\x00-\xff]/ig);<br><br> return arr == null ? this.length : this.length + arr.length;<br><br> } </p> <p>function OperatePort()<br> {<br> if(MSComm1.PortOpen==true)<br> {<br> try{MSComm1.PortOpen=false;<br> document.getElementById(&quot;OperateButton&quot;).value=&quot;打开串口&quot;;<br> }catch(ex)<br> {alert(ex.message);}<br><br> }<br> else{<br> try{ MSComm1.PortOpen=true;<br> document.getElementById(&quot;OperateButton&quot;).value=&quot;关闭串口&quot;;<br> }catch(ex)<br> {alert(ex.message);}<br><br> }<br> }</p> <p>function ConfigPort()<br> {<br> alert(MSComm1.PortOpen);<br> if(MSComm1.PortOpen==false)<br> {<br><br> try{<br> MSComm1.CommPort=document.getElementById(&quot;ComName&quot;).value;<br> MSComm1.Settings=document.getElementById(&quot;BaudRate&quot;).value.toString()+<br> &quot;,&quot;+document.getElementById(&quot;CheckBit&quot;).value.toString()+<br> &quot;,&quot;+document.getElementById(&quot;DataBits&quot;).value.toString()+<br> &quot;,&quot;+document.getElementById(&quot;StopBits&quot;).value.toString();<br><br> MSComm1.OutBufferCount =0; //清空发送缓冲区<br> MSComm1.InBufferCount = 0; //滑空接收缓冲区<br><br> alert(&quot;已配置串口COM&quot;+MSComm1.CommPort+&quot;\n 参数:&quot;+MSComm1.Settings);<br><br> }catch(ex){alert(ex.message);}<br> }<br> else{ alert(&quot;请先关闭串口后再设置!&quot;);}<br> }</p> <p>function stringToHex(str,n){<br>     var val=&quot;&quot;;<br>     for(var i = 0; i &lt; str.length; i++){<br>       if(val == &quot;&quot;)<br>         val = str.charCodeAt(i).toString(16);<br>       else<br>         val += &quot;,&quot; + str.charCodeAt(i).toString(16);<br>     }<br>     return val;<br>   }</p> <p>function Send()<br> {<br> //alert(document.getElementById(&quot;txtSend&quot;).value);<br> var orgstr=document.getElementById(&quot;txtSend&quot;).value;<br><br> var newstr=&quot;&quot;;<br> var hexflag=document.getElementById(&quot;isSendHex&quot;).checked;<br> if(hexflag&amp;&amp;orgstr!=&quot;&quot;)<br> {<br> if(orgstr.substr(0,2)==&quot;0x&quot;||orgstr.substr(0,2)==&quot;0X&quot;) orgstr=orgstr.substring(2,orgstr.length);<br> if(orgstr.length%2!=0) orgstr=&quot;0&quot;+orgstr;<br> //alert(stringToHex(orgstr,16));<br> if((newstr=stringToHex(orgstr,16))==&quot;&quot;) {alert(&quot;错误的16进制数&quot;);return false;}<br> }<br> try{<br> MSComm1.Output=hexflag?newstr:orgstr;<br><br> }catch(ex)<br><br> {alert(ex.message);}<br> }</p> <p>function Receive()<br> {<br><br> //alert(&quot;InBufferCount::&quot;+MSComm1.InBufferCount);<br> document.getElementById(&quot;txtReceive&quot;).value += MSComm1.Input;<br><br> //alert(&quot;InBufferCount::&quot;+MSComm1.InBufferCount);<br><br> }</p> <p>function ClearReceived()<br> {<br> document.getElementById(&quot;txtReceive&quot;).innerText=&quot;&quot;;<br> } </p> <p>


配置串口

串口号: COM1COM2COM3COM4
波特率: 960057600115200
校验位: 无NONE奇ODD偶EVEN
数据位:
停止位:

发送区域

16进制
接收区域

16进制

CLASSID="CLSID:648A5600-2C6E-101B-82B6-000000000014"
CODEBASE="MSCOMM32.OCX">








  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答