-------通过USB将硬件连接PC, 更新USB驱动,使用下面方法可控制相关设备。
//1声明一个串口对象 public static SerialPort MainModule_Port_ = new SerialPort(); //2给串口设置参数 CreateSerialPort(MainNode.MainModule_Port_, comboBox2.Text.Trim(), "9600", "2"); //3设置参数方法 /// <summary> /// 公共创建串口 /// </summary> /// <param name="SP">串口对象</param> /// <param name="SerialPortName">串口名</param> /// <param name="BaudRate">波特率</param> /// <param name="Parity">校验位</param> public void CreateSerialPort(SerialPort SP, string SerialPortName, string BaudRate, string Parity) { try { //串口属性类 //SerialPortParamter app = new SerialPortParamter(); SP.PortName = SerialPortName;//串口号 SP.BaudRate = Convert.ToInt32(BaudRate);//波特率 SP.Parity = StringToParity(Parity);//校验位 SP.DataBits = 8;//数据位 SP.StopBits = StringToStopBits("1");//解析停止位 //SP = app.setParatemer(); SP.DtrEnable = true; SP.RtsEnable = true; } catch (Exception ex) { MessageBox.Show("创建串口失败!" + SerialPortName); } } //3.1串口属性校验位 public static System.IO.Ports.Parity StringToParity(string parity) { if (string.IsNullOrEmpty(parity)) { return System.IO.Ports.Parity.None; } return (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), parity); } //3.2串口属性停止位 public static System.IO.Ports.StopBits StringToStopBits(string stopBits) { if (string.IsNullOrEmpty(stopBits)) { return System.IO.Ports.StopBits.None; } return (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), stopBits); } //////////目前已经创建好了一个串口,下面用来发送指令,下面为16进制的645协议 string mess="68 19 00 00 19 67 37 68 91 08 33 33 34 33 33 35 34 35 d7 16"; byte[] messArray=StrSplitToByte(mess); MainNode.MainModule_Port_.Write(messArray, 0, messArray.Length);//从串口发送协议,正常会将发送或收到帧保持到一个集合,供展示 ///字符串 转 byte 数组 public static byte[] StrSplitToByte(string inSplitString) { string[] byteStrings; byteStrings = inSplitString.Split(" ".ToCharArray()); byte[] byteOut; byteOut = new byte[byteStrings.Length]; for (int i = 0; i < byteStrings.Length; i++) { if (string.IsNullOrEmpty(byteStrings[i])) { continue; } byteOut[i] = Convert.ToByte(byteStrings[i], 16); } return byteOut; } //////////////发送了协议 ,我们希望得到回复,下面为如何接收协议 //接收帧方法,1376.2协议测试 private void __RecvThread_Agreement_13762() { while (1 == 1) { try { if (MainNode.MainModule_Port_.IsOpen) { int iReadSize = MainNode.MainModule_Port_.BytesToRead; if (iReadSize == 0) { Thread.Sleep(10); continue; } byte[] readBuffer = new byte[iReadSize]; MainNode.MainModule_Port_.Read(readBuffer, 0, iReadSize); //该方法是将接收的帧,在方法中根据帧格式进行判断,正确后放入同发送一起的集合中。 ProcessRecvData(readBuffer);//处理接收数据 } } catch (Exception e) { ;} } } ///////////////////////////////下面是算帧的校验位的方法 /// <summary> /// 更新校验码645 /// 参数无需" "分隔 /// </summary> public static string UpdateCS645(string baowen) { baowen = baowen.Trim(); string[] mess = null;// AnalysisMessage.separate(baowen, 2).Split(' '); if (baowen.Trim().IndexOf(' ') == -1) { mess = AnalysisMessage.separate(baowen, 2).Split(' '); } else { mess = baowen.Split(' '); } int cs = 0; for (int i = 0; i < mess.Length; i++) { cs += Convert.ToInt32((HexConvert.BCDstrToBin(mess[i])), 2); } string csV = HexConvert.Mess10to16((cs % 256).ToString()); if (csV.Length == 2) return csV; else if (csV.Length == 4) return csV.Substring(2, 2); return csV.Substring(1, 2); } /// <summary> /// 更新校验码1376.2 /// </summary> public static int UpdateCS(byte[] bs) { int cs = 0; for (int i = 3; i < bs.Length - 2; i++) { cs += bs[i]; } return cs; }