Unable to send AT commands to the COM3 port. plz help.
-
I have configured the COM3 (modem) port with the help of CreateFile() function. But when i m using the WriteFile() function to send the AT command strings to them modem, its not working ... i mean nothing is happening .... plz help... how to send a string to a port ... ?
Apurv
Does
WriteFile()
fail or succeed? Regards, --Perspx"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
-
Does
WriteFile()
fail or succeed? Regards, --Perspx"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
-
my code is as follows ...
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
char DataBuffer[] = "AT+FCLASS=8";
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
char Data2[]="ATDW2289759";DWORD dwBytesWritten = 0; DWORD dwBytesRead = 0; char ReadBuffer\[BUFFER\_SIZE\] = {0};
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.// SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}dcb.BaudRate = CBR\_2400; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
//WriteFile(hCom,"AT+FCLASS=8",sizeof("AT+FCLASS=8"),&len,0);
if( FALSE == WriteFile(hCom, // open file handle
DataBuffer + dwBytesWritten, // start of data to write
dwBytesToWrite - dwBytesWritten, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL)) // no overlapped structure
printf("Could not write %s to file (error %d)\n",DataBuffer, GetLastError());
else
_tprintf(TEXT("Wrote %d bytes successfully.\n"), dwBytesWritten);
if( FALSE == ReadFile(hCom, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead, -
my code is as follows ...
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
char DataBuffer[] = "AT+FCLASS=8";
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
char Data2[]="ATDW2289759";DWORD dwBytesWritten = 0; DWORD dwBytesRead = 0; char ReadBuffer\[BUFFER\_SIZE\] = {0};
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.// SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}dcb.BaudRate = CBR\_2400; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
//WriteFile(hCom,"AT+FCLASS=8",sizeof("AT+FCLASS=8"),&len,0);
if( FALSE == WriteFile(hCom, // open file handle
DataBuffer + dwBytesWritten, // start of data to write
dwBytesToWrite - dwBytesWritten, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL)) // no overlapped structure
printf("Could not write %s to file (error %d)\n",DataBuffer, GetLastError());
else
_tprintf(TEXT("Wrote %d bytes successfully.\n"), dwBytesWritten);
if( FALSE == ReadFile(hCom, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead, -
Probably you are communicating with some mobile or modem. Append \r\n at the end of every AT Command. Then only you will get the response from the device.
Regards, Sandip.
modified on Friday, August 29, 2008 2:32 PM
-
STILL no call is made by the modem, even though same commands are working well with HyperTerminal ... what shall i do ? :(
Apurv
-
you are reading as soon as you write try adding some sleep in between write and read. I hope it will help you. But don't forget to add "\r\n" may be you can try "\n\r" i am not sure about the exact order.
Regards, Sandip.
-
i have tried Sleep(1000*10); and Sleep(1000*20); the o/p of reading is the earlier command + OK (in the next line)
Apurv
-
OK is nothing but response from the modem. what is not working then for you???:confused:
Regards, Sandip.
-
my code is as follows ...
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM3");
char DataBuffer[] = "AT+FCLASS=8";
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
char Data2[]="ATDW2289759";DWORD dwBytesWritten = 0; DWORD dwBytesRead = 0; char ReadBuffer\[BUFFER\_SIZE\] = {0};
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.// SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}dcb.BaudRate = CBR\_2400; // set the baud rate dcb.ByteSize = 8; // data size, xmit, and rcv dcb.Parity = NOPARITY; // no parity bit dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
//WriteFile(hCom,"AT+FCLASS=8",sizeof("AT+FCLASS=8"),&len,0);
if( FALSE == WriteFile(hCom, // open file handle
DataBuffer + dwBytesWritten, // start of data to write
dwBytesToWrite - dwBytesWritten, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL)) // no overlapped structure
printf("Could not write %s to file (error %d)\n",DataBuffer, GetLastError());
else
_tprintf(TEXT("Wrote %d bytes successfully.\n"), dwBytesWritten);
if( FALSE == ReadFile(hCom, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead,See documentation for SetCommTimeouts. After that verify your AT command string. As debugging tool, I would turn on modem speaker if you have one. Also verify that COM3 is valid and unused by other application. " 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. For more information about communication time-outs, see COMMTIMEOUTS. "
-
See documentation for SetCommTimeouts. After that verify your AT command string. As debugging tool, I would turn on modem speaker if you have one. Also verify that COM3 is valid and unused by other application. " 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. For more information about communication time-outs, see COMMTIMEOUTS. "
-
Does this command work in HyperTerminal BTW:this link explains the AT commands might help you.. Modem commands[^]
Regards, Sandip.
-
SandipG :) wrote:
Does this command work in HyperTerminal
yes ... the number is getting dialed thro' HyperTerminal ... it is working perfectly there ...
Apurv
-
SandipG :) wrote:
Does this command work in HyperTerminal
yes ... the number is getting dialed thro' HyperTerminal ... it is working perfectly there ...
Apurv
-
in the HyperTerminal, when the command ATDW2289759 is given the response is VCON, but in my code the response is OK
Apurv
-
there is a while loop as follows :
while(1)
{
if(kbhit())
{
key=getch();
if(key=='x')
{
CloseHandle(hCom);
exit(0);
}
switch(key)
{
case '1':
// code to issue command ATDW2289759\r\n
}
}
}Apurv