// Use this for initialization void Start() { //registe bytes msg MsgManager.Instance.RegisterMsg(110, OnByteMsg); //you can registe many msg here //.... //registe protobuf msg MsgManager.Instance.RegisterMsg(typeof(TestProtobufStruct).FullName, OnProtobufMsg); //.... //connect(prefer host names) ClientTcp socket = new ClientTcp(); bool tempIsConnect = socket.Connect("www.google.com", 111); Debug.Log("是否连接成功: " + tempIsConnect); // send byte msg MsgByte tempMsg1 = new MsgByte(110); //110 is proto id tempMsg1.Write <int>(100); //write msg's body tempMsg1.Write("hello"); //write msg's body tempMsg1.Flush(); //send //send protobuf msg TestProtobufStruct testProtobufStruct = new TestProtobufStruct(); testProtobufStruct.x = 100; testProtobufStruct.y = "hello"; MsgProtobuf tempMsg2 = new MsgProtobuf(); tempMsg2.Write(testProtobufStruct); tempMsg2.Flush();//send }
// Read serial port public static void Read() { byte MsgByte; string MsgByteString = null; byte[] MsgPacketBuffer = new byte[65536]; // Reserve an array to store the max number of bytes possible in a frame while (ConsoleContinue) // Loop until user wants to stop the program. { try { MsgByte = (byte)SPort.ReadByte(); // Read a byte MsgByteString += MsgByte.ToString("X"); // Save the packet to a text file. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@MsgFileName, true)) { // Write the messsages to file, on hex or deciaml or both // file.Write(SalDecPacketString); file.Write(MsgByteString); } Console.WriteLine(MsgByteString); // Display the raw message to the console } //try catch (TimeoutException) { } } }