求助!!!我用C#语言,通过Socket同作为客户端的一个芯片进行通信,电脑作为服务器端。建立通信后,服务器如果给芯片发送一个指令“01 04 00 00 00 01 31 CA”,芯片收到后应该给我回复一个类似的十六进制数据。我非常确定的是:
1.客服端和服务器端的连接成功了。
2.我的指令发送到了客户端芯片
3.客户端没给我回复任何数据。
我用示波器看到了芯片收到了我的指令,但不知道为什么我发送的指令“01 04 00 00 00 01 31 CA”后面还跟着很多其他字符,可能正是因为这个原因,芯片认为我发送的命令不正确所以没给我回复。看看哪位高手能知道问题在哪里?为什么我的程序会除了发送我要求的指令外,还发送了很多其他字符?下面是代码,程序运行到skclient.Receive(byteTime, byteTime.Length, 0);之后就不动了,因为没有收到任何数据。
static void Main(string[] args)
        {
            try
            {
                IPEndPoint serverIP = new IPEndPoint(IPAddress.Any, 7777);
                Socket skserver = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                skserver.Bind(serverIP);
                skserver.Listen(100);                while (true)
                {
                    Socket skclient;
                    try
                    {
                        Console.Write("\nWaiting for connection……");
                        
                        skclient = skserver.Accept();
                        if (skclient.Connected)
                        {
                            Console.WriteLine("\nClient Connected!!  CLient IP {0}", skclient.RemoteEndPoint);
                        }
                        Console.WriteLine("Connection accepted.");
                       
                        byte[] byteTime = new byte[8];
                        byteTime[0] = 0x01;
                        byteTime[1] = 0x04;
                        byteTime[2] = 0x00;
                        byteTime[3] = 0x00;
                        byteTime[4] = 0x00;
                        byteTime[5] = 0x01;
                        byteTime[6] = 0x31;
                        byteTime[7] = 0xCA;
    
                        skclient.Send(byteTime, 8, SocketFlags.None);
                        byteTime = new byte[8];
                        skclient.Receive(byteTime, byteTime.Length, 0);
                        Console.WriteLine("收到:" + Encoding.ASCII.GetString(byteTime,0,byteTime.Length));
                        Console.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.ReadLine();//让程序暂停一下好看清结果
            }
        }