对序列com的访问被拒绝

时间:2017-06-01 05:49:10

标签: c# iis-7 serial-port razor-2

我正在使用IIS 7和c#razor编写一个非常简单的逻辑 我正在使用System.IO.Ports.SerialPort和USB到串口适配器使用 usb串行控制器d 和一些“运行”之后,在那些“运行”中一切正常,该程序由于某种原因获得访问被拒绝到comX

我还检查了im发送的参数是否正确(波特率,奇偶校验等)

 string[] ports = getAvilablePorts(fs);
 int[] baudRate = new int[] { 2400, 9600, 19200, 38400, 115200 };
 System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();

 if (OpenSerailport(fs, serialPort, ports, baudRate[1], 
       System.Text.Encoding.ASCII, System.IO.Ports.StopBits.One, dataBits, System.IO.Ports.Parity.None,                                  System.IO.Ports.Handshake.None, readTimeOut, writeTimeout))
     {    
       string grossWeight = readData(serialPort, fs);  

      } 

和功能

string[] getAvilablePorts(FileStream fs)
    {

        string[] ports = System.IO.Ports.SerialPort.GetPortNames();
    //   log(fs, false, "portsAvilable:");
        for (int i = 0; i < ports.Length; i++)
        {
        //    log(fs, false, ports[i]);
        }
      //  log(fs, true);
        return ports;


    }




 string readData(System.IO.Ports.SerialPort serialPort, FileStream fs)
    {

        string grossWeight = "", data = "";



        if (!serialPort.IsOpen)
            ReOpenPort(serialPort, fs);

         serialPort.DiscardInBuffer();
        try
        {

            while (true)
            {           


                if(serialPort.BytesToRead > 0 )
                {
                    data = serialPort.ReadExisting();
                    data = data.Substring(1, 23);
                    break;
                }
                 else
            {
               //log(fs, true, "no data was sent from:", serialPort.PortName);
                break;
            }

            }               


        }
        catch (Exception ex)
        {
           // log(fs, true, "could not read from", serialPort.PortName, "reason:", ex.Message);            
            return "";
        }
        finally
        {
            fs.Close();  
        }



        if (data.ToLower().Contains("e"));
         //   log(fs, true, "ERROR:", "Error reading data from scale", "time:", Convert.ToString(DateTime.Now));
        else if (data.ToLower().Contains("o"));
           // log(fs, true, "ERROR:", "Overload", "time:", Convert.ToString(DateTime.Now));
        else if (data.ToLower().Contains("m"));
           // log(fs, true, "ERROR:", "Motion", "time:", Convert.ToString(DateTime.Now));
        else
        {

            grossWeight = data.Substring(7, 6);

           // log(fs, true, "READ DATA FROM SCALE:", "netWeight:", netWeight, "grossWeight", grossWeight, "peekValue:", peakValues);
        }

        if (!String.IsNullOrWhiteSpace(grossWeight))
            return grossWeight;
        else
            return "";


    }


bool OpenSerailport(FileStream fs, System.IO.Ports.SerialPort serialPort, string[] ports, int baudRate, System.Text.Encoding encoding, System.IO.Ports.StopBits stopBits,
        int dataBits, System.IO.Ports.Parity parity, /*System.Environment newLine,*/ System.IO.Ports.Handshake handShake, int readTimeOut, int writeTimeout)
    {
        try
        {
            if (serialPort.IsOpen)//close previous connections if there are any
                serialPort.Close();

            //init serial port
            serialPort.Encoding = encoding;
            serialPort.StopBits = stopBits;
            serialPort.DataBits = dataBits;
            serialPort.Parity = parity;
            serialPort.NewLine = System.Environment.NewLine;
            serialPort.Handshake = handShake;
            serialPort.ReadTimeout = readTimeOut;
            serialPort.WriteTimeout = writeTimeout;
            serialPort.PortName = ports[ports.Length  - 1];
            serialPort.BaudRate = baudRate;
            if (!serialPort.IsOpen)
            {
                serialPort.Open();
                return true;
            }
            return false;

        }
        catch (UnauthorizedAccessException ex)
        {
          //  log(fs, true, "CANNOT OPEN PORT", "portName:", Convert.ToString(serialPort.PortName), "reason", ex.Message);
            fs.Close();
            return false;

        }
        finally
        {
        //    log(fs, true, "GIVEN DATA:", "Encoidng:", Convert.ToString(serialPort.Encoding), "StopBits:", Convert.ToString(serialPort.StopBits), "dataBits:", Convert.ToString(serialPort.DataBits),
        //        "Parity:", Convert.ToString(serialPort.Parity), "newLine:", "\\n \\r", "handshake:", Convert.ToString(serialPort.Handshake),
        //        "readTimeout:", Convert.ToString(serialPort.ReadTimeout), "writeTimeOut:", Convert.ToString(serialPort.WriteTimeout), "portname:", Convert.ToString(serialPort.PortName),
        //        "baudRate:", Convert.ToString(serialPort.BaudRate));

        }

1 个答案:

答案 0 :(得分:0)

我设法通过添加SerialPort.Close();

来解决问题

到我的所有异常处理程序