Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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);
                }

                 }
	    }
Posted
Updated 20-Jun-13 3:01am
v2
Comments
Ron Beyer 20-Jun-13 8:00am    
We can't see your screen, can you post the relevant parts of your code?
Rajesh Anuhya 20-Jun-13 8:02am    
You mean, you are sending your AT commands in Socket channel to device?, I am bit consufed, can you clarify
--RA
SNI 20-Jun-13 8:32am    
Yes I am sending AT commands to the device i.e. dongle in this case, which returns response to me.
Rajesh Anuhya 20-Jun-13 8:57am    
Post your comment on relative message reply, then i will get a notification.
--RA
Sunasara Imdadhusen 20-Jun-13 9:01am    
Please post your code inside question box or update your question instead of adding in comment box

1 solution

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
 
Share this answer
 
Comments
SNI 21-Jun-13 3:05am    
I took the sample application of C# and trying to send commands using that but its gets hanged in receive. Tried with Asysnc sample also.
Rajesh Anuhya 21-Jun-13 3:16am    
One more Try.., Open Hypertrminal(Accessiories->Communication->Hypertrminal) and Open a TCP socket and try to connect and send data fromt there(type you AT command in notepad and copy ->Paste in hyperterminal(use PastetoHost). wait for result. and tell me you are getting responce or not?
--RA
SNI 21-Jun-13 6:00am    
Hi I ma using Win7, i dont have this option enable. I used putty on this which connects to server and send cmdsand get proper response
Rajesh Anuhya 21-Jun-13 6:05am    
OK thats fine, what is the length of responce you are getting?
--RA
SNI 21-Jun-13 6:31am    
it depends on cmnd that we sent. For some cmnd it return 150 bytes and for others it could be more that 1024.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900