TCP Socket Using PIC

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

TCP Socket Using PIC

Postby Enamul » Mon Jul 30, 2012 12:58 pm

I will post that in three different sections...one for newbie to show client (pc) to server (pc) TCP socket establishment for data tunneling. Second using Hyperterminal and modem where PC will be client and will connect to a server but connection will be through modem GPRS....
Finally PIC will replace HPT and connected to modem will be client and will connect to a server..using GPRS

I have wavecom 1206B and wavecom 1306B and wavecom supreme 10 Modem with me and my post will be applicable for 1306B and supreme 10 but can be easily modified to 1206B...Also can be used in simcom modems if anyone interested..

I posted this in V4 forum as the program I will post will be using FC v4...

It's now in!
These users thanked the author Enamul for the post:
dazz (Mon Jul 30, 2012 8:33 pm)
Rating: 5.26%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: TCP Socket Using PIC

Postby Enamul » Mon Jul 30, 2012 2:12 pm

PC to PC TCP socket:
Step to do "Hello" and "Fine" from a PC(client) to PC(server) which is connected with internet using TCP-IP mode.

1. Add your any port number (well known port is 0-1023; so any port above 1023 for example 6666)to your router's port forwarding setting. for help you can see this video(for xp):
How to Forward a TCP/UDP Port on a Router ?http://www.youtube.com/watch?v=yJABo8fXeXg

you can find other by googling..

2. Now download terminal.exe (we need that later on as an alternative to HPT)http://www.ddrservice.net/download/Software/tools/Terminal/Terminal.exe.html and socket testhttp://sockettest.sourceforge.net/ from link given.

4. Now in socket test soft in "server" tab set your ip (used in port fwd setting in router) and port no (in our case 6666).

5. Press start listening button.

6. Find out your dynamic (public IP) using google "my ip". (say this ip as your "dynamic ip" appeared to out side world)

7. In client machine, run the same socket test soft in "client" tab set your ip (get from my ip in google) and same port no (in our case 6666).Press connect.
If the server already in listening mode, the connection will established between two pc and you will see in server pc one client is connected.

Now you can send and receive data in this channel...Enjoy your chat window.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: TCP Socket Using PIC

Postby Enamul » Fri Aug 03, 2012 2:39 pm

PC-MODEM to PC TCP Socket:

Step to do "Hello" and "Fine" from Wavecom supreme 10 modem connected PC to PC which is connected with internet using TCP-IP mode.

1. Forward the port as mentioned in last post..note your IP which might be fixed one if you are in a home network but may change each time you start your machine.

2. Now download terminal.exe and socket test from my first post.

4. Now in socket test soft in "server" tab set your ip (used in port fwd setting in router). and port no (in our case 6666).

5. Press start listening button.

6. Find out your dynamic (public IP) using google "my ip". (say this ip as your "dynamic ip" appeared to out side world)

7. Now call your service provider cost care of the SIM card you have used with wavecom supreme 10 and ask for your "apn" and "user name" and "password".

8. Now connect your Wavecom supreme 10 with terminal.exe (set your port setting...9600,8,N,1). Type the AT cmd listed below in your terminal.exe('>>' for tx and '<<' for rx ):

Code: Select all

>>AT+CFUN=0 (ENTER)    // Off modem functionality
<<OK
<<
<<ERROR
<<
<<+CGREG:0

>>AT+CFUN=1 (ENTER)    //Restart entire GSM stack and functionality
<<OK
<<
<<+CREG:0,1

please wait a while....

Code: Select all

>>AT+CREG?  (ENTER)    //Check network connection status
<<+CREG:0,1
<<
<<OK

>>AT+CGREG=1 (ENTER)    //Set GPRS network registration status
<<OK

>>AT+WOPEN=1 (ENTER)    //Enable TCP/IP stack
<<OK

>>AT+WIPCFG=1 (ENTER)    //Start TCP/IP stack IP handling
<<OK

>>AT+WIPBR=1,6 (ENTER)   //Set GPRS as bearer service
<<OK

>>AT+WIPBR=2,6,0,"your username"  (ENTER)   //Set User Name
<<OK

>>AT+WIPBR=2,6,1,"your password"  (ENTER)   //Set password
<<OK

>>AT+WIPBR=2,6,11,"your APN"     (ENTER)   //Set APN
<<OK

>>AT+WIPBR=4,6,0 (ENTER)    //Bring up wireless connection with GPRS

please wait here until it appears like bellow:

Code: Select all

<<OK

>>AT+WIPCREATE=2,1,"***your dynamic ip***",port No(6666 in this case)  (ENTER)  //Set up TCP connection


please wait here until it appears like bellow:

Code: Select all

<<OK
<<
<<+WIPREADY:2,1


you will see a new client with a public IP is connected to the server in socket.

Code: Select all

>>AT+WIPDATA=2,1,1    (ENTER)
<<CONNECT
> Hello


9. Now see in the socket test soft your msg received there.
10. you can type in msg field "Fine" and press send button. you will find in terminal.exe:

Code: Select all

<<Fine

11. Now you can use this method and establish tcp server on any other pc (must be internet connected and have port forwareded).
12.When you want to disconnect the connection, it's advisable to disconnect it from client end as it starts from there.You have to
press"+++" in terminal window without "ENTER"..it will change the connection from data mode to command mode..

Code: Select all

>>+++
<<OK

>>AT+WIPCLOSE=2,1    (ENTER)
<<OK

In server end you will see client connection is now closed!
13. enjoy

Although I have tried to clear all about AT commands but if any one want to look bit more details of general AT commands, you can find them by googling
or from the following link..
ftp://ftp.shtrih-m.ru/minipos/minipos/G ... manual.pdf

But the OPEN AT command list for IP is real problem, I have spoiled my one evening in finding the right one..
I am attching here to preserve it..

Thanks,
Enamul
Attachments
WIP_Open__AT_Commands__User__Guide_V2_02.pdf
(1.08 MiB) Downloaded 236 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: TCP Socket Using PIC

Postby Enamul » Sun Sep 16, 2012 5:01 pm

Introduction:
This is first time in the forum I am going to present the M2M communication principle using Wavecom modem. I will post same using SIMCOM900 modem later on. This project can help anyone to develop web based application of microcontroller which is very popular in the recent times. I have although tried to clear any possible limitation of the program for the reliability of the communication even though some experts could notice some limitation of the code. I will appreciate any positive or negative comment in the code to make it more robust..
Circuit Connection
EB006-00-07 board
EB005 LCD board
EB015 RS232 board
Modem: Wavecom Fastrack Supreme 10 or 20 connected to RS232 board
Server is in listening mode
Description
Initialization:
In the beginning of the code, the GSM module is initialized to TEXT sms format which is done by
AT+CMGF=1 command and looking for “OK” to be returned from GSM module.
Then to receive the message in the modem not in Modem SIM, the following command is used
AT+CNMI=2,2,0,0,0 and looking for “OK” to be returned.
If any of the above “OK” is not returned or both is not returned, command not accepted is returned and the program will remain in loop and try again after one second..
Network Status Checking:
Then the code tests the status of network i.e., whether the Modem is registered to network or not. Modem could be connected to network in two modes: Registered, Registered-Roaming. The status is tested using AT+CREG?

Code: Select all

if ( GSM_STRING_ARRAY[7] == '0' || GSM_STRING_ARRAY[7] == '1' && GSM_STRING_ARRAY[9] == '1' || GSM_STRING_ARRAY[9] == '5')
      retval = 1;

In registered mode, the above command should return +CREG: 0,1 or +CREG: 1,0 whereas in roaming mode it should return +CREG: 1,5 or +CREG: 0,5
That’s why in the code 7th and 9th byte of received data array are checked to confirm about registration status. If the Modem responses as above mentioned, the code will return 1. The program will loop until it gets 1 returned from the macro.
Send Command:
Command macro need to modify to accommodate return values..

Code: Select all

//0=ok
//253=CONNECT
//254=Error
//255=no reply
if ((GSM_STRING_ARRAY[0] == 'O') && (GSM_STRING_ARRAY[1] == 'K'))
   return 0;

   if ((GSM_STRING_ARRAY[0] == 'E') && (GSM_STRING_ARRAY[4] == 'R'))
   return 254;

   if ((GSM_STRING_ARRAY[0] == 'C') && (GSM_STRING_ARRAY[6] == 'T'))
   return 253;

This macro is to send AT commands directly to the modem to perform any task. Like following..
GPRS ON
The following command is used to turn on GPRS interface in modem and as this command follows a <cr> after the command that’s why bSendCR should be 1. If bSendCR = 1 or 0 means whether there is any <cr> is going to add at the end of command or not.
AT+CGREG=1 <cr>
The program will wait in the loop till the command returns “OK”.
TCP/IP ENABLE
The following command will enable TCP/IP
AT+WOPEN=1 <cr>
The program will wait in the loop till the command returns “OK”.
START TCP STACK
The following command will start TCP stack
AT+WIPCFG=1<cr>
The program will wait in the loop till the command returns “OK”. If you repeat this command without restarting or powering up modem, you will get “ERROR” message from modem. That’s why “ERROR” in this case is not a problem rather an indication that you have tried more than once. I have modified the original send command macro to show this “ERROR” return.
OPEN GPRS BEARER
The following command will start TCP stack
AT+WIPBR=1,6 <cr>
The program will wait in the loop till the command returns “OK”. If you repeat this command without restarting or powering up modem, you will also get “ERROR” message from modem.
TCP CLIENT
Now the important bit for this program which is to create TCP client and I have to make custom code for that. The custom macro is added in PIC_GSM.c so can be accessed in flowcode. This enables to create TCP server/client. This is as follows..

Code: Select all

char GPRSTcpClientServer(char SrCt)
{
/*Macro_GPRSTcpClientServer_Start*/

//0 = OK
//254 = Error
//255 = No Reply

   char retval = 0;
   char idx;
      
   GSM_Send_Byte('A');
   GSM_Send_Byte('T');
   GSM_Send_Byte('+');
   GSM_Send_Byte('W');
   GSM_Send_Byte('I');
   GSM_Send_Byte('P');
   GSM_Send_Byte('B');
   GSM_Send_Byte('R');
   GSM_Send_Byte('=');
   GSM_Send_Byte('4');
   GSM_Send_Byte(',');
   GSM_Send_Byte('6');
   GSM_Send_Byte(',');
   
    if (SrCt)
        GSM_Send_Byte('1');     //send 1
   else if (SrCt==0)
   GSM_Send_Byte('0');     //send 0

   GSM_Send_Byte(GSM_CHAR_CR);
   
   while ( StringReceive()==0);
         
   if ((GSM_STRING_ARRAY[0] == 'E') && (GSM_STRING_ARRAY[4] == 'R'))
      return 254;
   
   if ((GSM_STRING_ARRAY[0] == 'O') && (GSM_STRING_ARRAY[1] == 'K'))
      retval = 0;
   
   StringReceive();
   StringReceive();
   Wdt_Delay_Ms(100);

   return retval;

/*Macro_GPRSTcpClientServer_End*/
}

Here we are waiting for response from modem and that's why we can't use normal command macro as that will return 255 always.
TCP Socket Create
This is also required to be custom made as IP and PORT number both needed to be string and IP needed to be send to modem in " " so I have modified msg send macro to make a new one which will wait for modem to respond. The code is following..

Code: Select all

char TCPSocketInfo (char* ip, char* port)
{
/*Macro_TCPSocketInfo_Start*/

//0=ok
//254=Error
//255=no reply

   char idx;
   char retval=0;

   GSM_Send_Byte('A');
   GSM_Send_Byte('T');
   GSM_Send_Byte('+');
   GSM_Send_Byte('W');
   GSM_Send_Byte('I');
   GSM_Send_Byte('P');
   GSM_Send_Byte('C');
   GSM_Send_Byte('R');
   GSM_Send_Byte('E');
   GSM_Send_Byte('A');
   GSM_Send_Byte('T');
   GSM_Send_Byte('E');
   GSM_Send_Byte('=');
   GSM_Send_Byte('2');
   GSM_Send_Byte(',');
   GSM_Send_Byte('1');
   GSM_Send_Byte(',');
   GSM_Send_Byte('"');

   for (idx=0; idx<MSZ_ip; idx++)
   {
      if (ip[idx] == 0)
         break;

      GSM_Send_Byte(ip[idx]);
   }

   GSM_Send_Byte('"');
   GSM_Send_Byte(',');
   
   for (idx=0; idx<MSZ_port; idx++)
   {
      if (port[idx] == 0)
         break;

      GSM_Send_Byte(port[idx]);
   }

   GSM_Send_Byte(GSM_CHAR_CR);
      
   while ( StringReceive()==0);
   
      
   if ((GSM_STRING_ARRAY[0] == 'E') && (GSM_STRING_ARRAY[4] == 'R'))
      return 254;

   if ((GSM_STRING_ARRAY[0] == 'O') && (GSM_STRING_ARRAY[1] == 'K'))
      retval = 0;
      
   StringReceive();
   StringReceive();
   Wdt_Delay_Ms(100);
      
   return retval;

/*Macro_TCPSocketInfo_End*/
}

CONNECTION
This command is done with built-in macro send command as this one executes immediately and for this command specially I have added

Code: Select all

if ((GSM_STRING_ARRAY[0] == 'C') && (GSM_STRING_ARRAY[6] == 'T'))
   return 253;

in the original send command macro as this command returns "CONNECT" if connection established.
In this command execution loop, I have tested server connection as well. If server is not in listening mode in that an "ERROR" will be returned by the modem and using this info controller will try twice to connect with server if still can't establish connection..Controller will send a "Server Connection Fail" sms to a pre-defined mobile number which can be assigned in the beginning of the code.
DATA SENDING
This is done with SEND Command macro. Here controller will keep modem waiting for server acknowledgement. If server send "OK", in that case controller will assume data has been sent to server. So, to keep modem in data receiving mode I have made a custom data receive code..

Code: Select all

void CheckForData (char* FCR_RETVAL, char FCR_RETVAL_SIZE)
{
/*Macro_CheckForData_Start*/

   char idx = 0;
   
   while ( StringReceive()==0);

   while ((idx < FCR_RETVAL_SIZE) && (GSM_STRING_ARRAY[idx] > 0))
   {
      FCR_RETVAL[idx] = GSM_STRING_ARRAY[idx];
      idx = idx + 1;
   }
   if (idx < FCR_RETVAL_SIZE)
      FCR_RETVAL[idx] = 0;         //String termination


/*Macro_CheckForData_End*/
}

The code I have attached here will create TCP client connection every 30 seconds and I put here a connection counter which will count how many successful connection has made with server to verify the reliability of communication.
DISCONNECTING
Before disconnecting from server, modem need to be taken to command mode from data mode which is done by "+++" command and executed using send command macro.
TCP CLOSE
To disconnect from server AT+WIPCLOSE=2,1 command is executed in send command macro.
Hope everyone will enjoy this.. :D
Attachments
TCP.jpg
TCP.jpg (90.78 KiB) Viewed 4096 times
PIC_GSM.c
(26.65 KiB) Downloaded 153 times
TCP_PIC16f877a.fcf
using FCv4.5
(24.33 KiB) Downloaded 196 times
These users thanked the author Enamul for the post (total 2):
Benj (Tue Sep 18, 2012 11:06 am) • hyperion007 (Thu Feb 06, 2014 2:08 pm)
Rating: 10.53%
 
Enamul
University of Nottingham
enamul4mm@gmail.com