async Task <Cls.utils.M_SendType> ReturnResult(SerialPort _sp, byte[] _order, CancellationToken ct) { if (ct.IsCancellationRequested) { return(m_checkType); } else { m_checkType = await SendOrder(_sp, _order); if (m_checkType == Cls.utils.M_SendType.NoResult) { m_checkType = await SendOrder(_sp, _order); } M_lstSendType.Add(m_checkType); return(m_checkType); } }
async Task <Cls.utils.M_SendType> SendOrder(SerialPort _sp, byte[] _order) { try { if (_sp.IsOpen) { await Task.Delay(100); m_checkType = Cls.utils.M_SendType.NoResult; if (_sp.PortName.ToLower() == "com3") { m_checkType = Cls.utils.M_SendType.pumptrue; } _sp.DiscardOutBuffer(); _sp.Write(_order, 0, _order.Length); #if RunStartTest await Task.Delay(900);//Test #endif return(m_checkType); } else { return(Cls.utils.M_SendType.portfalse); } } catch (Exception ee) { if (this.InvokeRequired) { this.BeginInvoke((EventHandler)(delegate { MessageBox.Show(this, ee.Message, "通讯错误", MessageBoxButtons.OK, MessageBoxIcon.Error); })); } else { MessageBox.Show(this, ee.Message, "通讯错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(Cls.utils.M_SendType.portfalse); } }
void port_main_DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.Sleep(50); if (M_Closing_main) { return; } try { M_Listening_main = true; int n = this.port_main.BytesToRead; byte[] buf = new byte[n]; //声明一个临时数组存储当前来的串口数据 port_main.Read(buf, 0, n); //读取缓冲数据 M_buffer_main.AddRange(buf); //-------------------------------串口数据处理流程--------------------------------- //1、读取缓存区数据到一个byte[]数组; //2、按字节找帧头,若不是所需要的帧头则去掉该字节; //3、找到帧头后,进行校验,校验通过则处理数据; //4、处理数据(包含更新界面)完成之后,删除该条命令;继续缓存区下一条命令的处理; //-------------------------------------------------------------------------------- while (M_buffer_main.Count >= 5) { if (M_buffer_main[0] == 0xFF) { int len = M_buffer_main[2]; //有校验 if (checkOut(M_buffer_main)) { switch (M_buffer_main[1]) { #region 夹管阀状态 夹管阀状态位XX: 00松管状态/01夹管状态 // 阀状态 夹(1) 松(2) 松(3) 松(4) 松(5) 松(6) //FE B1 06 01(1) 00(2) 00(3) 00(4) 00(5) 00(6) 48 ED case 0xB1: //夹管阀状态 if (M_buffer_main[3] == 0x03 || M_buffer_main[4] == 0x03 || M_buffer_main[5] == 0x03 || M_buffer_main[6] == 0x03 || M_buffer_main[7] == 0x03 || M_buffer_main[8] == 0x03) { m_checkType = Cls.utils.M_SendType.vfalse; } else { m_checkType = Cls.utils.M_SendType.vtrue; } break; #endregion #region 声光 case 0xA1: m_checkType = Cls.utils.M_SendType.vltrue; break; #endregion #region 气泡检测 //气泡检测1开启 FF F1 00 0E ED case 0xF1: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测1报警 FF 1F 00 E1 ED case 0x1F: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测1关闭 FF F2 00 0D ED case 0xF2: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测2开启 FF F3 00 0C ED case 0xF3: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测2报警 FF 2F 00 D1 ED case 0x2F: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测2关闭 FF F4 00 0B ED case 0xF4: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测3开启 FF F5 00 0A ED case 0xF5: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测3报警 FF 3F 00 C1 ED case 0x3F: m_checkType = Cls.utils.M_SendType.bubbletrue; break; //气泡检测3关闭 FF F6 00 09 ED case 0xF6: m_checkType = Cls.utils.M_SendType.bubbletrue; break; #endregion #region 加热系统 //启动加热系统 FF EE 00 11 ED //停止加热系统 FF EF 00 10 ED case 0xEE: break; case 0xEF: break; #endregion #region 血泵旋钮调速 //顺时针转动 FF 0A 00 F5 ED //逆时针转动 FF 1A 00 E5 ED case 0x0A: //顺时针存储1 //#if LOG_MAINPORT_DATARECEIVE // getLog.WriteLogFile("clockwise:1 count:" + m_lstCircleDirection.Count.ToString()); //#endif break; case 0x1A: //逆时针存储0 //#if LOG_MAINPORT_DATARECEIVE // getLog.WriteLogFile("counterclockwise:0 count" + m_lstCircleDirection.Count.ToString()); //#endif break; #endregion } M_buffer_main.RemoveRange(0, 5); } else { M_buffer_main.RemoveAt(0); } } else { //如果数据开始不是FF,则删除数据 M_buffer_main.RemoveAt(0); } } } catch (Exception ee) { MessageBox.Show(this, ee.ToString()); } finally { M_Listening_main = false; } }