2

我正在尝试将 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 到 com)吗?
(注意:如果我没有指定超时 WriteFile 只是挂起)

portMon 转储:

        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     
4

1 回答 1

1

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

于 2012-12-23T22:09:00.360 回答