Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#串行端口在收到数据后挂起_C# - Fatal编程技术网

C#串行端口在收到数据后挂起

C#串行端口在收到数据后挂起,c#,C#,这是我的密码 public void ClosePort() { if (comPort.IsOpen == true) { Thread CloseDown = new Thread(() => CloseSerialOnExit(comPort)); //close port in new thread to avoid hang CloseDown.Start(); //close po

这是我的密码

    public void ClosePort()
    {
        if (comPort.IsOpen == true)
        {
            Thread CloseDown = new Thread(() => CloseSerialOnExit(comPort)); //close port in new thread to avoid hang

            CloseDown.Start(); //close port in new thread to avoid hang
        }
    }

    private void CloseSerialOnExit(SerialPort port)
    {
        port.DiscardOutBuffer();
        port.Close();

        DisplayData(MessageType.Error, "Port communication has been closed" + " " + DateTime.Now + "\n");
    }
为什么我的应用程序收到数据时关闭不工作?。这是本案的另一个解决方案吗?我的工作步骤是:

  • 连接端口
  • 扫描并接收数据
  • 断开端口连接,然后再次从连接端口重复启动,但当我们要启动连接端口时,系统显示错误消息:
  • 访问com端口被拒绝

    我认为发生这种情况是因为港口实际上并没有关闭。如何关闭港口

    这是我在开放端口中的代码

    public bool OpenPort()
        {
            try
            {
                //first check if the port is already open
                //if its open then close it
                if (comPort.IsOpen == true) comPort.Close();
    
                //set the properties of our SerialPort Object
                comPort.BaudRate = int.Parse(_baudRate);    //BaudRate
                comPort.DataBits = int.Parse(_dataBits);    //DataBits
                comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), _stopBits);    //StopBits
                comPort.Parity = (Parity)Enum.Parse(typeof(Parity), _parity);    //Parity
                comPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), _parity);    //Parity                
                comPort.PortName = _portName;   //PortName
                //now open the port
                comPort.WriteTimeout = 400;//Write timeout, if the efficiency of the serial driver software, can effectively avoid the deadlock
                comPort.ReadTimeout = 400;//Read timeout, ibid
                comPort.Open();
                comPort.DtrEnable = false;
                comPort.RtsEnable = false;
                //display message
                DisplayData(MessageType.Normal, "Port opened at " + DateTime.Now + "\n");
                //return true
                return true;
            }
            catch (Exception ex)
            {
                DisplayData(MessageType.Error, ex.Message);
                return false;
            }
        }
    
    这是我在comPort_datareceived中的代码

    void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
                //determine the mode the user selected (binary/string)
                switch (CurrentTransmissionType)
                {
                    //user chose string
                    case TransmissionType.Text:
                        //read data waiting in the buffer
                        string msg = comPort.ReadExisting();
                        //display the data to the user
                        DisplayData(MessageType.Incoming, msg + "\n");
                        break;
                    //user chose binary
                    case TransmissionType.Hex:
                        //retrieve number of bytes in the buffer
                        int OpeCode = 0;
                        int RequestID = 0;
                        int Product = 0;
                        int IPenNo = 0;
                        string status = " ";
    
                        while (true)
                        {
                            DateTime time = DateTime.Now;             // Use current time.
                            string format = "dddddddd, MMMMM d, yyyy HH:mm:ss";
                            string currentTime = time.ToString(format);
                            byte[] TrueData = new byte[256];
    
                            try
                            {
                                int bytes = comPort.BytesToRead;
                                if (bytes == 0) continue;
                                //create a byte array to hold the awaiting data
                                byte[] comBuffer = new byte[bytes];
                                comPort.Read(comBuffer, 0, bytes);
                                DisplayData(MessageType.Incoming, "Hexa :" + ByteToHex(comBuffer) + "\n");
                                DisplayData(MessageType.Incoming, "Byte :" + bytes.ToString() + "\n");
    
                                if (bytes == 3)
                                {
                                    var lines = File.ReadAllLines(Fullpath).ToList();
    
                                    // Remove as many lines as you'd like from the end
                                    if (lines.Count > 2)
                                    {
                                        lines.RemoveRange(lines.Count - 2, 2);
                                    }
    
                                    // Iterate backwards through the list until we've updated 2 (or more or less) lines
                                    var linesUpdated = 0;
    
                                    for (var i = lines.Count - 1; i >= 0 && linesUpdated < 1; i--)
                                    {
                                        if (lines[i].Contains("OK"))
                                        {
                                            lines[i] = lines[i].Replace("OK", "NG");
                                            linesUpdated++;
                                        }
                                    }
    
                                    File.WriteAllLines(Fullpath, lines.ToArray());
                                    //DisplayData(MessageType.Incoming, "NG" + "\n");
                                }
    
                                if (bytes == 2)
                                {
                                    continue;
                                }
    
                                int etx_ok = 0;
    
                                for (int i = 0; i < bytes; i++)
                                {
                                    if (comBuffer[i] == 0x02)
                                    {
                                        //DisplayData(MessageType.Incoming, "cek II:" + checkStatus + "\n");
                                        int length = comBuffer[i + 1];
                                        DisplayData(MessageType.Incoming, "Length=" + length.ToString() + "\n");
    
                                        if (String.IsNullOrEmpty(bytes.ToString()))
                                        {
                                            status = "NG";
                                        }
                                        if (length + i + 1 != bytes && status == " ")
                                        {
                                            DisplayData(MessageType.Incoming, length.ToString() + " " + i.ToString() + " " + bytes.ToString() + " ");
                                            status = "NG";
                                            DisplayData(MessageType.Incoming, "ERROR \n");
                                            //break;
                                        }
                                        else
                                        {
                                            status = "OK";
                                        }
    
                                        DisplayData(MessageType.Incoming, "ini statusnya  : " + status + "\n");
    
                                        if (comBuffer[length + i - 1] == 0x03)
                                        {
                                            DisplayData(MessageType.Incoming, "ETX OK\n");
                                            etx_ok = 1;
                                            OpeCode = comBuffer[i + 2];
                                            DisplayData(MessageType.Incoming, "OpeCode=" + OpeCode.ToString() + ",");
                                            RequestID = comBuffer[i + 3];
                                            DisplayData(MessageType.Incoming, "RequestID=" + RequestID.ToString() + ",");
                                            int StoreCode = comBuffer[i + 4];
                                            DisplayData(MessageType.Incoming, "StoreCode=" + StoreCode.ToString() + ",");
                                            int ProductBatteryTraining = comBuffer[i + 5];
                                            DisplayData(MessageType.Incoming, "ProductBatteryTraining=" + ProductBatteryTraining.ToString() + ",");
    
                                            Product = ProductBatteryTraining >> 4;
                                            DisplayData(MessageType.Incoming, "  Product=" + Product.ToString() + ",");
                                            int Battery = ProductBatteryTraining & 4;
                                            DisplayData(MessageType.Incoming, "  Batery=" + Battery.ToString() + ",");
                                            int Training = ProductBatteryTraining & 1;
                                            DisplayData(MessageType.Incoming, "  Training=" + Training.ToString() + ",");
    
                                            IPenNo = comBuffer[i + 6];
                                            DisplayData(MessageType.Incoming, "IPenNo=" + IPenNo.ToString() + ",");
    
                                            int CrcCalc = comBuffer[length + i] + 0x11;
    
                                            for (int j = 7, k = 0; j < length; j++, k++)
                                            {
                                                //syswrite STDOUT , "TrueDataX " . $length . "\n";
                                                DisplayData(MessageType.Incoming, "TrueDataX " + length.ToString() + "," + "\n");
                                                TrueData[k] = comBuffer[i + j];
                                            }
                                            if (OpeCode == 0x63)
                                            {
                                                byte[] replyStr = new byte[] { 
                                            Convert.ToByte(0x45), Convert.ToByte(0x53), Convert.ToByte(0x4c), Convert.ToByte(0x14), Convert.ToByte(0x09), Convert.ToByte(0x00),                             //#Length Change
                                            Convert.ToByte(0x02), Convert.ToByte(0x08),Convert.ToByte(OpeCode), Convert.ToByte(RequestID),Convert.ToByte(Product-1), Convert.ToByte(IPenNo),                                       //#Reply Header Data                   
                                            Convert.ToByte(0x00),                                                                                                                                           //#Reply Status      
                                            Convert.ToByte(0x03), Convert.ToByte(CrcCalc),                                                                                                                     //#Footer Data    
                                            Convert.ToByte(0xcc), Convert.ToByte(0xcc) 
                                            };
    
                                                comPort.Write(replyStr, 0, replyStr.Length);
    
                                                //write file to textfile
                                                //string path = @"d:\yosafat\testfile\tes1_Friday0916201614.33.txt"; 
                                                string IPenID = IPenNo.ToString();
                                                string appendText = ("IPen ID \t" + "Datetime\t\t\t\t\t" + "Status" + Environment.NewLine + IPenID + "\t\t" + currentTime + "\t\t" + status + Environment.NewLine);
                                                File.AppendAllText(Fullpath, appendText);
                                            }
                                        }
                                        else
                                        {
                                            OpeCode = 0;
                                            //syswrite STDOUT , "ETX Bad Data" . $length . "\n";
                                            DisplayData(MessageType.Incoming, "ETX Bad Data" + length.ToString() + "\n");
                                            break;
                                        }
                                    }
                                    if (etx_ok == 1)
                                    {
                                        break;
                                    }
                                }
                            }
                            catch (Exception) { }
                        }
                }
            }
        public static int GetFirstOccurance(byte byteToFind, byte[] byteArray)
        {
            return Array.IndexOf(byteArray, byteToFind);
        }
    
    void comPort\u DataReceived(对象发送方,SerialDataReceivedEventArgs e)
    {
    //确定用户选择的模式(二进制/字符串)
    开关(电流传输类型)
    {
    //用户选择的字符串
    案例传输类型。文本:
    //读取缓冲区中等待的数据
    字符串msg=comPort.ReadExisting();
    //向用户显示数据
    显示数据(MessageType.Incoming,msg+“\n”);
    打破
    //用户选择二进制
    变速箱类型十六进制:
    //检索缓冲区中的字节数
    int OpeCode=0;
    int RequestID=0;
    int乘积=0;
    int-IPenNo=0;
    字符串状态=”;
    while(true)
    {
    DateTime time=DateTime.Now;//使用当前时间。
    字符串格式=“dddddddd,mmmmmd,yyyy HH:mm:ss”;
    字符串currentTime=time.ToString(格式);
    字节[]TrueData=新字节[256];
    尝试
    {
    int bytes=comPort.BytesToRead;
    如果(字节==0)继续;
    //创建一个字节数组来保存等待的数据
    字节[]comBuffer=新字节[字节];
    comPort.Read(comBuffer,0,字节);
    显示数据(MessageType.Incoming,“Hexa:”+ByteToHex(comBuffer)+“\n”);
    显示数据(MessageType.Incoming,“字节:”+bytes.ToString()+“\n”);
    如果(字节==3)
    {
    var lines=File.ReadAllLines(Fullpath.ToList();
    //从末端删除任意数量的线条
    如果(lines.Count>2)
    {
    lines.RemoveRange(lines.Count-2,2);
    }
    //向后遍历列表,直到更新了2行(或更多或更少)
    var linesUpdated=0;
    对于(变量i=lines.Count-1;i>=0&&linesUpdated<1;i--)
    {
    如果(第[i]行包含(“确定”))
    {
    第[i]行=第[i]行。替换(“OK”、“NG”);
    linesUpdated++;
    }
    }
    File.writeAllines(完整路径,line.ToArray());
    //显示数据(MessageType.Incoming,“NG”+“\n”);
    }
    如果(字节==2)
    {
    继续;
    }
    int etx_ok=0;
    for(int i=0;iThread CloseDown = null;
    public void ClosePort()
    {
        if (comPort.IsOpen == true)
        {
            CloseDown = new Thread(() => CloseSerialOnExit(comPort)); //close port in new thread to avoid hang
            CloseDown.Start(); //close port in new thread to avoid hang
        }
    }
    
    while (true)
    {
        //...
        try {
            //...
        } catch (Exception) { return; //add this}
    }