|
Also, asserting on m_bReadMode==false indicates that the code
requires read mode - you specify GENERIC_WRITE.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
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
|
|
|
|
|
WriteFile() succeeds ....
Apurv
|
|
|
|
|
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,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hCom == INVALID_HANDLE_VALUE)
{
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
dcb.BaudRate = CBR_2400;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
_tprintf (TEXT("Serial port %s successfully reconfigured.\n"),pcCommPort);
if( FALSE == WriteFile(hCom,
DataBuffer + dwBytesWritten,
dwBytesToWrite - dwBytesWritten,
&dwBytesWritten,
NULL))
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, NULL) )
{
printf("Could not read from file (error %d)\n", GetLastError());
}
else
printf("%s",ReadBuffer);
dwBytesWritten = 0;
if( FALSE == WriteFile(hCom,
Data2 + dwBytesWritten,
(DWORD)sizeof(Data2) - dwBytesWritten,
&dwBytesWritten,
NULL))
printf("Could not write %s to file (error %d)\n",Data2, GetLastError());
else
_tprintf(TEXT("Wrote %d bytes successfully.\n"), dwBytesWritten);
if( FALSE == ReadFile(hCom, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead, NULL) )
{
printf("Could not read from file (error %d)\n", GetLastError());
}
else
printf("%s",ReadBuffer);
after sending the string to the port, i m reading the port, and every time same string is read !
Apurv
|
|
|
|
|
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???
Regards,
Sandip.
|
|
|
|
|
it is not dialing the number i provided ....
ATDW2289759\r\n
Apurv
|
|
|
|
|
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
|
|
|
|
|
What does your program do after read buffer??
Regards,
Sandip.
|
|
|
|
|
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':
}
}
}
Apurv
|
|
|
|
|
well... its done !
the problem was, i was sending the command as a complete string, the modem needed the command in the pieces ... i mean character-by-character ....
the problem is solved ...
thanx to all of u ....
Apurv
|
|
|
|
|
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.
"
|
|
|
|
|
THANX Vaclav_Sal
.... will check it soon ....
Apurv
|
|
|
|
|
how to set the timeouts ??
what should be the second parameter to SetCommTimeouts() function ?
Apurv
|
|
|
|
|
I have a function that uses a variable argument list, but the va_arg macro always throws an exception. Why can't I use CString objects as my arguments? What am I doing wrong? My code is below.
void COdb::WriteAttribute( CStdioFile& fileOutput,
const CString& strTagName,
const CString& strAttribute,
const CString& strValue, ... )
{
va_list vl;
va_start( vl, strValue );
CString strBuffer;
strBuffer.Format( " <%s %s=\"%s\"/>",
strTagName, strAttribute, strValue );
CString strOpAttr = va_arg( vl, CString );
}
|
|
|
|
|
Royce Fickling wrote: Why can't I use CString objects as my arguments?
Why don't you use LPCTSTR as the parameter types instead?
void COdb::WriteAttribute( CStdioFile& fileOutput, LPCTSTR strTagName, LPCTSTR strAttribute, LPCTSTR strValue, ... )
{
va_list vl;
va_start( vl, strValue );
CString strBuffer;
strBuffer.Format( " <%s %s=\"%s\"/>", strTagName, strAttribute, strValue);
LPCTSTR strOpAttr = va_arg( vl, LPCTSTR);
}
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Interesting.
I looked at this for a while. The problem is the "strValue"
argument. The offset of the strValue parameter is used to calculate
the offset of the start of the variable-length parameter list.
If you don't pass it by reference the function works fine.
I can't figure out why the reference doesn't work - the sizeof a reference
is the same as the sizeof a CString (4 bytes on my 32-bit build), so I would
expect the same result.
I guess references are passed on the stack differently.
I don't think this old C feature was meant to deal with C++ references anyway...
Regardless, passing strValue by value instead of by reference fixes it:
void COdb::WriteAttribute( CStdioFile& fileOutput,
const CString& strTagName,
const CString& strAttribute,
const CString strValue, ... )
...
If anyone wants to analyze this further, here's the macro definitions:
#define _ADDRESSOF(v) ( &reinterpret_cast<const char &>(v) )
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
#define _crt_va_start(ap,v) ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
#define _crt_va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define _crt_va_end(ap) ( ap = (va_list)0 )
*edit* Now that the caffeine has kicked in, I realize taking the address<br />
of a reference gives you the address of the object referenced, not the<br />
address of the reference. Yeah - those macros weren't meant to work<br />
with references
Mark
Mark Salsbery
Microsoft MVP - Visual C++
modified on Friday, August 29, 2008 2:09 PM
|
|
|
|
|
After converting the CString reference parameters to LPCTSTR parameters, it works, but only when the optional parameters are actually passed. When they are not passed, I still get garbage in the 1st optional parameter. So how do I determine when I have them and when I don't? I thought that if the optional arguments were not passed, the first call to va_arg would return a null, but that is not the case. I have posted my code below.
void COdb::WriteAttribute( CStdioFile& fileOutput,
LPCTSTR strTagName,
LPCTSTR strAttribute,
LPCTSTR strValue, ... )
{
va_list vl;
va_start( vl, strValue );
CString strBuffer;
strBuffer.Format( " <%s %s=\"%s\"/>",
strTagName, strAttribute, strValue );
LPCTSTR pstrOpArg = va_arg( vl, LPCTSTR );
CString strOption;
bool bAttr = true;
while ( pstrOpArg )
{
if ( bAttr )
{
strOption.Format( "%s=", pstrOpArg );
strBuffer += strOption;
bAttr = false;
}
else
{
strOption.Format( "\"%s\"", pstrOpArg );
strBuffer += strOption;
bAttr = true;
}
pstrOpArg = va_arg( vl, LPCTSTR );
}
fileOutput.WriteString( strBuffer );
}
|
|
|
|
|
Royce Fickling wrote: I thought that if the optional arguments were not passed, the first call to va_arg would return a null, but that is not the case.
Yes, that is NOT the case. You'd need to pass a NULL parameter.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|