在串行端口上拒绝“ COM”访问

时间:2019-07-02 09:18:11

标签: c# c#-2.0

我正在用c#制作一个dll以将EDC连接到PC,并通过USB端口在Internet Explorer上显示它,第一次尝试,成功完成,代码连接到EDC,EDC抛出一个值,将其转换为xml,并在Internet Explorer上显示xml,当我再次尝试时,Internet Explorer抛出“拒绝访问COM”的执行,当我关闭Internet Explorer时,代码再次运行,当我再次尝试时,它抛出相同的值。

如何解决?请帮助,我对这些SerialPort真的很陌生

我在互联网上搜索,他们建议打开端口后将其关闭,我尝试了一下,但仍然抛出异常。

用于发送和接收的C#代码

 public string CobaAja(string a)
{    
    SerialPort port;
    port = new SerialPort(com, p);
    port.Parity = Parity.None;
    port.BaudRate = 9600; //115200
    port.DataBits = 8;
    port.StopBits = StopBits.One;
    port.Handshake = Handshake.None;
    port.RtsEnable = true;

    //sending
    port.Open();
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(a);
    port.Write(messageSent, 0, messageSent.Length);

    //receive
     string data;
     string xmlBuild;
     byte[] buffer = new byte[port.ReadBufferSize];
     int bytesToRead = port.Read(buffer, 0, buffer.Length);
     while ((bytesToRead = port.Read(buffer, 0, buffer.Length)) > 0)
     {
          data = Encoding.ASCII.GetString(buffer, 0, bytesToRead);
           //these are the codes for creating XML 
           //After creating XML, i try to return the xml value 
           xmlBuild = sb.ToString();
           return xmlBuild;
      }
          port.close();
          return a;
    }

用于调用dll并在控制台上显示结果的javascript代码

var objPP = new ActiveXObject("Nyoba.Dulu");
 var coba = objPP.CobaAja(xml);
 console.log(coba);

0 个答案:

没有答案