How to port the GSM module with windows pc using c program for internet connectivity for IOT RRS feed

  • Question

  • Hi

    I have bought the gsm module here is the link https://shop.mikroe.com/gsm-4-click  

    I am trying to port the module with the windows pc directly using a c program for writing the AT commands and to read back its response  
    and also to activate the gprs connection and to make use of its internet data for IOT project to send and write the data to a server 
    Can anybody please suggest how to be done using a c program to open the port ,write and read the port 

    I have wrote a c program but the response is same as what we write into it , i couldn't able to read the response from the module I am including my code with this message please suggest a better way to do it

    	#include <Windows.h>
    	#include <stdio.h>
    	#include <stdlib.h>
    	#include <string.h>
    	void SerialPortInit();
    	void Send2Gsm(char *);
    	void DelayS(int);
    		HANDLE hComm;                         // Handle to the Serial port
    		char   ComPortName[] = "COM3"; // Name of the Serial port(May Change) to be opened,
    		BOOL   Status;
    		DWORD dwEventMask;                     // Event mask to trigger
    		char  TempChar;                        // Temperory Character
    		char  SerialBuffer[256];               // Buffer Containing Rxed Data
    	void main(void)
    		EscapeCommFunction(hComm, CLRDTR);
    		EscapeCommFunction(hComm, CLRRTS);
    	    EscapeCommFunction(hComm, SETDTR);
            EscapeCommFunction(hComm, SETRTS);
    		PurgeComm(hComm, PURGE_RXCLEAR);
    	    PurgeComm(hComm, PURGE_TXCLEAR);*/
    		CloseHandle(hComm);//Closing the Serial Port
    		printf("\n ==========================================\n");
    	void SerialPortInit()		
    		printf("\n\n +==========================================+");
    		printf("\n |  Serial Transmission (Win32 API)         |");
    		printf("\n +==========================================+\n");
    		/*----------------------------------- Opening the Serial Port --------------------------------------------*/
    		hComm = CreateFile( ComPortName,                       // Name of the Port to be Opened
    							GENERIC_READ | GENERIC_WRITE,      // Read/Write Access
    							0,                                 // No Sharing, ports cant be shared
    							NULL,                              // No Security
    							OPEN_EXISTING,                     // Open existing port only
    							0,                                 // Non Overlapped I/O
    							NULL);                             // Null for Comm Devices
    		if (hComm == INVALID_HANDLE_VALUE)
    			printf("\n   Error! - Port %s can't be opened", ComPortName);
    			printf("\n   Port %s Opened\n ", ComPortName);
    		/*------------------------------- Setting the Parameters for the SerialPort ------------------------------*/
    		DCB dcbSerialParams = { 0 };                        // Initializing DCB structure
    		dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    		Status = GetCommState(hComm, &dcbSerialParams);     //retreives  the current settings
    		if (Status == FALSE)
    			printf("\n   Error! in GetCommState()");
    		dcbSerialParams.BaudRate = CBR_115200;      // Setting BaudRate = 9600
    		dcbSerialParams.ByteSize = 8;             // Setting ByteSize = 8
    		dcbSerialParams.StopBits = ONESTOPBIT;    // Setting StopBits = 1
    		dcbSerialParams.Parity   = NOPARITY;      // Setting Parity = None 
    		/*dcbSerialParams.fOutxCtsFlow    = 0;
            dcbSerialParams.fOutxDsrFlow    = 0;
            dcbSerialParams.fDtrControl     = DTR_CONTROL_DISABLE;
            dcbSerialParams.fDsrSensitivity = 0;
            dcbSerialParams.fRtsControl     = RTS_CONTROL_DISABLE;
            dcbSerialParams.fOutX           = 0;
            dcbSerialParams.fInX            = 0;*/
    		Status = SetCommState(hComm, &dcbSerialParams);  //Configuring the port according to settings in DCB 
    		if (Status == FALSE)
    				printf("\n   Error! in Setting DCB Structure");
    				printf("\n   Setting DCB Structure Successfull\n");
    				printf("\n       Baudrate = %d", dcbSerialParams.BaudRate);
    				printf("\n       ByteSize = %d", dcbSerialParams.ByteSize);
    				printf("\n       StopBits = %d", dcbSerialParams.StopBits);
    				printf("\n       Parity   = %d", dcbSerialParams.Parity);
    		/*------------------------------------ Setting Timeouts --------------------------------------------------*/
    		COMMTIMEOUTS timeouts = { 0 };
    		timeouts.ReadIntervalTimeout         = 50;
    		timeouts.ReadTotalTimeoutConstant    = 50;
    		timeouts.ReadTotalTimeoutMultiplier  = 10;
    		timeouts.WriteTotalTimeoutConstant   = 50;
    		timeouts.WriteTotalTimeoutMultiplier = 10;
    		if (SetCommTimeouts(hComm, &timeouts) == FALSE)
    			printf("\n   Error! in Setting Time Outs");
    			printf("\n\n   Setting Serial Port Timeouts Successfull");
    		/*------------------------------------ Setting Receive Mask ----------------------------------------------*/
    		Status = SetCommMask(hComm, EV_RXCHAR); //Configure Windows to Monitor the serial device for Character Reception
    		if (Status == FALSE)
    			printf("\n\n    Error! in Setting CommMask");
    			printf("\n\n    Setting CommMask successfull");
    	void Send2Gsm(char *lpBuffer)
    		/*----------------------------- Writing a Character to Serial Port----------------------------------------*/
    		//char   lpBuffer[] = "AT+CGDCONT?\r\n";		       // lpBuffer should be  char or byte array, otherwise write wil fail
    		DWORD  dNoOFBytestoWrite;              // No of bytes to write into the port
    		DWORD  dNoOfBytesWritten = 0;          // No of bytes written to the port
    		DWORD NoBytesRead;                     // Bytes read by ReadFile()
    		int i = 0;
    		dNoOFBytestoWrite = sizeof(lpBuffer); // Calculating the no of bytes to write into the port
    		Status = WriteFile(hComm,               // Handle to the Serialport
    						   lpBuffer,            // Data to be written to the port 
    						   dNoOFBytestoWrite,   // No of bytes to write into the port
    						   &dNoOfBytesWritten,  // No of bytes written to the port
    		if (Status == TRUE)
    			printf("\n\n    %s - Written to %s", lpBuffer, ComPortName);
    			printf("\n\n   Error %d in Writing to Serial Port",GetLastError());
            /*------------------------------------ Setting WaitComm() Event   ----------------------------------------*/
    		printf("\n\n    Waiting for Data Reception");
    		Status = WaitCommEvent(hComm, &dwEventMask, NULL); //Wait for the character to be received
    		/*-------------------------- Program will Wait here till a Character is received ------------------------*/				
    		if (Status == FALSE)
    				printf("\n    Error! in Setting WaitCommEvent()");
    		else //If  WaitCommEvent()==True Read the RXed data using ReadFile();
    				printf("\n\n    Characters Received");
    						Status = ReadFile(hComm, &TempChar, sizeof(TempChar), &NoBytesRead, NULL);
    						SerialBuffer[i] = TempChar;
    				while (NoBytesRead > 0);
    				/*------------Printing the RXed String to Console----------------------*/
    				printf("\n\n    ");
    				int j =0;
    				for (j = 0; j < i-1; j++)		// j < i-1 to remove the dupliated last character
    					printf("%c", SerialBuffer[j]);
    	void DelayS(int secs)

    Please any body help us to resolve this



    • Moved by Hart Wang Tuesday, November 7, 2017 8:44 AM
    Tuesday, October 10, 2017 7:12 AM

All replies

  • Hi,

    Thank you for posting here.

    >>" I have wrote a c program but the response is same as what we write into it , i couldn't able to read the response from the module I am including my code with this message please suggest a better way to do it "

    As far as I know that the GSM device should has its configuration register. When you are reading data, did you set read module?

    I am not sure that the data has been readied by GSM device.  I think that if data has been readied, some register bit should be set 0 or 1.

    Here is about GSM demo, you can reference it.


    Best  Regards,


    Since your issue is related to GSM mode and IOT , I suggest that you can post the issue on Windows IoT . will move the case to off-topic forum.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Hart Wang Thursday, November 2, 2017 7:47 AM
    Thursday, October 12, 2017 2:06 AM