using System; 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; namespace MOKA_Factory_Tools { class SerialCMD { /// /// 串口指令读超时值,单位毫秒; /// 默认: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 Sendcmd(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 Sendcmd(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 Sendcmd(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 (Sendcmd(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 Sendcmd(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 (Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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) { if (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xBE }, new byte[] { 0x06 }, true, false, 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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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(Sendcmd(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(Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(Comport, 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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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(Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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( Sendcmd(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( Sendcmd(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( Sendcmd(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 (Sendcmd(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 (Sendcmd(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( Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 Sendcmd(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 (Sendcmd(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 (Sendcmd(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 Sendcmd(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 (Sendcmd(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 (Sendcmd(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 Sendcmd(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 Sendcmd(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 Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 (Sendcmd(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 Sendcmd(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 (Sendcmd(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 (Sendcmd(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 Sendcmd(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 (Sendcmd(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 (Sendcmd(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) { error = "返回DevcieCode长度错误,应为3 bytes"; return false; } } else { error = "返回DevcieCode类型错误,应为0x11 0x06"; return false; } } Log.WriteInfoLog(string.Format("GetDeviceCode Elapsed = {0}", watch.Elapsed.TotalMilliseconds)); return true; } /// /// 烧录FireTV项目Playready key /// /// /// /// /// /// /// /// public static bool SetFireTVPlayready(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x01,0x00 }, 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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x00 }, 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 Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x01, 0x02 }, 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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x02 }, 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 Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x01, 0x01 }, 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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x01 }, 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项目Hash key /// /// /// /// /// /// /// /// public static bool SetFireTVHashkey(SerialPort Comport, byte[] param, out byte[] result, out byte[] data, out string error, int waitTime) { return Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x01, 0x03 }, 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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x03 }, 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 Sendcmd(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 Sendcmd(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 Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x01, 0x06 }, 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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x06 }, 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 (Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x05 }, 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 (Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x09 }, 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 Sendcmd(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 (Sendcmd(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 Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x08 }, 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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x01, 0x08 }, 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 Sendcmd(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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x03, 0x0A }, 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 (Sendcmd(Comport, new byte[] { 0xAA }, new byte[] { 0xFC }, new byte[] { 0x01, 0x0A }, 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 Sendcmd(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 Sendcmd(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 (Sendcmd(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 /// /// 通讯逻辑 /// /// /// /// /// /// /// /// /// /// /// 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); bool mark = false; // 设置读超时; Comport.ReadTimeout = ReadTimeout; //delay(Comport, CMDID);//已有ReadTimeout了,这里多余; // ReadTimeout对ReadByte生效的话,所有ReadByte都会有超时响应,如果异常存在会导致更长的延时? while (true) { 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++) { result[j] = ACKhead[j]; } mark = true; goto CheckTCL2Receive; } } else if (onebit == ACKhead2[2]) { if (Comport.ReadByte() == ACKhead2[3]) if (Comport.ReadByte() == ACKhead2[4]) { for (int j = 0; j <= 4; j++) { result[j] = ACKhead2[2]; } error = "命令错误或无法识别数据返回"; Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead2) + "\r\n" + error); return false; } } else if (onebit == ACKhead3[2]) { if (Comport.ReadByte() == ACKhead3[3]) if (Comport.ReadByte() == ACKhead3[4]) { for (int j = 0; j <= 4; j++) { result[j] = ACKhead3[2]; } Log.WriteInfoLog("Received ErrorACK:" + SerialInit.ByteToHex(ACKhead3) + "\r\n" + error); error = "CRC16校验错误返回"; return false; } } } } //delay(Comport, CMDID); } CheckTCL2Receive: if (receive2) { //delay(Comport, CMDID, true); if (mark) { string Br = ""; string Sr = ""; // 分2段2次返回,第2次同样要再次设置超时值; Comport.ReadTimeout = ReadTimeout; while (Comport.BytesToRead >= 0) { if (Comport.ReadByte() == ACKhead[0]) { result[5] = ACKhead[0]; break; } } if (!TwoLengthBit) { int k = Comport.ReadByte(); result[6] = (byte)k; int timeout = 0; while (Comport.BytesToRead < (k - 2) && timeout < 20) { Thread.Sleep(100); timeout++; } Comport.Read(result, 7, k - 2); Comport.ReadTimeout = 200; Br = SerialInit.ByteToHex(result.Skip(8).Take(k - 5).ToArray()); Sr = Encoding.ASCII.GetString(result.Skip(8).Take(k - 5).ToArray()); data = result.Skip(8).Take(k - 5).ToArray(); Log.WriteInfoLog("Received:" + SerialInit.ByteToHex(result.Take(5 + k).ToArray())); } else { int mark1= Comport.ReadByte(); result[6] = (byte)mark1; int k1 = Comport.ReadByte(); int k2 = Comport.ReadByte(); result[7] = (byte)k1; result[8] = (byte)k2; int k = k1 * 256 + k2; int timeout = 0; while (Comport.BytesToRead < (k - 4) && timeout < 20) { Thread.Sleep(100); timeout++; } Comport.Read(result, 9, k - 4); Comport.ReadTimeout = 200; Br = SerialInit.ByteToHex(result.Skip(10).Take(k - 7).ToArray()); Sr = Encoding.ASCII.GetString(result.Skip(10).Take(k - 7).ToArray()); data = result.Skip(10).Take(k - 7).ToArray(); Log.WriteInfoLog("Received:" + SerialInit.ByteToHex(result.Take(5 + k).ToArray())); } Thread.Sleep(NextCommandWaitTime); return true; } else { return false; } } 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; } } // 这个函数在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++; } } } }