1
votes

I am working on a project having GSM module and it is also communicating serially with computer.

I have written a code in C on Visual Studio 2010 to transmit and receive data serially.

Code is given below:-

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h> 
#include <commdlg.h>
int nread,nwrite;


int main()
{

COMMTIMEOUTS timeouts;
COMMCONFIG dcbSerialParams;
char *words, *buffRead, *buffWrite;
DWORD dwBytesWritten,dwBytesRead;

HANDLE hSerial= CreateFile(L"COM1", GENERIC_READ | GENERIC_WRITE,
                       0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if ( hSerial == INVALID_HANDLE_VALUE) 
{ 
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
printf(" serial port does not exist \n");
} 
printf(" some other error occured. Inform user.\n");
}


//DCB    dcbSerialParams ;
//GetCommState( hSerial, &dcbSerialParams.dcb);
if (!GetCommState(hSerial, &dcbSerialParams.dcb)) 
{
printf("error getting state \n");
}

dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);


dcbSerialParams.dcb.BaudRate = CBR_9600;
dcbSerialParams.dcb.ByteSize = 8;
dcbSerialParams.dcb.StopBits = ONESTOPBIT;
dcbSerialParams.dcb.Parity = NOPARITY;

dcbSerialParams.dcb.fBinary = TRUE;
dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcbSerialParams.dcb.fOutxCtsFlow = FALSE;
dcbSerialParams.dcb.fOutxDsrFlow = FALSE;
dcbSerialParams.dcb.fDsrSensitivity= FALSE;
dcbSerialParams.dcb.fAbortOnError = TRUE;


if (!SetCommState(hSerial, &dcbSerialParams.dcb)) 
{
printf(" error setting serial port state \n");
}


GetCommTimeouts(hSerial,&timeouts);
//COMMTIMEOUTS timeouts = {0};

timeouts.ReadIntervalTimeout = 1000;
timeouts.ReadTotalTimeoutConstant = 1000;
timeouts.ReadTotalTimeoutMultiplier = 1000;
timeouts.WriteTotalTimeoutConstant = 1000;
timeouts.WriteTotalTimeoutMultiplier= 1000;

if(!SetCommTimeouts(hSerial, &timeouts)) 
{
printf("error setting port state \n");
}
while(1)
{
//****************Write Operation*********************//
char data_tx[]="at\r";
words=&data_tx[0];
nwrite = strlen(words);
buffWrite = words;
dwBytesWritten = 0;

if (!WriteFile(hSerial, buffWrite, nwrite, &dwBytesWritten, NULL)) 
{ 
printf("error writing to output buffer \n");
}
printf("Data written to write buffer is\n %s \n",buffWrite);


//***************Read Operation******************//
dwBytesRead = 0;
nread = strlen(words);
buffRead = new char[nread +1];
memset(buffRead, 0, nread+1);   // ensure that string will be null-terminated

if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL)) 
{
printf("error reading from input buffer \n");
}
printf("Data read from read buffer is \n %s \n ",buffRead);
Sleep(10000);
}
CloseHandle(hSerial);

}

When command "at\r" is transmitted from computer to GSM module it receive it should respond to it by sending OK.

Now I am getting only O. If I send "at\r" again I am getting K.

I have verified it on hyper terminal GSM module is working properly.If I send "at" and enter it responds with OK.

Also I have verified my C code on other program it is working properly.

I want to know why it is not working properly with GSM module.

1
Have you checked nread before reading and dwBytesRead after reading?Alex
Possibly this will help: ReadFile only reading one buffer character?Alex
@Alex Thanks for your help. I have verified my code by transmitting and receiving string of 5 characters between microcontroller and computer serially without a GSM module and code is working properlySaad Rafey

1 Answers

0
votes

You need to call sleep before reading the result. 250 ms is more than enough