private void sendDataAlarm(Object ob) { if (ob.GetType() == typeof(ConnBean)) { ConnBean bean = ob as ConnBean; if (bean != null && bean.Ip2 != null) { Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (bean.Ip2 == null || client == null) { LogUtil.instance.Log("有值为空" + (bean.Ip2 == null) + "-" + (client == null)); return; } alarmClients.TryGetValue(bean.Ip2, out client);//从Dictionary中读取报警灯Client if (client != null) { //Encoding.ASCII.GetBytes(); //byte[] byteMsgOn = { 0xFE, 05, 00, 00, 0xFF, 00, 0x98, 53 }; //byte[] byteMsgOff = { 0xFE, 05 ,00, 00, 00 , 00, 0xD9 ,0xC5 }; byte[] byteMsgOn = CommandUtil.WriteDO(Convert.ToInt16(bean.Addr), bean.Channel - 1, true);//第几个灯,下标从0开始 byte[] byteMsgOff = CommandUtil.WriteDO(Convert.ToInt16(bean.Addr), bean.Channel - 1, false); LogUtil.instance.Log("开始发送开灯指令:" + byteMsgOn.ToArray()); foreach (byte b in byteMsgOn) { Console.Write(b + " "); } client.Send(byteMsgOn); //254 5 0 1 255 0 201 245 Thread.Sleep(5000); //异常 毫秒数 LogUtil.instance.Log("开始发送关灯指令:" + byteMsgOff.ToArray()); foreach (byte b in byteMsgOff) { Console.Write(b + " "); } client.Send(byteMsgOff);//254 5 0 1 0 0 136 5 } else { LogUtil.instance.Log(bean.Ip + " 对应的报警IP: " + bean.Ip2 + " 未在线"); } } else { LogUtil.instance.Log("电子称 " + bean.Ip + " 对应的报警器 " + bean.Ip2 + "不存在"); } } }