/// <summary> /// 串口数据接收事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void serialPort_DataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e) { SerialPortIsReceiving = true; try { int n = Com.BytesToRead; byte[] buf = new byte[n]; Com.Read(buf, 0, n); //1.缓存数据 buffer.AddRange(buf); //2.完整性判断 if (buffer.Count == ReceiveDataLen) { byte[] readBuffer = new byte[buffer.Count]; buffer.CopyTo(0, readBuffer, 0, buffer.Count); _Veryfy(ReceiveVerify, ConvertUtilClass.byteTostring(readBuffer)); GC.KeepAlive(readBuffer); } GC.KeepAlive(buf); GC.KeepAlive(buffer); } catch (Exception ex) { Log.Error(ex.ToString()); } finally { SerialPortIsReceiving = false; } }
private void convert(object data) { try { string[] arr = (string[])data; int id = Convert.ToInt32(arr[0]); string HexData = arr[1]; int DataLen = Convert.ToInt32(arr[2]); string Decode = arr[3]; string ss = arr[1].Substring(6, DataLen); string Value = string.Empty; switch (Decode) { case "FLOAT-0.001": Value = Convert.ToDouble(Convert.ToInt32(ss, 16) * 0.001).ToString(); break; case "FLOAT-0.01": Value = Convert.ToDouble(Convert.ToInt32(ss, 16) * 0.01).ToString(); break; case "FLOAT-0.1": Value = Convert.ToDouble(Convert.ToInt32(ss, 16) * 0.1).ToString(); break; case "INT-10": Value = (Convert.ToInt32(ss, 16) * 10).ToString(); break; case "INT": Value = Convert.ToInt32(ss, 16).ToString(); break; case "STRING": Value = ConvertUtilClass.HexToChar(ss); break; case "FLOAT-IEEE754": Value = ConvertUtilClass.HexToFloat(ss).ToString(); break; default: Value = (Convert.ToInt32(ss, 16)).ToString(); break; } GlobalData.DevicesDataStorage[0][id]["Value"] = Value; GlobalData.DevicesDataStorage[0][id]["HexData"] = HexData; GlobalData.DevicesDataStorage[0][id]["RefTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//更新时间 }catch (Exception ex) { string msg = "数据转存错误:" + ex.ToString(); UIAction.AppendLog(msg); Log.Fatal(msg); } }
//执行优先命令 private void Exec_FirstCommand() { bool success = false; try { bool revstate; Stopwatch sw = new Stopwatch(); long i; for (int j = 0; j < GlobalData.FirstCommandDictionary.Count; j++)//遍历设备 { if (!GlobalData.START) { start = false; } if (!start || !serialport.opened) { break; } var item = GlobalData.FirstCommandDictionary.ElementAt(j); int devid = item.Key; for (int l = 0; l < GlobalData.FirstCommandDictionary[devid].Count; l++)//遍历可写命令 { var item2 = GlobalData.FirstCommandDictionary[devid].ElementAt(l); string pramaname = item2.Key; if (!GlobalData.FirstCommandDictionary[devid].ContainsKey(pramaname)) { Log.Info(pramaname + ":此设备无此参数"); continue; } if (GlobalData.FirstCommandDictionary[devid][pramaname]["Finish"] == "1" || GlobalData.FirstCommandDictionary[devid][pramaname]["Command"].Length == 0) { continue; //命令已经完成则下一个 } if (GlobalData.FirstCommandDictionary[devid][pramaname]["Status"] == "1") { if (!string.IsNullOrEmpty(GlobalData.FirstCommandDictionary[devid][pramaname]["SendTime"])) { DateTime.TryParse(GlobalData.FirstCommandDictionary[devid][pramaname]["SendTime"], out DateTime sendtime); TimeSpan timeSpan = DateTime.Now - sendtime; int.TryParse(GlobalData.FirstCommandDictionary[devid][pramaname]["ErrorWait"], out int errorwait); if (timeSpan.TotalSeconds > errorwait) { GlobalData.FirstCommandDictionary[devid][pramaname]["Status"] = "0"; GlobalData.FirstCommandDictionary[devid][pramaname]["Error"] = "0"; Log.Info("到达错误等待时间:" + GlobalData.FirstCommandDictionary[devid][pramaname]["Command"]); } else { Thread.Sleep(1); continue; } } } revstate = false; int.TryParse(GlobalData.FirstCommandDictionary[devid][pramaname]["TimeOut"], out int timeout);//接收数据超时时间:毫秒 if (TimeOut != 0) { timeout = TimeOut; } int.TryParse(GlobalData.FirstCommandDictionary[devid][pramaname]["DataLen"], out int datalen); serialport.ReceiveDataLen = datalen; serialport.ReceiveVerify = GlobalData.FirstCommandDictionary[devid][pramaname]["Verify"];//数据验证方法 if (string.Equals(GlobalData.FirstCommandDictionary[devid][pramaname]["Verify"], "HEAD")) { serialport.ReceiveHead = GlobalData.FirstCommandDictionary[devid][pramaname]["VerifyFlag"]; serialport.ReceiveVerifyLen = GlobalData.FirstCommandDictionary[devid][pramaname]["VerifyFlag"].Length; } GlobalData.FirstCommandDictionary[devid][pramaname]["SendTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string command = GlobalData.FirstCommandDictionary[devid][pramaname]["Command"]; byte[] datas = ConvertUtilClass.HexstrToByte(command); int resend = 0; RESEND: //重新发送 if (serialport.SendData(datas)) { //接收数据延时 resend++; sw.Start(); while (sw.ElapsedMilliseconds < timeout && start) { if (serialport.ReceiveFlag) { revstate = true; //接收到正确的数据 GlobalData.FirstCommandDictionary[devid][pramaname]["HexData"] = serialport.ReceiveData; GlobalData.FirstCommandDictionary[devid][pramaname]["Error"] = "0"; GlobalData.FirstCommandDictionary[devid][pramaname]["Status"] = "0"; GlobalData.FirstCommandDictionary[devid][pramaname]["RecTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); bool decoderesult = Exec_FirstCommand_DataConvert(command, serialport.ReceiveData, GlobalData.FirstCommandDictionary[devid][pramaname]["Decode"]); //数据解析结果 if (decoderesult) //如果数据解析结果为真则移除此命令 { SetStatusMsg("设备" + GlobalData.FirstCommandDictionary[devid][pramaname]["Remark"] + "命令正确执行!"); //发送通知 if (GlobalData.FirstCommandDictionary[devid][pramaname]["PramaName"].Length > 0) { GlobalData.DevicesDataStorage[0][devid]["Value"] = GlobalData.FirstCommandDictionary[devid][pramaname]["Value"]; //更新值 } GlobalData.FirstCommandDictionary[devid][pramaname]["Finish"] = "1"; //命令完成 if (GlobalData.FirstCommandCount > 0) { GlobalData.FirstCommandCount -= 1; //更新数量 } } break; } else { Thread.Sleep(10); } } i = sw.ElapsedMilliseconds; sw.Stop(); sw.Reset(); if (revstate == false) { if (resend == 1) { SetStatusMsg("重新发送"); goto RESEND;//重新发送 } GlobalData.FirstCommandDictionary[devid][pramaname]["Finish"] = "1"; GlobalData.FirstCommandDictionary[devid][pramaname]["Error"] = "1";//改为错误状态 string Remark = GlobalData.FirstCommandDictionary[devid][pramaname]["Remark"]; SetStatusMsg(Remark + "通讯异常!"); if (GlobalData.FirstCommandCount > 0) { GlobalData.FirstCommandCount -= 1; //更新数量 } } success = true; } else { success = false; break; } } } success = true; GC.KeepAlive(success); GC.KeepAlive(sw); } catch (Exception ex) { Log.Error("通讯错误:" + ex.ToString()); success = false; } finally { if (!success) { SetStatusMsg("串口异常!"); close_Collect(); } } }
private void run_PollingCommand(Dictionary <int, Dictionary <string, string> > polldic) { bool revstate = false; bool success = false; Stopwatch sw = new Stopwatch(); long i = 0; for (int j = 0; j < polldic.Count; j++) //for { if (!GlobalData.START) { start = false; } if (!start || !serialport.opened) { break; } if (GlobalData.FirstCommandCount > 0)//如果有优先命令,那么就先执行 { Exec_FirstCommand(); } var item = polldic.ElementAt(j); //for int count = item.Key; revstate = false; try { int timeout = GlobalData.TimeOut;//接收数据超时时间:毫秒 if (timeout < 1) { timeout = 200; } int.TryParse(polldic[count]["DataLen"], out int datalen); serialport.ReceiveDataLen = (datalen + 10) / 2; serialport.ReceiveVerify = "CRC16";//数据验证方法 serialport.ReceiveHead = "010302"; serialport.ReceiveVerifyLen = 6; string command = polldic[count]["Command"]; byte[] datas = ConvertUtilClass.HexstrToByte(command); if (serialport.SendData(datas)) { //接收数据延时 sw.Start(); Thread.Sleep(1); int y = 0; while (sw.ElapsedMilliseconds < timeout && start) { if (serialport.ReceiveFlag) { revstate = true; //接收到正确的数据 polldic[count]["HexData"] = serialport.ReceiveData; do_DataDecode(count, polldic[count]);//数据解析 break; } else { Thread.Sleep(1); } y++; } i = sw.ElapsedMilliseconds; sw.Stop(); sw.Reset(); if (revstate == false) { Log.Error("此命令未收到回应,编号:" + command + ",DataLen:" + polldic[count]["DataLen"]); } success = true; Log.Info("serialport.ReceiveData:" + serialport.ReceiveData); } else { success = false; break; } } catch (Exception ex) { Log.Error("发送轮询命令错误:" + ex.ToString()); success = false; } finally { if (!success) { SetStatusMsg("串口异常!"); close_Collect(); } } } GC.KeepAlive(sw); }