// 接收线程 public static void receivedata() { while (true) { byte[] rec = new byte[10]; port.Read(rec, 0, 10);//读取缓冲数据 string str = StrCommon.GetHexStrByByteArray(rec); Console.WriteLine("接收线程:{0}", str); Thread.Sleep(500); } }
/// <summary> /// 获取串口命令 /// </summary> /// <returns></returns> public static string GetSerialCommand(string command) { string STX = "F2";//F2H string LEN = "00" + command.Length.ToString().PadLeft(2, '0') + " "; byte[] bArray = StrCommon.GetByteArrayByHexStr(STX + LEN + StrCommon.StrToHex(command)); string firstStr = StrCommon.GetHexStrByByteArray(bArray).Replace(" ", ""); byte[] crcArray = CRCCore.GetCrcByByteArray(bArray, bArray.Length); string crcStr = StrCommon.GetHexStrByByteArray(crcArray).Replace(" ", ""); //firstStr + crcStr string retStr = (firstStr + crcStr).ToCharArray().Aggregate("", (result, c) => result += ((!string.IsNullOrEmpty(result) && (result.Length + 1) % 3 == 0) ? " " : "") + c.ToString()); return(retStr); }