public static int SendCommand(byte cmd0, byte cmd1, byte[] data) { int dlen = 0; if (data != null) { dlen = data.Length; } int slen = dlen + 6; byte[] buf = new byte[slen]; buf[0] = 0xFE; buf[1] = (byte)(dlen >> 8); buf[2] = (byte)(dlen & 0x00FF); buf[3] = cmd0; buf[4] = cmd1; if (data != null) { Array.Copy(data, 0, buf, 5, dlen); } buf[slen - 1] = CosLibs.GetXor(buf, 1, (slen - 1)); if (Port.IsOpen) { Port.Write(buf, 0, buf.Length); if (myRecieveMessageListener != null) { myRecieveMessageListener(buf, null); } } return(0); }
private static int check_package(byte[] pdata, int startID, int len) { int slen; byte fcs, sum; if (len < 6) { return(1); } slen = BtoU16(pdata, (startID + 1)) + 6; if (slen > len)//package length error! { return(1); } fcs = pdata[startID + (slen - 1)]; sum = CosLibs.GetXor(pdata, startID + 1, (slen - 2)); if (fcs == sum) { return(0); } else { return(1); } }