带有AT Cmds的C#套接字 [英] C# socket with AT Cmds

查看:74
本文介绍了带有AT Cmds的C#套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试将套接字连接到加密狗并向其发送AT命令。我能够连接到服务器并且还能够发送命令但是当我尝试从它接收respose时,连接被挂起。它甚至没有提出任何例外。与VC ++运行良好的相同代码。我使用了winsock的发送和接收API。有人可以告诉我可能是什么原因。

Hi,

I am trying to make socket connection to dongle and sending AT commands to that. I am able to connect to server and also able send the command but when I am trying to receive respose from it, connection gets hanged. It is not even raising any exception. The same code it running fine with VC++. I have used send and receive API of winsock. Can someone tell me what could be reason.

Encoding ASCII = Encoding.ASCII;
            string AtCmd = "AT%MEAS=\"2\"";
            Byte[] ByteGet = ASCII.GetBytes(AtCmd);
            Byte[] RecvBytes = new Byte[1024];
            String strRetPage = null;

            // IPAddress and IPEndPoint represent the endpoint that will
            //   receive the request.
            // Get the first IPAddress in the list using DNS.
            string server = "127.0.0.1";
            IPAddress hostadd = Dns.Resolve(server).AddressList[0];
            IPEndPoint EPhost = new IPEndPoint(hostadd, 9999);

            //Creates the Socket for sending data over TCP.
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);

            // Connects to the host using IPEndPoint.
            s.Connect(EPhost);
            if (!s.Connected)
            {
                strRetPage = "Unable to connect to host";
               // return strRetPage;
            }

            // Sends the GET text to the host.
            if (s.Send(ByteGet, ByteGet.Length, SocketFlags.None) == ByteGet.Length)
            {

                // Receives the page, looping until all bytes are received
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); ---> Here it gets stuck
                while (bytes > 0)
                {
                    bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None);
                    strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
                }

                 }
	    }

推荐答案





我建议,首先调试问题所在,下载任何免费的tcp通信程序。尝试从那里发送AT命令,检查你是否得到了响应。

第二个,尝试在您的应用程序中使用Async TCP套接字。



谢谢

--RA
Hi,

I am suggesting, first debug the problem where it is, download any free tcp communication program. try to send the AT commands from that, check either you are getting responce or not.
second one, try to use Async TCP socket in your application.

Thanks
--RA


这篇关于带有AT Cmds的C#套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆