1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include "ProjectSMS.h"
HANDLE hComport;
int ConnectWithModem(void) // Tested OK
{
char COM[]="COM";
char numberOfComport[2];
unsigned long result;
itoa(comportNumber,numberOfComport,10);
strcat(COM,numberOfComport);
hComport = CreateFile(COM,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
result = GetLastError();
if(hComport == INVALID_HANDLE_VALUE ||
result == ERROR_FILE_NOT_FOUND ||
result == ERROR_DEVICE_NOT_CONNECTED) {
printf("%lu",result);
printf("\nReturning from first if\n");
CloseHandle(hComport);
return 1;
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if(!GetCommState(hComport, &dcbSerialParams)) {
printf("\nReturning from second if\n");
CloseHandle(hComport);
return 1;
}
dcbSerialParams.BaudRate = CBR_9600; // This has to be programmed for user input.
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(!SetCommState(hComport, & dcbSerialParams)) {
printf("\nReturning from third if\n");
CloseHandle(hComport);
return 1;
}
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(!SetCommTimeouts(hComport, &timeouts)) {
printf("\nReturning from third if\n");
CloseHandle(hComport);
return 1;
}
return 0;
}
int DissconnectWithModem(void) //Tested OK
{
if(CloseHandle(hComport)) {
return 0;
}
else {
return 1;
}
}
int SendDataToModem(const char * data) //Tested OK
{
int lengthOfData = strlen(data);
DWORD dwBytesRead = 0;
if(!WriteFile(hComport,data,lengthOfData, &dwBytesRead,NULL)) {
CloseHandle(hComport);
return 1;
}
printf("LAST ERROR While Sending : %lu",GetLastError());
getch();
return 0;
}
int ReceiveDataFromComport(char *receivedData, int numberOfCharacter) //Tested OK but Confusing in size of the character
{
DWORD dwBytesRead = 0;
if(!ReadFile( hComport, receivedData, numberOfCharacter, &dwBytesRead,NULL)) {
CloseHandle(hComport);
return 1;
}
printf("LAST ERROR While Receiving : %lu",GetLastError());
getch();
return 0;
}
|