//回复数据的方法 void SendData(object sender) { //测试 //cs.sp.WriteLine("bbbbbbbbbbbbbbbbbbbbb"); ComBussiness.SendData(com); }
/// <summary> /// 发送数据(未使用) /// </summary> //public event EventHandler<SendDataEventArgs> OnSendData; /// <summary> /// 有一个客户端连接上来 /// </summary> //public event EventHandler<ConnectedEventArgs> OnConnected; /// <summary> /// 断开释放 /// </summary> //public event EventHandler<DisconnectedEventArgs> OnDisconnected; #endregion public ComServer(string Portname, int Baudrate, string serviceID) { PortName = Portname; BaudRate = Baudrate; ServiceID = serviceID; Cs = ComBussiness.GetComSatelliteList(); CQ = new ComQueue(); CT = new ComThread(this); }
public void Start() { if (sp == null) { sp = new SerialPort(); sp.PortName = PortName; sp.BaudRate = BaudRate; sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); sp.Open(); ComBussiness.ToQcsd(); } //重启 else if (sp != null && !sp.IsOpen) { try { sp = new SerialPort(); sp.PortName = PortName; sp.BaudRate = BaudRate; sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); sp.Open(); ComBussiness.ToQcsd(); } catch (Exception ex) { Service.ServiceControl.log.Warn(DateTime.Now + "com服务(" + sp.PortName + ":" + sp.BaudRate + ")重启失败!", ex); } } ////卫星发送启动后测试 //byte[] b = Service.EnCoder.HexStrToByteArray("24434F55542C302C312C302C3232373730382C302C32352C34362C7E7E0100000000010451EF001D020001140801093511F1F1000000000150F0F0140801093539230003730003DBAC2C020D"); //byte[] b = Service.EnCoder.HexStrToByteArray("24545354412C302C302C322C332C322C302C312C3232373730382C31313136333035302C36302C332C302C302C382C2C"); //byte[] b = Service.EnCoder.HexStrToByteArray("24545354412C342C342C322C332C322C302C312C3232373730382C31313136333035302C36302C332C302C302C380D0A"); //byte[] b = Service.EnCoder.HexStrToByteArray("24434F55542C302C312C302C3232373730382C302C39362C35332C7E7E010000000002045132002402001D140819154708F1F1000000000250F0F01407300700261900005038121230FF03080403EA0B2C060D0A"); //byte[] b = Service.EnCoder.HexStrToByteArray("2454585351004E022AC446022AC301E0007E7E000015000001123430002B020019150430111014F1F100150000014BF0F00000000000201900009726190000973C23000000003812000003B0CDD0"); //string str="24 49 43 4A 43 00 0C 00 00 00 00 2B 24 49 43 58 58 00 16 02 2A C4 00 1D 77 14 06 00 3C 04 00 00 00 94 24 58 54 5A 4A 00 0D 02 2A C4 00 00 D9 24 5A 4A 58 58 00 15 02 2A C4 01 00 64 02 02 04 02 00 04 02 A8 24 54 58 53 51 00 4E 02 2A C4 46 02 2A C3 01 E0 00 7E 7E 00 00 15 00 00 01 12 34 30 00 2B 02 00 19 15 04 30 11 10 14 F1 F1 00 15 00 00 01 4B F0 F0 00 00 00 00 00 20 19 00 00 97 26 19 00 00 97 3C 23 00 00 00 00 38 12 00 00 03 B0 CD D0 24 46 4B 58 58 00 10 02 2A C4 00 54 58 53 51 DB "; //string str1 = "24 54 58 53 51 00 4E 02 2A C4 46 02 2A C3 01 E0 00 7E 7E 00 00 15 00 00 01 12 34 30 00 2B 02 00 19 15 04 30 11 10 14 F1 F1 00 15 00 00 01 4B F0 F0 00 00 00 00 00 20 19 00 00 97 26 19 00 00 97 3C 23 00 00 00 00 38 12 00 00 03 B0 CD D0"; //string str1 = "24 54 58 58 58 00 1F 02 2A C3 62 02 2A C3 00 00 00 58 A4 C4 E3 BA C3 D6 D0 B9 FA C8 CB 00 B1"; //str1 = "24545858580050022AC363022AC4000001E07E7E000015000001123430002B020013150504155214F1F100150000014BF0F00000000000201900017226190001723C230000000038120000038A2E0049"; //str1 = "24 5A 4A 58 58 00 15 02 2A C3 01 00 64 02 01 04 01 00 01 00 A8 "; //byte[] b = Service.EnCoder.HexStrToByteArray(str1.Replace(" ", "")); //sp.Write(b, 0, b.Length); }
/// <summary> /// 发送数据(未使用) /// </summary> //public event EventHandler<SendDataEventArgs> OnSendData; /// <summary> /// 有一个客户端连接上来 /// </summary> //public event EventHandler<ConnectedEventArgs> OnConnected; /// <summary> /// 断开释放 /// </summary> //public event EventHandler<DisconnectedEventArgs> OnDisconnected; #endregion public ComServer(string Portname, int Baudrate, string satellite, string serviceID) { PortName = Portname; BaudRate = Baudrate; ServiceID = serviceID; Satellite = satellite; Cs = ComBussiness.GetComSatelliteList(); CQ = new ComQueue(); CT = new ComThread(this); //原版本卫星协议的卫星状态 CState = new ComState(); //新版本卫星协议4.0的卫星状态 CStateFor4 = new ComStateFor4(); }
/// <summary> /// 收到上报数据给卫星回复 /// </summary> /// <param name="CS"></param> public static void Reply(ComServer CS) { if (CS.sp.IsOpen) { lock (CS.sp) { byte[] vBuffer = ASCIIEncoding.ASCII.GetBytes("$COSS,1,"); ComBussiness.XorSAT(ref vBuffer); CS.sp.Write(vBuffer, 0, vBuffer.Length); Service.ServiceControl.LogInfoToTxt(Service.ServiceEnum.NFOINDEX.COM, "", vBuffer, "ASC"); Service.ServiceControl.LogInfoToTxt(Service.ServiceEnum.NFOINDEX.COM, "", vBuffer); } } }
/// <summary> /// 定时发送查询卫星状态指令 /// </summary> void AccessCom() { Thread.Sleep(10 * 1000); //10秒后开始执行该方法 while (com.sp.IsOpen) //串口打开 { //原版本卫星协议 //ComBussiness.GetComState(); //新版本卫星协议4.0 ComBussiness.GetComStateFor4(); Thread.Sleep(3 * 1000); //等待3秒,等待卫星返回状态报 CheckComState(); Thread.Sleep(3 * 60 * 1000); } }
//召测的方法 void SendData() { Thread.Sleep(10 * 1000); while (true) { if (com.sp.IsOpen)//串口打开 { try { ComBussiness.SendCommand(com); } catch (Exception ex) { Service.ServiceControl.log.Error(DateTime.Now + ex.ToString()); } } Thread.Sleep(1000); } }
/// <summary> /// 发送卫星时间问询命令 /// </summary> public static void GetComTime() { foreach (ComServer item in Service.ServiceControl.com) { if (item.sp.IsOpen) { lock (item.sp) { //item.sp.WriteLine("$TINF,13:56:41.00, " + "\n");---返回的结果 byte[] vBuffer = ASCIIEncoding.ASCII.GetBytes("$TAPP,"); ComBussiness.XorSAT(ref vBuffer); item.sp.Write(vBuffer, 0, vBuffer.Length); Service.ServiceControl.LogInfoToTxt(Service.ServiceEnum.NFOINDEX.COM, "", vBuffer, "ASC"); Service.ServiceControl.LogInfoToTxt(Service.ServiceEnum.NFOINDEX.COM, "", vBuffer); } } } }
/// <summary> /// 发送卫星状态问询命令 /// </summary> public static void GetComState() { foreach (ComServer item in Service.ServiceControl.com) { if (item.sp.IsOpen) { lock (item.sp) { byte[] vBuffer = ASCIIEncoding.ASCII.GetBytes("$QSTA,0,"); ComBussiness.XorSAT(ref vBuffer); item.sp.Write(vBuffer, 0, vBuffer.Length); //byte[] enter=ASCIIEncoding.ASCII.GetBytes(item.sp.NewLine); //加回车 //Array.Resize(ref vBuffer, vBuffer.Length + enter.Length ); //Array.Copy(enter, 0, vBuffer, vBuffer.Length - enter.Length, enter.Length); Service.ServiceControl.LogInfoToTxt(Service.ServiceEnum.NFOINDEX.COM, "", vBuffer, "ASC"); Service.ServiceControl.LogInfoToTxt(Service.ServiceEnum.NFOINDEX.COM, "", vBuffer); } } } }
/// <summary> /// 从COM口读取数据分包,并处理状态时间等数据报 /// </summary> /// <param name="CS"></param> /// <param name="data"></param> /// <returns></returns> public static List <ComReceivedData> Subpackage(ComServer CS, byte[] data) { List <ComReceivedData> Lcrd = new List <ComReceivedData>(); string str = System.Text.Encoding.ASCII.GetString(data); string[] Temp_Strs = str.Split(new string[] { CS.sp.NewLine }, StringSplitOptions.None); bool IsCOUT = false; foreach (var item in Temp_Strs) { if (item.Length > 0) { string[] temp = item.Split(new string[] { "," }, StringSplitOptions.None); if (temp[0] == "$COUT" && temp.Length >= 10) { int dataLen = 0; if (int.TryParse(temp[7], out dataLen) && dataLen == temp[8].Length) { ComReceivedData crd = new ComReceivedData(); crd.SATELLITE = temp[4]; //从原始数据报里copy报文 byte[] vBuffer = new byte[dataLen]; Array.Copy(data, data.Length - dataLen - 4, vBuffer, 0, dataLen); crd.Data = vBuffer; Lcrd.Add(crd); } IsCOUT = true; //反馈给界面信息 ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, "接收卫星", "接收数据" + temp[0] + "," + temp[1] + "," + temp[2] + "," + temp[3] + "," + temp[4] + "," + temp[5] + "," + temp[6] + "," + temp[7], new byte[] { }, ServiceEnum.EnCoderType.HEX, ServiceEnum.DataType.Text); } //校验状态 else if (temp[0] == "$CASS" && temp.Length >= 3) { int check = 0; string Explain = "[失败]"; if (int.TryParse(temp[1], out check)) { if (check == 1) { Explain = "[成功]"; } } //反馈给界面信息 ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, "接收卫星", "接收数据" + item, new byte[] {}, ServiceEnum.EnCoderType.HEX, ServiceEnum.DataType.Text); ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, "", "接收校验状态" + Explain, new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.Text); } //授时信息 else if (temp[0] == "$TINF" && temp.Length >= 3) { //反馈给界面信息 string Explain = "接收授时信息" + "[" + temp[1] + "]"; ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, "接收卫星", "接收数据" + item, new byte[] { }, ServiceEnum.EnCoderType.HEX, ServiceEnum.DataType.Text); ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, "", Explain, new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.Text); } //卫星状态 else if (temp[0] == "$TSTA" && temp.Length >= 15) { string Satellite = temp[8]; string Explain = " 通道1信号功率:" + temp[1] + "\n" + " 通道2信号功率:" + temp[2] + "\n" + " 通道1卫星波束:" + temp[3] + "\n" + " 通道2卫星波束:" + temp[4] + "\n" + " 响应波束:" + temp[5] + "\n" + " 信号抑制:" + (temp[6] == "0" ? "有" : "无") + "\n" + " 供电状态:" + (temp[7] == "0" ? "异常" : "正常"); int Power1 = 0; int Power2 = 0; int Beam1 = 0; int Beam2 = 0; int Response = 0; int Inhibition = 0; int PowerSupply = 0; if (int.TryParse(temp[1], out Power1)) { CS.CState.Power1 = Power1; } if (int.TryParse(temp[2], out Power2)) { CS.CState.Power2 = Power2; } if (int.TryParse(temp[3], out Beam1)) { CS.CState.Beam1 = Beam1; } if (int.TryParse(temp[4], out Beam2)) { CS.CState.Beam2 = Beam2; } if (int.TryParse(temp[5], out Response)) { CS.CState.Response = Response; } if (int.TryParse(temp[6], out Inhibition)) { CS.CState.Inhibition = Inhibition; } if (int.TryParse(temp[7], out PowerSupply)) { CS.CState.PowerSupply = PowerSupply; } CS.CState.DATATIME = DateTime.Now; ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, Satellite, "接收卫星" + item, new byte[] { }, ServiceEnum.EnCoderType.HEX, ServiceEnum.DataType.Text); ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, Satellite, "接收到状态信息\n" + Explain, new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.Text); } else { ServiceBussiness.WriteQUIM(ServiceEnum.NFOINDEX.COM.ToString(), CS.ServiceID, "", "接收异常数据" + item, new byte[] { }, Service.ServiceEnum.EnCoderType.HEX, Service.ServiceEnum.DataType.Text); } } } if (IsCOUT) //如果接收的数据报含有$COUT指令,回复$COSS { ComBussiness.Reply(CS); } return(Lcrd); }