Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
when i write AT Commands in dat array for sending it to GSM Device

int TransxPortGSM() Function
C++
unsigned char dat[3]={'A','T'};

//tried char,int..

DWORD dwNumBytesWritten=sizeof(dat);
    check=WriteFile( hPort,              // Port is getting correct 
               &dat,               // Pointer to the data to write
               2,                  // Number of bytes to write n tried[1.2.3...] 
               &dwNumBytesWritten, // Pointer to the number of bytes written
               NULL                // Must be NULL for Windows CE
            );



and ReadFile execute it
int ReadxPortGSM() Function
C++
bResult=ReadFile( hPort,                // Port handle
                  &cp.Byte,                // out Pointer to the buffer that receives the data read from the file.
                  1,                    // Number of bytes to read
                  &cp.dwBytesTransferred,  // out Pointer to number of bytes read
                  NULL                  // Must be NULL
                );


my setTimeDCB() is
C++
cp.CommTimeouts.ReadIntervalTimeout = 100;    //MAXDWORD
cp.CommTimeouts.ReadTotalTimeoutMultiplier = 0;  //10
cp.CommTimeouts.ReadTotalTimeoutConstant = 10;    //13
cp.CommTimeouts.WriteTotalTimeoutMultiplier = 10;  //10
cp.CommTimeouts.WriteTotalTimeoutConstant = 10;   //1000


cp._dcb.BaudRate = 115200;                   // Current baud
cp._dcb.fBinary = TRUE;                      // Binary mode; no EOF check
cp._dcb.fParity = FALSE;                         // Enable parity checking
cp._dcb.fOutxCtsFlow = TRUE;                 // No CTS output flow control
cp._dcb.fOutxDsrFlow = TRUE;                 // No DSR output flow control
cp._dcb.fDtrControl = DTR_CONTROL_ENABLE;    // DTR flow control type
cp._dcb.fDsrSensitivity = FALSE;             // DSR sensitivity
cp._dcb.fTXContinueOnXoff = TRUE;            // XOFF continues Tx
cp._dcb.fOutX = FALSE;                       // No XON/XOFF out flow control
cp._dcb.fInX = FALSE;                        // No XON/XOFF in flow control
cp._dcb.fErrorChar = FALSE;                  // Disable error replacement
cp._dcb.fNull = FALSE;                       // Disable null stripping
cp._dcb.fRtsControl = RTS_CONTROL_ENABLE;    // RTS flow control
cp._dcb.fAbortOnError = FALSE;               // Do not abort reads/writes on error
cp._dcb.ByteSize = 8;                        // Number of bits/byte, 4-8
cp._dcb.Parity = NOPARITY;                   // 0-4=no,odd,even,mark,space
cp._dcb.StopBits = ONESTOPBIT;               // 0,1,2 = 1, 1.5, 2


C++
int main(int argc, char* argv[])
{    
   initPort();
   settimeDCB();
   TransxPortGSM();  //sending AT
   ReadxPortGSM();   // getting AT but expected OK
   closePort();
}


but the output getting for ReadxPortGSM() reading is "AT" and not "OK"...

Am i missing anything after write file?
is GSM Device processing my AT command?
Posted
Updated 11-May-11 3:37am
v5
Comments
Olivier Levrey 11-May-11 9:16am    
What do you do in ReadxPortGSM? And TransxPortGSM? Is it the code at the top? We can't guess if you don't tell...
01.mandar 11-May-11 9:30am    
oh thanks,
the above code is for transmission of AT command from my code to GSMDevice. it transmits but when ReadFile() is executed it gets the same command as i have given in writeFile() My device is not executing the AT commands.
i have tried different DCB and timeouts configuration but same condition.


I believe you need to add a delay there, the simplest thing you can do is adding a Sleep.
Sample code[^]
 
Share this answer
 
Comments
01.mandar 11-May-11 10:11am    
i have added a sleep after transmission

Sleep(15000);

but same result getting AT as output after adding delay
01.mandar 11-May-11 10:21am    
ReadFile() gives output ATTTTTTTTT if done in loop
Device is not taking Values in the file for processing, is any problem in my device
Am i giving Correct values to WriteFile()?
Stephen Wiria 11-May-11 10:25am    
there're couple of things you need to re-check, start with the missing \r\n chars in your first AT command, DCB, then nNumberOfBytesToWrite/nNumberOfBytesToRead param (as already pointed out by Olivier Levrey), etc..
01.mandar 13-May-11 3:10am    
Thanks the code worked flawless on changing USB-Serial cable with above changes.
Thanks Olivier Levrey and Stephen Wiria
Check the return value of ReadFile. Is it returns FALSE, then call GetLastError to have details about the error. Also check the number of bytes really read 'cp.dwBytesTransferred).

Form this code:
C++
bResult=ReadFile( hPort,
                  &cp.Byte,
                  1,
                  &cp.dwBytesTransferred,
                  NULL
                );

you ask to read only 1 byte. So it is not possible to receive "OK", nor "AT". You should at least replace 1 y 2.

You may need also to wait a little bit after you sent "AT", to let your device do its processing. After the wait, you can read:

C++
TransxPortGSM();
//wait 1 second before reading the answer.
//It is probably too much: just for testing...
Sleep(1000);
ReadxPortGSM();   
 
Share this answer
 
Comments
01.mandar 11-May-11 10:15am    
hi
i have put the delay after transmission same as you said and kept readFile in loop for 10 times

it gives me ATTTTTTTTT
Olivier Levrey 11-May-11 10:22am    
Are you sure you just need to send "AT" to the device? Usually we send '\r' for the last character. Read your device documentation carefully about that.
01.mandar 13-May-11 3:10am    
Thanks the code worked flawless on changing USB-Serial cable with above changes.
Thanks Olivier Levrey and Stephen Wiria
Olivier Levrey 13-May-11 3:33am    
Ok good then. You are welcome.
01.mandar 13-May-11 10:25am    
i have attached \x1A at the end of the message and have transmitted to port it does not work,But NOW when i open hyper terminal and type AT and press enter it gives me > to write message and press crtl+z so it sends but the message is same which i have wrote in code and not on >....

is \x1A correct taken by port how can i find it?


thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900