WriteFile到串行端口总是超时,写入的字节数为零

时间:2012-12-22 11:56:36

标签: c++ file-io serial-port port at-command

我正在尝试将At命令发送到我的手机(诺基亚5130)

手机通过USB端口(在调制解调器模式下使用)连接,因此安装驱动程序后 我把它列为设备管理器上的COM端口(诺基亚5130 XPressMusic USB串口(COM17))

以下是代码:

            #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;
    }

有人能告诉我为什么WriteFile不会向串口写任何东西吗?

是因为com端口不是真正的com端口(usb to com)?
(注意:如果我没有指定超时WriteFile挂起)

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     

1 个答案:

答案 0 :(得分:1)

好吧我通过更改com端口号解决了这个问题 即使COM17是设备管理器显示的,它也不是正确的端口 这与端口配置无关(默认值有效) 而已 ! 谢谢大家。