private void btnSend_Click(object sender, EventArgs e) { //发送测试 //led.SendMessage("192.168.1.181"); LEDSendPackage sp = new LEDSendPackage(LEDCommandType.Clear); sp.Content = "Test"; led.SendMessage("192.168.1.181", sp); }
/// <summary> /// 10ms延迟,GPRS 1分钟;串口 200ms /// </summary> /// <param name="ip"></param> /// <param name="package"></param> public void SendMessage(string ip, LEDSendPackage package) { IPAddress ipAddr = IPAddress.Parse(ip); TcpClient tcpClient = new TcpClient(); tcpClient.Connect(ipAddr, TCPPort); if (tcpClient.Connected) { NetworkStream netStream = tcpClient.GetStream(); BinaryWriter writer = new BinaryWriter(netStream); BinaryReader reader = new BinaryReader(netStream); //string sendMsg = "a034000100000000000000000019191800000000000000000000000000000000000000000000000000000000000000001f010055"; //byte[] sendBytes = HexStrToByteArray(sendMsg); byte[] sendBytes = package.GetPackage(); writer.Write(sendBytes); writer.Flush(); byte[] receiveBytes = reader.ReadBytes(52); LEDReceivePackage rPck = new LEDReceivePackage(receiveBytes); if (reader != null) { reader.Close(); } if (writer != null) { writer.Close(); } if (tcpClient != null) { tcpClient.Close(); } } }