/// <summary> /// 发送到设备 /// </summary> /// <param name="deviceID"></param> /// <param name="command"></param> /// <param name="stat"></param> /// <param name="powerStat"></param> /// <param name="otherinfo"></param> /// <returns></returns> public string SendMsgToClient(string deviceID, COMMAND command, STAT stat, POWERSTAT powerStat, string otherinfo = null) { DMClient c = clientTable[deviceID]; if (c != null) { DMProtocol dmProtocol = null; string msg = null; switch (command) { case COMMAND.ASK: dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, 0, 0, DateTime.Now.ToString("yyyyMMddHHmmss")); msg = dmProtocol.makeMessage(); break; case COMMAND.REMIND: string[] info = new string[1]; info[0] = otherinfo; dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, (int)stat, (int)powerStat, DateTime.Now.ToString("yyyyMMddHHmmss"), info); msg = dmProtocol.makeMessage(); break; case COMMAND.SETUP: dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, (int)stat, (int)powerStat, DateTime.Now.ToString("yyyyMMddHHmmss")); msg = dmProtocol.makeMessage(); break; case COMMAND.UPDATE: dmProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)command, deviceID, 0, 0, DateTime.Now.ToString("yyyyMMddHHmmss")); msg = dmProtocol.makeMessage(); break; } //try //{ // c.Send(msg); // return "Success"; //} //catch (Exception ex) //{ // Console.WriteLine(ex.Message); // return "Err:ex.Message"; //} return(c.SendAndReceive(msg)); } else { return(GetErrMessageToLIMS(deviceID, "Device is offline", (int)COMMAND.ERR_CONCET)); } }
public void SendReply(COMMAND command, DMClient dmclient) { DMProtocol dMProtocol; switch (command) { case COMMAND.REP_REGISTER: dMProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)COMMAND.REP_REGISTER, dmclient.DeviceID, (int)dmclient.Stat, (int)dmclient.Powerstat, DateTime.Now.ToString("yyyyMMddHHmmss")); dmclient.Send(dMProtocol.makeMessage()); break; case COMMAND.REP_READCARD: dMProtocol = new DMProtocol(HEADER.XXZL.ToString(), VERSION.V0100.ToString(), (int)COMMAND.REP_READCARD, dmclient.DeviceID, (int)dmclient.Stat, (int)dmclient.Powerstat, DateTime.Now.ToString("yyyyMMddHHmmss")); dmclient.Send(dMProtocol.makeMessage()); break; } }
public static void DeviceInformationProcessing(IAsyncResult ar) { StateObject stateObject = (StateObject)ar.AsyncState; Socket clientSocket = stateObject.workSocket; int count = 0; DMProtocol dmProtocol; count = clientSocket.EndReceive(ar); string msg = Encoding.UTF8.GetString(stateObject.buffer, 0, count); //Console.WriteLine(msg); //string dData = stateObject.stringBuilder.ToString(); string[] dataString = msg.Split(Comma); //头,版本,命令,ID,stat if (dataString.Count() < 4) { throw new Exception("Incomplete message"); } if (!dataString[0].Equals(LEMServer.TcpServer.Protocol.HEADER.SXZL.ToString())) { throw new Exception("The message does not conform to the agreement"); } try { dmProtocol = new DMProtocol(msg); //Console.WriteLine("Deal String Success : " + msg); } catch (Exception ex) { Console.WriteLine("Deal String failure : " + msg); throw ex; } //Console.WriteLine("Deal String : " + msg); COMMAND command = (COMMAND)int.Parse(dataString[2]); switch (command) { case COMMAND.REGISTER: //头,版本,命令,ID,devicestat,powerstat,port //0 1 2 3 4 5 6 // 0 1 2 3 4 5 6 7 //"SXZL,V0100,01,DM77712,3696,00," + DateTime.Now.ToString("yyyyMMddHHmmss") + ",9910"; //DeviceRegistration(dataString[3], dataString[1],int.Parse(dataString[4]), int.Parse(dataString[5]), ((IPEndPoint)stateObject.workSocket.RemoteEndPoint).Address.ToString(), int.Parse(dataString[7])); DeviceRegistration(dmProtocol.DeviceID, dmProtocol.VersionString, dmProtocol.StatInt, dmProtocol.PowerStatInt, ((IPEndPoint)stateObject.workSocket.RemoteEndPoint).Address.ToString(), dmProtocol.Port); //RepDeviceRegistration(dataString[2], dataString[1], int.Parse(dataString[4]), int.Parse(dataString[5]), clientSocket); RepDeviceRegistration(dmProtocol.DeviceID, dmProtocol.VersionString, dmProtocol.StatInt, dmProtocol.PowerStatInt, clientSocket); break; case COMMAND.READCARD: Readcard(dmProtocol.DeviceID, dmProtocol.VersionString, dmProtocol.StatInt, dmProtocol.PowerStatInt, dmProtocol.Card); Readcard(dmProtocol.DeviceID, dmProtocol.VersionString, dmProtocol.StatInt, dmProtocol.PowerStatInt, dmProtocol.Card); break; } Thread.Sleep(300); try { clientSocket.Close(); } catch (Exception ex) { // Console.WriteLine(ex.Message); } }