/**************************************************************** File: modemtest.c Author: Peter Fleury Test modem communication using win32 This code demonstrates an extremely simple serial connection. Compiled using the freeware MinGW Compiler http://www.mingw.org/ Compile commands: gcc -Wall -c modemtest.c gcc modemtest.o -o modemtest *****************************************************************/ #include #include #include #include #include /* * function prototypes */ void ErrorHandler(char *message, DWORD error); int modemInit( char *comPort, int baudRate, HANDLE *comHandle ); int modemReadLine( HANDLE comHandle, char *data, int *dataLen ); int modemWriteMsg( HANDLE comHandle, char *data, int dataLen ); /************************************************************************ open comport, set baudrate and return handle to it ************************************************************************/ int modemInit( char *comPort, int baudRate, HANDLE *comHandle ) { BOOL success; DCB dcb; COMMTIMEOUTS timeouts; /* Open the comm port. Can open COM, LPT, or \\\\.\\TELNET */ *comHandle = CreateFile(comPort, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (*comHandle == INVALID_HANDLE_VALUE) ErrorHandler("In CreateFile", GetLastError()); /* Get the current settings of the COMM port */ success = GetCommState(*comHandle, &dcb); if (!success) ErrorHandler("In GetCommState",GetLastError()); /* Modify the baud rate, etc. */ dcb.BaudRate = baudRate; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; /* Apply the new comm port settings */ success = SetCommState(*comHandle, &dcb); if (!success) ErrorHandler("In SetCommState",GetLastError()); /* Change the ReadIntervalTimeout so that */ /* ReadFile will return immediately (MAXDWORD) or */ /* not return until it has data (0) */ timeouts.ReadIntervalTimeout = 0; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts( *comHandle, &timeouts ); return 0; } int modemReadLine( HANDLE comHandle, char *data, int *dataLen ) /* read terminated line from modem */ { BOOL success; DWORD numRead; char c; *dataLen = 0; do { success = ReadFile(comHandle, &c, 1, &numRead, 0); if (!success) ErrorHandler("In ReadFile", GetLastError()); *data++ = c; (*dataLen)++; }while( c != '\n' ); *data = '\0'; return 0; };/* modemReadLine */ int modemWriteMsg( HANDLE comHandle, char *data, int dataLen ) /* write data to modem */ { BOOL success; DWORD numWrite; success = WriteFile(comHandle, data, dataLen, &numWrite, 0); if (!success) ErrorHandler("modemWriteMsg:WriteFile", GetLastError()); return 0; };/* modemWriteMsg */ /* * module global variables */ void ErrorHandler(char *message, DWORD error) { fprintf(stderr,"Message=%s Errno=%u\n", message, (unsigned int)error ); ExitProcess(1); } int main( int argc, char **argv) { HANDLE comHandle; int retval; char str[1100]; retval = modemInit( "COM1", 9600, &comHandle ); /* Set the Data Terminal Ready line */ EscapeCommFunction(comHandle, SETDTR); /* Send an "at" command to the modem. Be sure to use \r rather than \n */ strcpy(str, "atz\r" ); modemWriteMsg(comHandle, str, strlen(str) ); modemReadLine( comHandle, str, &retval ); /* read echo */ /* expect OK */ modemReadLine( comHandle, str, &retval ); /* read one line from modem */ printf("Received: Len=%4d %s", retval, str ); /* and print the string received */ /* send "at &v" */ strcpy(str, "at&v\r" ); modemWriteMsg(comHandle, str, strlen(str) ); /* read answer from modem */ do { modemReadLine( comHandle, str, &retval ); /* read one line from modem */ printf("Received: Len=%4d %s", retval, str ); /* and print the string received */ } while ( !strstr(str,"OK")); // Clear the DTR line EscapeCommFunction(comHandle, CLRDTR); CloseHandle(comHandle); exit(0); }