Reputation: 43
I'm trying to send At commands to my mobile phone (Nokia 5130)
The phone is connected via a usb port (used in modem mode), so after I installed the driver I get it listed as a COM port on device manager (Nokia 5130 XPressMusic USB Serial Port (COM17))
Here is the code :
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
HANDLE hport = INVALID_HANDLE_VALUE;
DCB portConfig;
hport = CreateFile( TEXT("\\\\.\\COM17"),GENERIC_READ | GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,0);
if(hport == INVALID_HANDLE_VALUE)
{
cout<<GetLastError();
system("pause");
return 0;
}
if(GetCommState(hport,&portConfig)==0)
{
cout<<"Erreur de recuperation de la configuration :"<<GetLastError()<<endl;
system("pause");
return 0;
}
portConfig.BaudRate = CBR_9600;
portConfig.Parity = NOPARITY ;
portConfig.StopBits = ONESTOPBIT;
portConfig.ByteSize = 8;
portConfig.fBinary = TRUE;
portConfig.fDtrControl = DTR_CONTROL_HANDSHAKE;
portConfig.fOutX = true;
portConfig.fRtsControl = RTS_CONTROL_HANDSHAKE;
portConfig.fAbortOnError = TRUE;
portConfig.fParity = TRUE;
if(SetCommState(hport,&portConfig)==0)
{
cout<<"Erreur de configuration Setcommstate:"<<GetLastError()<<endl;
system("pause");
return 0;
}
COMMTIMEOUTS comTimeOut;
comTimeOut.ReadIntervalTimeout = 3000;
comTimeOut.ReadTotalTimeoutMultiplier = 3000;
comTimeOut.ReadTotalTimeoutConstant = 2000;
comTimeOut.WriteTotalTimeoutMultiplier = 3000;
comTimeOut.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(hport,&comTimeOut);
DWORD dwNumberOfBytesWritten;
unsigned char * buffer = new unsigned char[4];
buffer[0] = 'A';
buffer[1] = 'T';
buffer[2] = '\r';
buffer[3] = '\n';
WriteFile(hport,buffer,4,&dwNumberOfBytesWritten,NULL);
cout<<"erreur "<<dwNumberOfBytesWritten<<" d'ecriture :"<<GetLastError();
delete [] buffer;
system("pause");
CloseHandle(hport);
return 0;
}
Can anybody tell me why WriteFile doesn't write anything to the serial port ?
Is it because the com port is not a real com port (usb to com) ?
(NB : If I don't specify the timeout WriteFile just hangs)
portMon Dump :
19 0.00002536 ConsoleApplica IRP_MJ_CREATE USBSER000 SUCCESS Options: Open
20 0.00047645 ConsoleApplica IOCTL_SERIAL_GET_BAUD_RATE USBSER000 SUCCESS
21 0.00035417 ConsoleApplica IOCTL_SERIAL_GET_LINE_CONTROL USBSER000 SUCCESS
22 0.00000045 ConsoleApplica IOCTL_SERIAL_GET_CHARS USBSER000 SUCCESS
23 0.00000045 ConsoleApplica IOCTL_SERIAL_GET_HANDFLOW USBSER000 SUCCESS
24 0.00032201 ConsoleApplica IOCTL_SERIAL_GET_BAUD_RATE USBSER000 SUCCESS
25 0.00022917 ConsoleApplica IOCTL_SERIAL_GET_LINE_CONTROL USBSER000 SUCCESS
26 0.00000091 ConsoleApplica IOCTL_SERIAL_GET_CHARS USBSER000 SUCCESS
27 0.00000000 ConsoleApplica IOCTL_SERIAL_GET_HANDFLOW USBSER000 SUCCESS
28 0.00071875 ConsoleApplica IOCTL_SERIAL_SET_BAUD_RATE USBSER000 SUCCESS Rate: 9600
29 0.00062953 ConsoleApplica IOCTL_SERIAL_SET_LINE_CONTROL USBSER000 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
30 0.00000091 ConsoleApplica IOCTL_SERIAL_SET_CHAR USBSER000 SUCCESS EOF:0 ERR:0 BRK:0 EVT:0 XON:0 XOFF:0
31 0.00000045 ConsoleApplica IOCTL_SERIAL_SET_HANDFLOW USBSER000 SUCCESS Shake:80000042 Replace:81 XonLimit:0 XoffLimit:0
32 0.00000045 ConsoleApplica IOCTL_SERIAL_SET_TIMEOUTS USBSER000 SUCCESS RI:3000 RM:3000 RC:2000 WM:3000 WC:2000
33 14.00148370 ConsoleApplica IRP_MJ_WRITE USBSER000 TIMEOUT Length 4: ....
34 0.00000408 ConsoleApplica IRP_MJ_CLEANUP USBSER000 SUCCESS
35 0.00047464 ConsoleApplica IRP_MJ_CLOSE USBSER000 SUCCESS
Upvotes: 2
Views: 3001
Reputation: 43
ok i solved this by changing the com port number even if COM17 is what is shown at device manager it was not the right port this has nothing to do with port configuration (default values work) that's it ! thanks everyone.
Upvotes: 1