Serial communication not working. Readfile goes wrong - c

After hours of browsing and reading, I still can't figure out why my code isn't working. I saw similar code snippets on different websites, but I can't seem to get it to work. The writing part is working, but the reading goes wrong. Every 'real character' is followed by three null terminators. Writing a string of 19 characters works and the FPGA I am using gives the correct data on the display. The FPGA should reverse the input and send this pack to the serial port. In the Hyperterminal this is working without any problem.
Can someone maybe point me on my mistake and tell me what I am doing wrong?
Thanks in advance =)
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <commdlg.h>
//#include <windef.h>
#define BUFFERLENGTH 19
void writeToSerial(char *line, HANDLE hSerial, DWORD dwBytesWritten);
void printBuffer(char * buffRead, DWORD dwBytesRead);
int main(){
HANDLE hSerial;
COMMTIMEOUTS timeouts;
COMMCONFIG dcbSerialParams;
char *line, *buffWrite, *buffRead;
DWORD dwBytesWritten, dwBytesRead;
/* Create a handle to the serial port */
hSerial = CreateFile("COM3",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
/* Check if the handle is valid */
if(hSerial == INVALID_HANDLE_VALUE){
if(GetLastError() == ERROR_FILE_NOT_FOUND){
printf("Serial port does not exist \n");
}else{
printf("Port occupied. Please close terminals!\n");
}
}else{
printf("Handle created\n");
/* Check the state of the comm port */
if(!GetCommState(hSerial, &dcbSerialParams.dcb)){
printf("Error getting state \n");
}else{
printf("Port available\n");
/* Configure the settings of the port */
dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);
/* Basic settings */
dcbSerialParams.dcb.BaudRate = CBR_57600;
dcbSerialParams.dcb.ByteSize = 8;
dcbSerialParams.dcb.StopBits = ONESTOPBIT;
dcbSerialParams.dcb.Parity = NOPARITY;
/* Misc settings */
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;
/* Apply the settings */
if(!SetCommState(hSerial, &dcbSerialParams.dcb)){
printf("Error setting serial port state \n");
}else{
printf("Settings applied\n");
GetCommTimeouts(hSerial,&timeouts);
//COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier= 10;
if(!SetCommTimeouts(hSerial, &timeouts)){
printf("Error setting port state \n");
}else{
/* Ready for communication */
line = "Something else\r";
//****************Write Operation*********************//
writeToSerial(line, hSerial, dwBytesWritten);
//***************Read Operation******************//
if(ReadFile(hSerial, buffRead, BUFFERLENGTH, &dwBytesRead, NULL)){
printBuffer(buffRead, dwBytesRead);
}
}
}
}
}
CloseHandle(hSerial);
system("PAUSE");
return 0;
}
void printBuffer(char * buffRead, DWORD dwBytesRead){
int j;
for(j = 0; j < dwBytesRead; j++){
if(buffRead[j] != '\0'){
printf("%d: %c\n", j, buffRead[j]);
}
}
}
void writeToSerial(char *line, HANDLE hSerial, DWORD dwBytesWritten){
WriteFile(hSerial, line, 19, &dwBytesWritten,NULL);
if(dwBytesWritten){
printf("Writing success, you wrote '%s'\n", line);
}else{
printf("Writing went wrong =[\n");
}
}

In this line:
if(ReadFile(hSerial, buffRead, BUFFERLENGTH, &dwBytesRead, NULL))
the buffRead parameter is an uninitialised pointer. Change the declaration to:
char *line, *buffWrite, buffRead [BUFFERLENGTH+1];

Hyper terminal probably doesn't display null characters so it's possible that your fpga is actually sending them.
You could try testing it with Br#y Terminal in hex mode, or looking at the line with an oscilloscope.

Related

Reading from serial port in C, having problems with ReadFile maybe?

Well I've been at this program for maybe four hours. I've been trying to create a little program to read what my arduino Uno is spitting out through the serial port.
What I find odd is that the program works only AFTER I've launched the arduino IDE's built in serial monitor. Perhaps this is an issue with initializing the port correctly?
If someone could help me out it'd be much appreciated. The program seems to hang during ReadFile, so maybe there's an issue with permissions...
#include Windows.h>
#include stdio.h>
#include tchar.h>//Removed to allow for stackoverflow format
void printCommState(DCB d);
int main() {
DCB dcb = { 0 };
HANDLE hPort;
BOOL success;
TCHAR *commPort = TEXT("COM3");
char buffer[40] = { 0 };
DWORD dwBytesRead = 0;
DWORD dwBytesWrite = 0;
int l;
/*COMMTIMEOUTS cTimeOut;
cTimeOut.ReadIntervalTimeout = 50;
cTimeOut.ReadTotalTimeoutConstant = 50;
cTimeOut.ReadTotalTimeoutMultiplier = 10;
cTimeOut.WriteTotalTimeoutConstant = 50;
cTimeOut.WriteTotalTimeoutMultiplier = 10;*/
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = CBR_9600;//Found on microsofts website
dcb.ByteSize = DATABITS_8;// standardized number?
dcb.Parity = NOPARITY;// found in comp management
dcb.StopBits = 1;
/*dcb.fBinary = 1;
dcb.fDtrControl = 1;
dcb.fTXContinueOnXoff = 1;
dcb.fRtsControl = 1;
dcb.XonLim = 2048;
dcb.XoffLim = 512;
dcb.XoffChar = 2;*/
//dcb.fDtrControl = DTR_CONTROL_DISABLE;//maybe unnecessary?
printCommState(dcb);
hPort = CreateFile(commPort, // This comm port is defined by TCHAR so that we can use TEXT() LPFILENAME
GENERIC_READ | GENERIC_WRITE,//DesiredAccess
0,//dwShareMode
NULL,//LPSecurity
CREATE_NEW| OPEN_EXISTING,//dwCreationDisposition
0,//Flags and attributes
NULL);//hTemplateFile
if (hPort == INVALID_HANDLE_VALUE) {
printf("CreateFile failed with the error %d.\n", GetLastError());
scanf_s("%d", &l);
return 1;
}
success = GetCommState(hPort, &dcb);
if (!success) {
printf("GetCommState failed with the error %d.\n ", GetLastError());
scanf_s("%d", &l);
return 2;
}
success = SetCommState(hPort, &dcb);
if (!success) {
printf("SetCommState failed with error %d.\n", GetLastError());
scanf_s("%d", &l);
return 3;
}
/*TIME TO READ STUFF*/
while (GetCommState(hPort, &dcb)) {
printf("We're in the while statement\n");
//+=+=+=+=+=+POSSIBLE PROBLEM?
if (ReadFile(hPort, buffer, 39, &dwBytesRead, NULL)) {
//hFile,lpBuffer,NumberofBytesToRead,LPnumberofbytestoread,lpOverlapped
printf("We're in the ifReadFile Statement!\n");
for (int j = 0; j < sizeof(buffer); j++) {
printf("in the for loop!\n");
printf("%c", buffer[j]);
}
printf("\n");
}
if (!ReadFile(hPort, &buffer, 39, &dwBytesRead, NULL)) {
printf("Error with ReadFile %d\n.", GetLastError());
}
}
scanf_s("%d", &l);
CloseHandle(hPort);
return 0;
}
void printCommState(DCB d) {
printf("\nBaudRate: %d\tByteSize: %d\tParity: %d\tStopBits: %d\n",
d.BaudRate,
d.ByteSize,
d.Parity,
d.StopBits);
}
The issue lies in the GetCommState line of the code
GetCommState code function
Here we have it setting the DCB to the settings returned by that function, so by adding another DCB (dcbRecieving, for instance as an example ) just as an error-catching device and having GetCommState point to that other DCB, we can keep our error catching DCB and set our original to the other one, or set the new parameters after the if statement.
Anyways, thanks a ton!

UART using C in Windows

I'm trying to establish a UART interface between an external Microcontroller and Windows using C.
I'm using the following code to set up the UART Parameters and then send in a character to the designated COM port.
I'm successful in sending a character. But How do I receive one back ? The code is as follows:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
HANDLE hSerial;
int main()
{
// OPEN SERIAL PORT AND SET INITAL UART PARAMETERS
//=================================================
DCB dcbSerialParams = {0}; COMMTIMEOUTS timeouts = {0};
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile("\\\\.\\COM3", GENERIC_READ|GENERIC_WRITE, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE){fprintf(stderr, "Error\n");return 1;}
else {fprintf(stderr, "OK\n");}
// Set device parameters (115200 baud, 1 start bit, 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error getting device state\n");CloseHandle(hSerial);return 1;}
dcbSerialParams.BaudRate = CBR_57600; dcbSerialParams.ByteSize = 8; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0){fprintf(stderr, "Error setting device parameters\n");CloseHandle(hSerial);return 1;}
// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50; timeouts.ReadTotalTimeoutConstant = 50; timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50; timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0){fprintf(stderr, "Error setting timeouts\n"); CloseHandle(hSerial); return 1;}
// SETUP AND SEND DATA FROM UART
//==============================
int VarNum=8;
char str[15];
sprintf(str,"%ld",VarNum);
DWORD bytes_written, total_bytes_written = 0;
fprintf(stderr, "Sending bytes...");
if(!WriteFile(hSerial,str, strlen(str), &bytes_written, NULL))
{
fprintf(stderr, "Error\n");
CloseHandle(hSerial);
return 1;
}
fprintf(stderr, "%d bytes written\n", bytes_written);
// CLOSE SERIAL PORT AND EXIT MAIN FUNCTION
//=========================================
fprintf(stderr, "Closing serial port...");
if (CloseHandle(hSerial) == 0){fprintf(stderr, "Error\n"); return 1;}
fprintf(stderr, "OK\n");return 0;
}
You can use ReadFile() for that:
BOOL bOk = ReadFile(hSerial, buffer, sizeof(buffer) - 1, &bytesRead, NULL);
if (bOk && (bytesRead > 0)) {
buffer[bytesRead] = '\0';
}
When reading from a serial port, ReadFile() should block until there is more data, or a timeout occurs. (this should be done in a seperate thread (in a loop), or maybe by using ReadFileEx() for asynchronous operation).
When reading from a communications device, the behavior of ReadFile is
governed by the current communication time-outs as set and retrieved
using the SetCommTimeouts and GetCommTimeouts functions. Unpredictable
results can occur if you fail to set the time-out values.
Also checkout SetCommState() and PurgeComm().

writing and reading data to and from a serial port (to an intel galileo) windows c

I'm building a cnc machine for a school project, it consists of an Intel galileo with a shield that controls stepper motors, this is controlled by a program on a windows machine(windows 7), what the program basically does is read a text file containing gcode and then sends it line by line to the Galileo, the Galileo then takes that line breaks it down into coordinates and instructions, moves the spindle to where it needs to go and when the instruction is finished it sends a message back to the computer through serial telling it that its finished, the computer then sends the next line of code and the process is repeated.
so far I can read the gcode file and send it line by line (using a keypress to send each line) to the galileo and everything works fine. my problem is reading from the galileo I have tried a few methods with no joy. this is what I think is closest to what I should be doing. I wont post the whole source because I think it will be just to much to read through, ive posted the main() which has all the setup functions and the function that is supposed to read the serial.
/************************************************************************************************************
application for controling galileo or arduino cnc machine
by brendan scullion
10/11/2014
**********************************************************************************************************/
#include <string.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <ctype.h>
#include <conio.h>
#include "functions.h"
int main()
{
system("COLOR 1F");
// Declare variables and structures
unsigned char text_to_send[MAX_PATH];
unsigned char digits[MAX_PATH];
int baudrate = 19200;
int dev_num = 50;
char dev_name[MAX_PATH];
HANDLE hSerial;
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
printf("Searching serial ports...\n");
while(dev_num >= 0)
{
printf("\r ");
printf("\rTrying COM%d...", dev_num);
sprintf(dev_name, "\\\\.\\COM%d", dev_num);
hSerial = CreateFile(
dev_name,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (hSerial == INVALID_HANDLE_VALUE) dev_num--;
else break;
}
if (dev_num < 0)
{
printf("No serial port available\n");
return 1;
}
printf("OK\n");
// Set device parameters (38400 baud, 1 start bit,
// 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
printf("Error getting device state\n");
CloseHandle(hSerial);
return 1;
}
//dcbSerialParams.BaudRate = CBR_38400;
dcbSerialParams.BaudRate = baudrate;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
printf("Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}
// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutConstant = 1;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
printf("Error setting timeouts\n");
CloseHandle(hSerial);
return 1;
}
char *cmd = NULL;
char *para1 = NULL;
char *para2 = NULL;
char *para3 = NULL;
char comPort[10];
float baudRate;
int keepGoing = 1;
unsigned char message[MAX_STRING_LENGHT];
//*********************************************************************************************************************
char cmdLine[200];
heading();
while(keepGoing == 1)
{
printf(">>");
gets(cmdLine);
cmd = strtok(cmdLine, " ");
if(cmd!=false)
{
if(cmd != NULL)
{
para1 = strtok(NULL, " ");
}
else if(para1 != NULL)
{
para2 = strtok(NULL, " ");
}
else if(para2 != NULL)
{
para3 = strtok(NULL, " ");
}
if(strcmp(cmd, "help")== 0)
{
help();
}
else if(strcmp(cmd, "comset")== 0)
{
setupComs(comPort, baudRate);
}
else if(strcmp(cmd, "getg")== 0)
{
getgcode(hSerial,text_to_send,dev_name);
}
else if(strcmp(cmd, "readserial")== 0)
{
read_serial(hSerial, message, dev_name);
}
else if(strcmp(cmd, "offset")==0)
{
getOffset(hSerial, text_to_send, dev_name);
}
else if(strcmp(cmd, "setup") == 0)
{
setup(hSerial, text_to_send, dev_name);
}
else if(strcmp(cmd, "exit") == 0)
{
keepGoing = 0;
}
else
{
printf("Unknown command!\n");
}
printf("\n");
}
}
// Close serial port
printf("Closing serial port...");
if (CloseHandle(hSerial) == 0)
{
printf("Error\n");
return 1;
}
printf("OK\n");
return 0;
}
and the function for reading
void read_serial(HANDLE hComm, HANDLE screen, char *message, char *devName )
{
char buffer[MAX_STRING_LENGHT];
unsigned char ch;
DWORD bytes_recieved = MAX_STRING_LENGHT, written = 0;
strcpy(buffer,""); //empty buffer
strcpy(buffer,"");
while(buffer!=NULL){ // wait untill serail message revieved
ReadFile(hComm, &buffer,sizeof(buffer), // read serial
&bytes_recieved, NULL );
if(bytes_recieved){ // if something to read
WriteFile(screen, buffer, bytes_recieved, &written, NULL);
strcpy(message, buffer);
printf("%s", message);
if(kbhit()){
ch = getch();
if(ch== 'q')
break;
}
}
}
}
i can post the entire source code if anybody wants to have a look at it
In read_serial, you can try replacing &buffer with buffer in the call to ReadFile. Name of a character array should be a pointer to the first element of the array.
What i would do is i would frame every message that goes from and to the Galileo board in a simple and useful protocol. For example i would use STX and ETX to frame and send messages from the Windows 7 via serial to the board, use ACK NAK to acknowledge the received packet and use SYN for signalling end of execution of a line on Galileo.
With the above framing you can construct receive and send functions where you can wait for specific characters (ETX,STX,SYN,ACK,etc) and exit once you have a full frame or an control character.

WriteFile doesnt work without debug

I am trying to send arguments to my arduino. so i made this code:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
HANDLE serialPortHandler;
char *comPort[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
char comPortName[5] = "COM";
int i = 1;
int openPort(char *name){
serialPortHandler = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(serialPortHandler == INVALID_HANDLE_VALUE){
return -1;
}else{
DCB dcb;
FillMemory(&dcb, sizeof(dcb), 0);
if (!GetCommState(serialPortHandler, &dcb)){
return -1;
}
dcb.BaudRate = CBR_9600 ;
if (!SetCommState(serialPortHandler, &dcb)){
return -1;
}
}
return 1;
}
int writePort(char *lpBuf,DWORD dwToWrite){
DWORD dwWritten;
if(WriteFile(serialPortHandler, lpBuf, dwToWrite, &dwWritten, NULL)){
while(dwWritten < dwToWrite);
printf("User: %s, %d", lpBuf, dwWritten);
return 0;
}else{
printf("Error");
CloseHandle(serialPortHandler);
return 1;
}
return 0;
}
int main(int argc, char *argv[]){
if(argc != 2)
return 1;
strcat(comPortName, comPort[0]);
while(openPort(comPortName) < 0 && i < sizeof(comPort) / sizeof(int)){
comPortName[3] = *comPort[i];
//printf("%s", comPortName);
i++;
Sleep(0);
}if(i >= sizeof(comPort) / sizeof(int)){
printf("Cannot Find Port");
scanf("%d");
return 1;
}
printf("Port %s Is Opened - BaudRate 9600\n", comPortName);
printf("Sent Frequency: %s\n", argv[1]);
writePort(argv[1], strlen(argv[1]));
}
But it only works if i run it on debug mode and wait for a few moments at WriteFile.If i run it from cmd it doesnt output to my arduino.
The WriteFile function has the ability to run asynchronously. Have you checked for this case?
From:https://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx
If the function fails, or is completing asynchronously, the return value is zero (FALSE). To get extended error information, call the GetLastError function.
The WriteFile function returns when one of the following conditions occur:
The number of bytes requested is written.
A read operation releases buffer space on the read end of the pipe (if the write was blocked). For more information, see the Pipes section.
An asynchronous handle is being used and the write is occurring asynchronously.
An error occurs.
(Meta: Can't blockquote a bullet list, I guess)
The problem was it takes time to initiate these two things:
1.SetCommState(serialPortHandler, &dcb)
so i added Sleep before and it fixed the problem.

Serial communication between GSM module and computer

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.
You need to call sleep before reading the result. 250 ms is more than enough

Resources