using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; using CrcTest; using System.Collections; using System.Threading; using SXLibrary; using System.Diagnostics; using System.Web.UI.WebControls; using System.ComponentModel; using CCWin.Win32.Com; using System.Drawing; namespace MOKA_Factory_Tools { class SerialCMD { #region 串口重试机制; public static bool EnableRetry { get; set; } = false; public static int RetryCount { get; set; } = 3; #endregion /// /// 串口指令读超时值,单位毫秒; /// 默认:3000ms /// public static int ReadTimeout { get; set; } = 3000; /// /// 串口完成一次指令的Write、Read后,下一条指令等待多久开始执行(即执行完当前指令后,等待多久函数才返回); /// 单位:毫秒,默认200毫秒 /// public static int NextCommandWaitTime { get; set; } = 100; public static int SerialTimeout { get; set; } static byte[] ACKhead = new byte[5] { 0xAB, 0x05, 0x0A, 0xDF, 0x4E };//命令可执行数据返回 static byte[] ACKhead2 = new byte[5] { 0xAB, 0x05, 0x0E, 0x9F, 0xCA };//命令错误或无法识别数据返回 static byte[] ACKhead3 = new byte[5] { 0xAB, 0x05, 0x0F, 0x8F, 0xEB };//CRC16校验错误返回 /// /// 进工厂 /// /// /// /// /// public static bool EnterFactory(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0x10 }, new byte[] { 0x01 },false,false, out result, out data,out error, waitTime); } /// /// 退工厂 /// /// /// /// /// /// public static bool LeaveFactory(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0x10 }, new byte[] { 0x00 }, false, false, out result, out data,out error, waitTime); } /// /// 白平衡初始化 /// /// /// /// /// /// public static bool WBInit(SerialPort Comport, out byte[] result, out byte[]data,out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x16 }, new byte[] { 0x02 }, false, false, out result,out data, out error, waitTime); } //get keys /// /// 查询PID /// /// /// /// /// /// public static bool GetProjectID(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime, bool WriteEDID=false) { if (!WriteEDID) Thread.Sleep(3000); return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x84 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime); } /// /// 获取软件版本 /// /// /// /// /// /// public static bool GetSoftVersion(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x57 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime); } /// /// 获取Device ID /// /// /// /// /// /// public static bool GetDeviceId(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x01 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x01) { data=data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回did类型错误,应为01"; return false; } } else return false; } /// /// 获取Client Type /// /// /// /// /// /// public static bool GetClientType(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x8C }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime); } /// /// 获取Mac /// /// /// /// /// /// public static bool GetMAC(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x00) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回MAC类型错误,应为00"; return false; } } else return false; } /// /// 获取HDCP /// /// /// /// /// /// public static bool GetHDCPKey(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEE }, new byte[] { 0x00 }, true, true, out result, out data, out error, waitTime); } /// /// 获取HDCP2.2 /// /// /// /// /// /// public static bool GetHDCPKey22(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xE7 }, new byte[] { 0x00 }, true, true, out result, out data, out error,waitTime); } /// /// 获取WiDi /// /// /// /// /// /// public static bool GetWidi(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xE7 }, new byte[] { 0x00 }, true, true, out result, out data, out error, waitTime); } /// /// 获取ESN /// /// /// /// /// /// public static bool GetNetflixESN(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime, bool FEFlag=false) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x06 }, true, FEFlag, out result, out data, out error, waitTime)) { if (data[0] == 0x06) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回NetflixESN类型错误,应为06"; return false; } } else return false; } /// /// 获取Widevine /// /// /// /// /// /// public static bool GetWidevine(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEC }, new byte[] { 0x00 }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x00) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Widevine类型错误,应为00"; return false; } } else return false; } /// /// 获取Ci /// /// /// /// /// /// public static bool GetCiKey(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEC }, new byte[] { 0x01 }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x01) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回CiKey类型错误,应为01"; return false; } } else return false; } /// /// 获取ECP /// /// /// /// /// /// public static bool GetECP(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEC }, new byte[] { 0x0C }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x0C) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回ECP类型错误,应为01"; return false; } } else return false; } /// /// 获取MGK /// /// /// /// /// /// public static bool GetMGKKey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x06 }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x06) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回MGKKey类型错误,应为06"; return false; } } else return false; } /// /// 获取OSD语言 /// /// /// /// /// /// public static bool GetOSDLanguage(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if(SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x97 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x00) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回OSD language类型错误,应为00"; return false; } } else return false; } /// /// 获取出货语言 /// /// /// /// /// /// public static bool GetShopLanguage(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if(SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x97 }, new byte[] { 0x01 }, true, false, out result, out data, out error, waitTime)) { { if (data[0] == 0x01) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回SHOP language类型错误,应为01"; return false; } } } else return false; } /// /// 获取当前信源 /// /// /// /// /// /// public static bool GetChannel(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x97 }, new byte[] { 0x13 }, true, false, out result, out data, out error, waitTime); } //Set keys /// /// 写入Pid /// /// /// /// /// /// /// public static bool SetProjectId(SerialPort Comport, byte[] param,out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x70 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入Device ID /// /// /// /// /// /// /// public static bool SetDeviceID(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xB2 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入Mac /// /// /// /// /// /// /// public static bool SetMAC(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xB3 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入HDCP /// /// /// /// /// /// /// public static bool SetHDCPKey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA,0xFE }, new byte[] { 0x86 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入HDCP22 /// /// /// /// /// /// /// public static bool SetHDCPKey22(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xE4 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入ESN /// /// /// /// /// /// /// public static bool SetNetflixESN(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, param.Length > 255 ? new byte[] { 0xAA,0xFE } : new byte[] { 0xAA }, new byte[] { 0x99,0x00 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入Widi /// /// /// /// /// /// /// /// /// public static bool SetWidi(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xE4 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入Widevine /// /// /// /// /// /// /// public static bool SetWidevine(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xE9,0x00 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入Ci /// /// /// /// /// /// /// public static bool SetCiKey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xE9, 0x01 }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入ECP /// /// /// /// /// /// /// public static bool SetECP(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xE9, 0x0C }, param, false, false, out result, out data, out error, waitTime); } /// /// 写入MGK /// /// /// /// /// /// /// public static bool SetMGKKey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0x99, 0x00 }, param, false, false, out result, out data, out error, waitTime); } //check keys /// /// 校验Mac /// /// /// /// /// /// public static bool CheckMAC(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if(SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xB4 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 1 && (data[0] == 0x01 || data[0] == 0x03 || data[0] == 0x05 || data[0] == 0x07)) { /* 0x00:All data hadn't beeninstalled 0x01:MAC address successful 0x02:Device ID successful 0x03:Mac and device ID had been installed 0x04:User ID successful 0x05:Mac and user ID had been installed 0x06:User ID and device ID had been installed 0x07:All data had been installed */ return true; } else { error = "Mac haven't been installed"; return false; } } else return false; } /// /// 校验Device ID /// /// /// /// /// /// public static bool CheckDeviceId(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xB4 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 1 && (data[0] == 0x02 || data[0] == 0x03 || data[0] == 0x06 || data[0] == 0x07)) { /* 0x00:All data hadn't beeninstalled 0x01:MAC address successful 0x02:Device ID successful 0x03:Mac and device ID had been installed 0x04:User ID successful 0x05:Mac and user ID had been installed 0x06:User ID and device ID had been installed 0x07:All data had been installed */ return true; } else { error = "Diveice ID haven't been installed"; return false; } } else return false; } /// /// 校验HDCP /// /// /// /// /// /// public static bool CheckHDCP(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x87 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 1 && data[0] == 0x01) return true; else { error = "HDCP KEY check fail,return status shoule be 0x01"; return false; } } else return false; } /// /// 校验ESN /// /// /// /// /// /// public static bool CheckNetflixESN(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x00 && data[1] == 0x01) return true; else { error = "ESN return status fail,should be 0x00,0x01"; return false; } } else return false; } /// /// 校验Widi /// /// /// /// /// /// public static bool CheckWidi(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if( SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xE5 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 1 && data[0] == 0x01) return true; else { error = "WiDi KEY check fail,return status shoule be 0x01"; return false; } } else return false; } /// /// 校验Widevine /// /// /// /// /// /// public static bool CheckWidevine(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if( SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEA }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x00 && data[1] == 0x01) return true; else { error = "Widevine return status fail,should be 0x00,0x01"; return false; } } else return false; } /// /// 校验Ci /// /// /// /// /// /// public static bool CheckCikey(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if( SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEA }, new byte[] { 0x01 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x01 && data[1] == 0x01) return true; else { error = "Ci return status fail,should be 0x01,0x01"; return false; } } else return false; } /// /// 校验ECP /// /// /// /// /// /// public static bool CheckECP(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEA }, new byte[] { 0x0C }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0C && data[1] == 0x01) return true; else { error = "ECP return status fail,should be 0x01,0x01"; return false; } } else return false; } /// /// 校验MGK /// /// /// /// /// /// public static bool CheckMGKkey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x00 && data[1] == 0x01) return true; else { error = "MGK return status fail,should be 0x00,0x01"; return false; } } else return false; } /// /// 校验HDCP22 /// /// /// /// /// /// public static bool CheckHDCP22(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if( SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xE5 }, new byte[] { 0x00 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 1 && data[0] == 0x01) return true; else { error = "HDCP22 KEY check fail,return status shoule be 0x01"; return false; } } else return false; } /// /// 开老化 /// /// /// /// /// /// public static bool StartWarmUpMode(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x13 }, new byte[] { 0x01 }, false, false, out result, out data, out error, waitTime); } /// /// 关老化 /// /// /// /// /// /// public static bool StopWarmUpMode(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x13 }, new byte[] { 0x00 }, false, false, out result, out data, out error, waitTime); } //set something /// /// 设置OSD语言 /// /// /// /// /// /// /// public static bool SetOSDLanguage(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x96, 0x00 }, param, false, false, out result, out data, out error, waitTime); } /// /// 设置出厂语言 /// /// /// /// /// /// /// public static bool SetShopLanguage(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x96, 0x01 }, param, false, false, out result, out data, out error, waitTime); } /// /// 预置频道 /// /// /// /// /// /// /// public static bool SetChannel(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x15 }, param, false, false, out result, out data, out error, waitTime); } /// /// 设置标准色温RGB值 /// /// /// /// /// /// /// public static bool SetWBNormal(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x4D ,0x04 ,0x01 ,0x01 }, param, false, false, out result, out data, out error, waitTime); } /// /// 设置冷色温RGB值 /// /// /// /// /// /// /// public static bool SetWBCool(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x4D, 0x04, 0x02, 0x01 }, param, false, false, out result, out data, out error, waitTime); } /// /// 设置暖色温RGB值 /// /// /// /// /// /// /// public static bool SetWBWarm(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x4D, 0x04, 0x03, 0x01 }, param, false, false, out result, out data, out error, waitTime); } /// /// SCBC ROKU 项目品牌服务信息抄写 /// /// /// /// /// /// /// public static bool SetROKUBrand(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x11 }, param, false, false, out result, out data, out error, waitTime); } /// /// SCBC ROKU 项目区域服务信息抄写 /// /// /// /// /// /// /// public static bool SetROKURegion(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x12 }, param, false, false, out result, out data, out error, waitTime); } /// /// SCBC ROKU 项目服务网站服务信息抄写 /// /// /// /// /// /// /// public static bool SetROKUSupportURL(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x13 }, param, false, false, out result, out data, out error, waitTime); } /// /// SCBC ROKU 项目服务电话服务信息抄写 /// /// /// /// /// /// /// public static bool SetROKUSupportPhone(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x14 }, param, false, false, out result, out data, out error, waitTime); } /// /// SCBC ROKU 项目服务生产日期信息抄写 /// /// /// /// /// /// /// public static bool SetROKUProductionDate(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error,int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x15 }, param, false, false, out result, out data, out error, waitTime); } /// /// SCBC ROKU 项目遥控器类型信息抄写 /// /// /// /// /// /// /// public static bool SetROKURemoteType(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x19 }, param, false, false, out result, out data, out error, waitTime); } /// /// SCBC ROKU 项目品牌服务信息抄写状态查询 /// /// /// /// /// /// public static bool CheckROKUBrand(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x0B }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0B && data[1] == 0x01) return true; else { error = "ROKU Brand check fail,return status shoule be 0x0B ,0x01"; return false; } } else return false; } /// /// SCBC ROKU 项目服务网址服务信息抄写状态查询 /// /// /// /// /// /// public static bool CheckROKUSupportURL(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x0D }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0D && data[1] == 0x01) return true; else { error = "ROKU Support URL check fail,return status shoule be 0x0D ,0x01"; return false; } } else return false; } /// /// SCBC ROKU 项目区域服务信息抄写状态查询 /// /// /// /// /// /// public static bool CheckROKURegion(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x0C }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0C && data[1] == 0x01) return true; else { error = "ROKU Region check fail,return status shoule be 0x0C ,0x01"; return false; } } else return false; } /// /// SCBC ROKU 项目服务电话服务信息抄写状态查询 /// /// /// /// /// /// public static bool CheckROKUSupportPhone(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x0E }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0E && data[1] == 0x01) return true; else { error = "ROKU Support Phone check fail,return status shoule be 0x0E ,0x01"; return false; } } else return false; } /// /// SCBC ROKU 项目生产日期服务信息抄写状态查询 /// /// /// /// /// /// public static bool CheckROKUProductionDate(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x0F }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0F && data[1] == 0x01) return true; else { error = "Production Date check fail,return status shoule be 0x0F ,0x01"; return false; } } else return false; } /// /// SCBC ROKU 项目遥控器信息抄写状态查询 /// /// /// /// /// /// public static bool CheckROKURemoteType(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x13 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x13 && data[1] == 0x01) return true; else { error = "Remote type check fail,return status shoule be 0x13 ,0x01"; return false; } } else return false; } /// /// 获取SCBC ROKU 项目品牌服务信息 /// /// /// /// /// /// public static bool GetROKUBrand(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x10 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x10) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回brand类型错误,应为10"; return false; } } else return false; } /// /// 获取SCBC ROKU 项目区域服务信息 /// /// /// /// /// /// public static bool GetROKURegion(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x11 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x11) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Region类型错误,应为11"; return false; } } else return false; } /// /// 获取SCBC ROKU 项目服务网址服务信息 /// /// /// /// /// /// public static bool GetROKUSupportURL(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x12 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x12) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Support URL类型错误,应为12"; return false; } } else return false; } /// /// 获取SCBC ROKU 项目服务网址服务信息 /// /// /// /// /// /// public static bool GetROKUSupportPhone(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x13 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x13) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Support Phone类型错误,应为13"; return false; } } else return false; } /// /// 获取SCBC ROKU 项目服务网址服务信息 /// /// /// /// /// /// public static bool GetROKUProductionDate(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x14 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x14) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Production Date类型错误,应为14"; return false; } } else return false; } /// /// 获取SCBC ROKU 项目遥控器类型信息(00---普通遥控器;01---语音遥控器) /// /// /// /// /// /// public static bool GetROKURemoteType(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x18 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x18) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Remote Type类型错误,应为0x18"; return false; } } else return false; } /// /// 产品机型名抄写 /// /// /// /// /// /// /// public static bool SetModelName(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x09 }, param, false, false, out result, out data, out error,waitTime); } /// /// 产品机型名检查 /// /// /// /// /// /// public static bool CheckModelName(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x08 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x08 && data[1] == 0x01) return true; else { error = "Model name check fail,return status shoule be 0x08 ,0x01"; return false; } } else return false; } /// /// 产品机型名读取 /// /// /// /// /// /// public static bool GetModelName(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x0E }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x0E) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Model name类型错误,应为0E"; return false; } } else return false; } /// /// 写入Attestation Key /// /// /// /// /// /// /// public static bool SetAttestationKey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xE9, 0x0D }, param, false, false, out result, out data, out error, waitTime); } /// /// 检查Attestation Key /// /// /// /// /// /// public static bool CheckAttestationKey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEA }, new byte[] { 0x0D }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0D && data[1] == 0x01) return true; else { error = "Attestation Key return status fail,should be 0x0D,0x01"; return false; } } else return false; } /// /// 读取AttestationKey /// /// /// /// /// /// public static bool GetAttestationKey(SerialPort Comport, out byte[] result, out byte[] data, out string error,int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEC }, new byte[] { 0x0D }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x0D) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回AttestationKey类型错误,应为0D"; return false; } } else return false; } /// /// FireTV项目WiFi_Mac抄写 /// /// /// /// /// /// /// /// public static bool SetFireTVWifiMac(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x16 }, param, false, false, out result, out data, out error, waitTime); } /// /// FireTV项目BT_Mac抄写 /// /// /// /// /// /// /// /// public static bool SetFireTVBTMac(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x17 }, param, false, false, out result, out data, out error, waitTime); } /// /// FireTV项目DSN抄写 /// /// /// /// /// /// /// /// public static bool SetFireTVDSN(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x18 }, param, false, false, out result, out data, out error, waitTime); } /// /// 校验FireTV项目WiFi_Mac /// /// /// /// /// /// public static bool CheckFireTVWifiMac(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x10 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x10 && data[1] == 0x01) return true; else { error = "FireTVWifiMac return status fail,should be 0x10,0x01"; return false; } } else return false; } /// /// 校验FireTV项目BT_Mac /// /// /// /// /// /// public static bool CheckFireTVBTMac(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x11 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x11 && data[1] == 0x01) return true; else { error = "FireTVBTMac return status fail,should be 0x11,0x01"; return false; } } else return false; } /// /// 校验FireTV项目DSN /// /// /// /// /// /// public static bool CheckFireTVDSN(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x12 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x12 && data[1] == 0x01) return true; else { error = "FireTVDSN return status fail,should be 0x12,0x01"; return false; } } else return false; } /// /// 获取FireTV项目WiFi_Mac /// /// /// /// /// /// public static bool GetFireTVWiFi_Mac(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x15 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x15) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回FireTVWiFi_Mac类型错误,应为15"; return false; } } else return false; } /// /// 获取FireTV项目BT_Mac /// /// /// /// /// /// public static bool GetFireTVBT_Mac(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x16 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x16) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回FireTVBT_Mac类型错误,应为16"; return false; } } else return false; } /// /// 获取FireTV项目DSN /// /// /// /// /// /// public static bool GetFireTVDSN(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x17 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x17) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回FireTVDSN类型错误,应为17"; return false; } } else return false; } /// /// FireTV项目Fairplay key抄写 /// /// /// /// /// /// /// public static bool SetFireTVFairplay(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xE9, 0x0E }, param, false, false, out result, out data, out error, waitTime); } /// /// 校验FireTV项目Fairplay key /// /// /// /// /// /// /// public static bool CheckFireTVFairplay(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEA }, new byte[] { 0x0E }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x0E && data[1] == 0x01) return true; else { error = "Fairplay return status fail,should be 0x0E,0x01"; return false; } } else return false; } /// /// 读取FireTV项目Fairplay key /// /// /// /// /// /// /// public static bool ReadFireTVFairplay(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEC }, new byte[] { 0x0F }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x0F) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回Fairplay类型错误,应为0F"; return false; } } else return false; } /// /// DHA激活生成 /// /// /// /// /// /// /// public static bool CreateFireTVDHA(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x96, 0x03 }, new byte[] { 0x00 }, false, false, out result, out data, out error, waitTime); } /// /// 检查DHA生成情况 /// /// /// /// /// /// /// public static bool CheckFireTVDHA(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x14 }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 2 && data[0] == 0x14 && data[1] == 0x01) return true; else { error = "DHA return status fail,should be 0x14,0x01"; return false; } } else return false; } /// /// 读取设备码 /// /// /// /// /// /// public static bool GetFireTVDevcieCode(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { // 计算函数耗时; Stopwatch watch = new Stopwatch(); watch.Start(); if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xEC }, new byte[] { 0x11,0x06 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x11 & data[1] == 0x06) { data = data.Skip(2).Take(data.Length - 2).ToArray(); if (data.Length == 3) return true; error = "返回DevcieCode长度错误,应为3 bytes"; } else { error = "返回DevcieCode类型错误,应为0x11 0x06"; } } Log.WriteInfoLog(string.Format("GetDeviceCode Elapsed = {0}", watch.Elapsed.TotalMilliseconds)); return false; } /// /// 烧录FireTV项目Playready key /// /// /// /// /// /// /// /// public static bool SetFireTVPlayready(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x00 }, param, false, false, out result, out data, out error, waitTime); } /// /// 检查FireTV项目Playready key /// /// /// /// /// /// /// public static bool CheckFireTVPlayready(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x00 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x00 && data[2] == 0x01) return true; else { error = "Playready return status fail,should be 0x02,0x00,0x01"; return false; } } else return false; } /// /// 读取FireTV项目Playready key /// /// /// /// /// /// /// public static bool ReadFireTVPlayready(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x00 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x00) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回Playready类型错误,应为0x04, 0x00"; return false; } } else return false; } /// /// 烧录FireTV项目LEK /// /// /// /// /// /// /// /// public static bool SetFireTVLEK(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x02 }, param, false, false, out result, out data, out error, waitTime); } /// /// 检查FireTV项目LEK /// /// /// /// /// /// /// public static bool CheckFireTVLEK(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x02 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x02 && data[2] == 0x01) return true; else { error = "LEK return status fail,should be 0x02,0x02,0x01"; return false; } } else return false; } /// /// 读取FireTV项目LEK /// /// /// /// /// /// /// public static bool ReadFireTVLEK(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x02 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x02) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回LEK类型错误,应为0x04,0x02"; return false; } } else return false; } /// /// 烧录FireTV项目PEK /// /// /// /// /// /// /// /// public static bool SetFireTVPEK(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x01 }, param, false, false, out result, out data, out error, waitTime); } /// /// 检查FireTV项目PEK /// /// /// /// /// /// /// public static bool CheckFireTVPEK(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x01 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x01 && data[2] == 0x01) return true; else { error = "PEK return status fail,should be 0x02,0x01,0x01"; return false; } } else return false; } /// /// 读取FireTV项目PEK /// /// /// /// /// /// /// public static bool ReadFireTVPEK(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x01 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x01) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回PEK类型错误,应为0x04 0x01"; return false; } } else return false; } /// /// 烧录FireTV项目的DAK /// /// /// /// /// /// /// /// public static bool SetFireTVDAK(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x0E }, param, false, false, out result, out data, out error, waitTime); } /// /// 校验FireTV项目的DAK抄写状态; /// /// /// /// /// /// /// public static bool CheckFireTVDAK(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x0E }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x0E && data[2] == 0x01) return true; else { error = "DAK return status fail,should be 0x02,0x0E,0x01"; return false; } } else return false; } /// /// 激活小米FireTV项目的FVP /// /// /// /// /// /// /// /// public static bool ActiveMiFireTVFVP(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x00, 0x0F }, param, false, false, out result, out data, out error, waitTime); } /// /// 校验小米FireTV项目的FVP激活状态; /// /// /// /// /// /// /// public static bool CheckMiFireTVFVP(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x0F }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x0F && data[2] == 0x01) return true; else { error = "FVP return status fail,should be 0x02,0x0F,0x01"; return false; } } else return false; } /// /// 烧录FireTV项目Hash key /// /// /// /// /// /// /// /// public static bool SetFireTVHashkey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x03 }, param, false, false, out result, out data, out error, waitTime); } /// /// 检查FireTV项目Hash key /// /// /// /// /// /// /// public static bool CheckFireTVHashkey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x03 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x03 && data[2] == 0x01) return true; else { error = "Hashkey return status fail,should be 0x02,0x03,0x01"; return false; } } else return false; } /// /// 读取FireTV项目Hash key /// /// /// /// /// /// /// public static bool ReadFireTVHashkey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x03 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x03) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回Hashkey类型错误,应为0x04 0x03"; return false; } } else return false; } /// /// 烧录条码 /// /// /// /// /// /// /// /// public static bool SetBarcode(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x71 }, param, false, false, out result, out data, out error, waitTime); } /// /// 烧录PSN /// /// /// /// /// /// /// /// public static bool SetFireTVPSN(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x00, 0x05 }, param, false, false, out result, out data, out error, waitTime); } /// /// 烧录FireTV项目Youtube /// /// /// /// /// /// /// /// public static bool SetFireTVYoutube(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x06 }, param, false, false, out result, out data, out error, waitTime); } /// /// 检查FireTV项目Youtube /// /// /// /// /// /// /// public static bool CheckFireTVYoutube(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x06 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x06 && data[2] == 0x01) return true; else { error = "Youtube return status fail,should be 0x02,0x06,0x01"; return false; } } else return false; } /// /// 读取FireTV项目Youtube /// /// /// /// /// /// /// public static bool ReadFireTVYoutube(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x06 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x06) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回Youtube类型错误,应为0x04 0x06"; return false; } } else return false; } /// /// 读取条码 /// /// /// /// /// /// /// public static bool ReadBarcode(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x03 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x03) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回S/N类型错误,应为0x03"; return false; } } else return false; } /// /// 读取PSN /// /// /// /// /// /// /// public static bool ReadFireTVPSN(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { // 计算函数耗时; Stopwatch watch = new Stopwatch(); watch.Start(); if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x05 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x05) { data = data.Skip(2).Take(data.Length - 2).ToArray(); Log.WriteInfoLog(string.Format("Read PSN Elapsed={0}", watch.Elapsed.TotalMilliseconds)); return true; } else { error = "返回PSN类型错误,应为0x04 0x05"; } } return false; } /// /// 读取日本客户的ACAS ID; /// /// /// /// /// /// /// public static bool ReadJapaneseACASID(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x15 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x15) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回ACASID类型错误,应为15"; return false; } } return false; } /// /// 读取EDID Pid; /// /// /// /// /// /// /// public static bool ReadEDIDPid(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x09 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x09) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回EDID PID错误,应为0x04 0x07"; } } return false; } /// /// 设置EDID Pid; /// /// /// /// /// /// /// /// public static bool SetEDIDPid(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x00, 0x09 }, param, false, false, out result, out data, out error, waitTime); } /// /// 读取EDID Mode Name; /// /// /// /// /// /// /// public static bool ReadEDIDModelName(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x0E }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x0E) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "返回EDID Mode Name错误,应为01"; } } return false; } /// /// 抄写EDID Mode Name; /// /// /// /// /// /// /// /// public static bool SetEDIDModelName(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x99, 0x09 }, param, false, false, out result, out data, out error, waitTime); } /// /// 读取ACAS Key /// /// /// /// /// /// /// 注意返回值为:md5xxxxx public static bool ReadACASKeyData(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x08 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x08) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回ACAS KEY DATA错误!"; } } return false; } public static bool CheckACASKeyData(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x08 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x08 && data[2] == 0x01) { return true; } else { error = "检验ACAS KEY DATA错误,应为0x02 0x08 0x01"; } } return false; } /// /// 设置ACAS Key /// /// /// /// /// /// /// /// public static bool SetACASKeyData(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x08 }, param, false, false, out result, out data, out error, waitTime); } /// /// 读取ACAS Key /// /// /// /// /// /// /// 注意返回值为:md5xxxxx public static bool ReadACASKeyTool(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x0A }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x0A) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回ACAS KEY TOOL 错误!"; } } return false; } public static bool CheckACASKeyTool(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x0A }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x0A && data[2] == 0x01) { return true; } else { error = "检验ACAS KEY TOOL 错误,应为0x02 0x0A 0x01"; } } return false; } /// /// 设置ACAS Key /// /// /// /// /// /// /// /// public static bool SetACASKeyTool(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x0A }, param, false, false, out result, out data, out error, waitTime); } #region 波兰工厂临时功能-抄写所有Key后执行Trigger和Check指令; public static bool TriggerDHA(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x96, 0x03 }, new byte[]{ 0x00 }, false, false, out result, out data, out error, waitTime); } public static bool CheckDHA(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0x9A }, new byte[] { 0x014 }, true, false, out result, out data, out error, waitTime)) { if (data[0] == 0x14 && data[1] == 0x01) { data = data.Skip(1).Take(data.Length - 1).ToArray(); return true; } else { error = "Check DHA失败,应为01"; } } return false; } #endregion public static bool SetDolbyKey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x13 }, param, false, false, out result, out data, out error, waitTime); } public static bool CheckDolbyKey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x13 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x13 && data[2] == 0x01) { return true; } else { error = "检验 Dolby Key 错误,应为0x02 0x13 0x01"; } } return false; } public static bool ReadDolbyKey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x13 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x13) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回Dolby Key错误!"; } } return false; } /// /// /// /// /// /// /// /// /// /// public static bool SetKFPKey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x15 }, param, false, false, out result, out data, out error, waitTime); } public static bool CheckKFPKey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x15 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x15 && data[2] == 0x01) { return true; } else { error = "检验 KFP Key 错误,应为0x02 0x15 0x01"; } } return false; } public static bool ReadKFPKey(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x15 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x15) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回KFP Key错误!"; } } return false; } /// /// 把FE当作数据的一部分进行解析; /// /// /// /// 实际接收到的串口数据长度 /// 接收到的数据(注:不包含ACK内容) /// 整个协议包的数据长度(注:正常接收是等于reallyLength,小于reallyLength的话可能接收了部分异常数据,大于reallyLength表示未接收完) /// 用户数据的取值起始值(注:除掉头+长度+命令码) /// private static bool ParseDataFEIsLen(SerialPort Comport, ref byte[] CMDID, ref int reallyLength, ref byte[] receive2Data, out int dataLen, out int dataIndex) { dataLen = 0; dataIndex = 0; if (CMDID[0] == 0xFC) { if (receive2Data[2] == 0xFC && receive2Data[3] == CMDID[1] + 1) {// FE则表示长度; dataIndex = 4; dataLen = 0xFE; } else {// 如果FE是大于255标记; Log.WriteErrorLog("FE may be is a mark"); return false; } } else { if (receive2Data[2] == CMDID[0] + 1) {// 否则FE则表示长度; dataIndex = 3; dataLen = 0xFE; } else {// 如果FE是大于255标记; Log.WriteErrorLog("FE may be is a mark"); return false; } } int tryCount = 5; while ( reallyLength < dataLen ) { if (tryCount-- == 0) break; // 读取剩余数据; int len = Comport.Read(receive2Data, reallyLength, 20480 - reallyLength); reallyLength += len; Thread.Sleep(30); } if (reallyLength < dataLen) { Log.WriteErrorLog("recv data is not enough!"); return false; } //CRC校验 byte crcHighByte = receive2Data[dataLen - 2]; byte crcLowByte = receive2Data[dataLen - 1]; string crc = CrcTest.Program.CalcCRC16(receive2Data.Take(dataLen - 2).ToArray()); byte[] calCrc = SerialInit.HexToByte(crc); if (crcHighByte != calCrc[0] || crcLowByte != calCrc[1]) { Log.WriteErrorLog("crc check error"); return false; } return true; } /// /// 把FE当作长度标记进行解析; /// /// /// /// 实际接收到的串口数据长度 /// 接收到的数据(注:不包含ACK内容) /// 整个协议包的数据长度(注:正常接收是等于reallyLength,小于reallyLength的话可能接收了部分异常数据,大于reallyLength表示未接收完) /// 用户数据的取值起始值(注:除掉头+长度+命令码) /// private static bool ParseDataFEIsMark(SerialPort Comport, ref byte[] CMDID, ref int reallyLength, ref byte[] receive2Data, out int dataLen, out int dataIndex) { dataLen = 0; dataIndex = 0; int tryCount = 0; if (CMDID[0] == 0xFC) { // 如果FE是大于255标记; tryCount = 5; while (reallyLength < 6) { int len = Comport.Read(receive2Data, reallyLength, 20480 - reallyLength); reallyLength += len; if (tryCount-- == 0) break; Thread.Sleep(20); } if (reallyLength < 6) {// 未接收够字节; return false; } if (receive2Data[4] == 0xFC && receive2Data[5] == CMDID[1] + 1) { dataIndex = 5; dataLen = (int)((receive2Data[2] << 8) | receive2Data[3]); } else { // 异常数据; return false; } } else if (receive2Data[4] == CMDID[0] + 1) { dataIndex = 5; dataLen = (int)((receive2Data[2] << 8) | receive2Data[3]); } else { return false; } tryCount = 50; while (reallyLength < dataLen) { if (tryCount-- == 0) break; // 读取剩余数据; int len = Comport.Read(receive2Data, reallyLength, 20480 - reallyLength); reallyLength += len; Thread.Sleep(20); } if (reallyLength < dataLen) { Log.WriteErrorLog("recv data is not enough!"); return false; } //CRC校验 byte crcHighByte = receive2Data[dataLen - 2]; byte crcLowByte = receive2Data[dataLen - 1]; string crc = CrcTest.Program.CalcCRC16(receive2Data.Take(dataLen - 2).ToArray()); byte[] calCrc = SerialInit.HexToByte(crc); if (crcHighByte != calCrc[0] || crcLowByte != calCrc[1]) { Log.WriteErrorLog("crc check error"); return false; } return true; } /// /// 没有FE字节的数据解析; /// /// /// /// 实际接收到的串口数据长度 /// 接收到的数据(注:不包含ACK内容) /// 整个协议包的数据长度(注:正常接收是等于reallyLength,小于reallyLength的话可能接收了部分异常数据,大于reallyLength表示未接收完) /// 用户数据的取值起始值(注:除掉头+长度+命令码) /// private static bool ParseDataWithoutFE(SerialPort Comport, ref byte[] CMDID, ref int reallyLength, ref byte[] receive2Data, out int dataLen, out int dataIndex) { dataLen = 0; dataIndex = 0; int tryCount = 0; if (CMDID[0] == 0xFC) { tryCount = 5; while (reallyLength < 5) { int len = Comport.Read(receive2Data, reallyLength, 20480 - reallyLength); reallyLength += len; if (tryCount-- == 0) break; Thread.Sleep(10); } if (reallyLength < 5) {// 未接收够字节; return false; } if (receive2Data[2] == 0xFC && receive2Data[3] == CMDID[1] + 1) { dataIndex = 3; dataLen = receive2Data[1]; } else { // 异常数据; return false; } } else if (receive2Data[2] == CMDID[0] + 1) { dataIndex = 3; dataLen = receive2Data[1]; } else { return false; } tryCount = 5; while (reallyLength < dataLen) { if (tryCount-- == 0) break; // 读取剩余数据; int len = Comport.Read(receive2Data, reallyLength, 20480 - reallyLength); reallyLength += len; Thread.Sleep(30); } if (reallyLength < dataLen) { Log.WriteErrorLog("recv data is not enough!"); return false; } //CRC校验 byte crcHighByte = receive2Data[dataLen - 2]; byte crcLowByte = receive2Data[dataLen - 1]; string crc = CrcTest.Program.CalcCRC16(receive2Data.Take(dataLen - 2).ToArray()); byte[] calCrc = SerialInit.HexToByte(crc); if (crcHighByte != calCrc[0] || crcLowByte != calCrc[1]) { Log.WriteErrorLog("crc check error"); return false; } return true; } private static bool ParseACK(SerialPort Comport, ref byte[] receiveData, out string error) { error = ""; // 设置读超时; Comport.ReadTimeout = ReadTimeout; if (Comport.ReadByte() == ACKhead[0]) { if (Comport.ReadByte() == ACKhead[1]) { int onebit = Comport.ReadByte(); if (onebit == ACKhead[2]) { if (Comport.ReadByte() == ACKhead[3]) { if (Comport.ReadByte() == ACKhead[4]) { for (int j = 0; j <= 4; j++) { receiveData[j] = ACKhead[j]; } // ACK返回正确; return true; } } } else if (onebit == ACKhead2[2]) { if (Comport.ReadByte() == ACKhead2[3]) { if (Comport.ReadByte() == ACKhead2[4]) { for (int j = 0; j <= 4; j++) { receiveData[j] = ACKhead2[2]; } error = "命令错误或无法识别数据返回"; Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead2) + "\r\n" + error); } } } else if (onebit == ACKhead3[2]) { if (Comport.ReadByte() == ACKhead3[3]) { if (Comport.ReadByte() == ACKhead3[4]) { for (int j = 0; j <= 4; j++) { receiveData[j] = ACKhead3[2]; } Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead3) + "\r\n" + error); error = "CRC16校验错误返回"; } } } } } return false; } /// /// 通讯逻辑 /// /// /// /// /// /// /// /// /// /// /// private static bool Sendcmd(SerialPort Comport, byte[] CMDHead,byte[] CMDID, byte[] param,bool receive2,bool TwoLengthBit,out byte[] result,out byte[] data,out string error,int waitTime) { result = new byte[20480]; data = new byte[20480]; error = ""; Comport.ReadExisting(); ArrayList command = new ArrayList(); command.Add(CMDHead); int Wholelength = CMDHead.Length * 2 + param.Length + CMDID.Length + 2; if(CMDHead.Length==1) command.Add(new byte[1] { Convert.ToByte(Wholelength) } ); else if(CMDHead.Length==2) command.Add(new byte[2] { Convert.ToByte(Wholelength/256), Convert.ToByte(Wholelength % 256) }); else { error = "无法识别的命令头"; return false; } command.Add(CMDID); command.Add(param); ArrayList head1 = new ArrayList(); byte[] head = new byte[Convert.ToUInt16(param.Length + CMDHead.Length * 2 + CMDID.Length)]; foreach (byte[] b in command) { foreach (byte b2 in b) head1.Add(b2); } head1.CopyTo(head); try { string CRC = CrcTest.Program.CalcCRC16(head); command.Add(SerialInit.HexToByte(CRC)); string s = ""; byte[] wdata = new byte[0] { }; foreach (byte[] b in command) { wdata= wdata.Concat(b).ToArray(); } Comport.WriteTimeout = 3000; Comport.Write(wdata, 0, wdata.Length); s += SerialInit.ByteToHex(wdata); Log.WriteInfoLog("Write:" + s); if ( !ParseACK(Comport, ref result, out error) ) { Log.WriteErrorLog("Received Error:" + SerialInit.ByteToHex(result.Take(5).ToArray())); return false; } if (receive2) { int TryCount = 5; int reallyLength = 0; byte[] receive2Data = new byte[20480]; Comport.ReadTimeout = ReadTimeout; // 读够最基本的5字节; while ( reallyLength <= 5 ) { Thread.Sleep(10); int len = Comport.Read(receive2Data, reallyLength, 20480 - reallyLength); reallyLength += len; if (TryCount-- == 0)break; } // 循环5次仍未满足5个字节; if (reallyLength < 5) { Log.WriteErrorLog("Received Error:" + SerialInit.ByteToHex(receive2Data.Take(reallyLength).ToArray())); return false; } if (receive2Data[0] != 0xAB) { Log.WriteErrorLog("Received Error:" + SerialInit.ByteToHex(receive2Data.Take(reallyLength).ToArray())); return false; } int dataLen = 0; int dataIndex = 0; if (receive2Data[1] == 0xFE) { if (!ParseDataFEIsLen(Comport, ref CMDID, ref reallyLength, ref receive2Data, out dataLen, out dataIndex)) { if (!ParseDataFEIsMark(Comport, ref CMDID, ref reallyLength, ref receive2Data, out dataLen, out dataIndex)) { Log.WriteErrorLog("Received Error:" + SerialInit.ByteToHex(receive2Data.Take(reallyLength).ToArray())); return false; } } } else { if (!ParseDataWithoutFE(Comport, ref CMDID, ref reallyLength, ref receive2Data, out dataLen, out dataIndex)) { Log.WriteErrorLog("Received Error:" + SerialInit.ByteToHex(receive2Data.Take(reallyLength).ToArray())); return false; } } data = receive2Data.Skip(dataIndex).Take(dataLen - dataIndex - 2).ToArray(); Log.WriteInfoLog("Receive Data:" + SerialInit.ByteToHex(result.Take(5).ToArray()) + SerialInit.ByteToHex(receive2Data.ToArray())); Thread.Sleep(NextCommandWaitTime); return true; } else { Log.WriteInfoLog("Received ACK:" + SerialInit.ByteToHex(result.Take(5).ToArray())); Thread.Sleep(NextCommandWaitTime); return true; } } catch(Exception ex) { error = ex.Message; Log.WriteErrorLog("Serial Error:" + ex.Message); return false; } } private static bool SendcmdEx(SerialPort Comport, byte[] CMDHead, byte[] CMDID, byte[] param, bool receive2, bool TwoLengthBit, out byte[] result, out byte[] data, out string error, int waitTime) { if (!EnableRetry) return Sendcmd(Comport, CMDHead, CMDID, param, receive2, TwoLengthBit, out result, out data, out error, waitTime); int count = RetryCount; bool bRet = false; do { --count; Log.WriteInfoLog(string.Format("Retrying {0} time", RetryCount-count)); bRet = Sendcmd(Comport, CMDHead, CMDID, param, receive2, TwoLengthBit, out result, out data, out error, waitTime); if (bRet) break; } while (count > 0); return bRet; } // 这个函数在receive2时,不具有超时有效性,不保证能读取成功; private static void delay(SerialPort Comport, byte[] CMDID, bool receive2 = false) { int l = 0; int num = SerialTimeout * 20; // CMD = 0x10表示EnterFactory指令; if (CMDID.Length == 1 && CMDID[0] == 0x10) num = 20; while (Comport.BytesToRead < 5 && l < num) { Thread.Sleep(SerialTimeout); l++; } } /// /// RMCA抄写 /// /// /// /// /// /// /// /// public static bool SetRMCA(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return SendcmdEx(Comport, new byte[] { 0xAA, 0xFE }, new byte[] { 0xFC, 0x00, 0x18 }, param, false, false, out result, out data, out error, waitTime); } /// /// RCMA校验 /// /// /// /// /// /// /// public static bool CheckRMCA(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x01, 0x18 }, new byte[] { }, true, false, out result, out data, out error, waitTime)) { if (data.Length == 3 && data[0] == 0x02 && data[1] == 0x18 && data[2] == 0x01) return true; else { error = "RMCA return status fail,should be 0x02,0x18,0x01"; return false; } } else return false; } /// /// RCMA读取 /// /// /// /// /// /// /// public static bool ReadRMCA(SerialPort Comport, out byte[] result, out byte[] data, out string error, int waitTime) { if (SendcmdEx(Comport, new byte[] { 0xAA }, new byte[] { 0xFC, 0x03, 0x18 }, new byte[] { }, true, true, out result, out data, out error, waitTime)) { if (data[0] == 0x04 && data[1] == 0x18) { data = data.Skip(2).Take(data.Length - 2).ToArray(); return true; } else { error = "返回RMCA类型错误,应为0x04, 0x18"; return false; } } else return false; } } }