예제 #1
0
        // 发送线程
        public static void SendData(string comStr, string cmdName)
        {
            string cmd = StrCommon.GetSerialCommand(comStr);

            byte[] cmdByteArr = StrCommon.GetByteArrayByHexStr(cmd);
            port.Write(cmdByteArr, 0, cmdByteArr.Length);
            Console.WriteLine("发送线程-" + cmdName + ":" + cmd);
        }
예제 #2
0
        /// <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);
        }