ReadFile返回0字节,GetLastError()为0

时间:2017-06-06 10:43:21

标签: c windows-ce

我正在使用与R-232连接器到外部设备的串行通信进行通用的写入和读取操作。     对于特定的写操作,readFile返回数据。     但是对于一个命令,它返回0个字节,GetLastError()返回0。     我在Windows PDA上使用该应用程序。     以下是使用的代码:

long port = jport;
    long baudrate = jbaudrate;  
    int cmd_length = env->GetStringLength(jcmd);
    const jchar* cmd = env->GetStringChars(jcmd, 0);
    zCommand = new char[cmd_length+1];
    for (int j=0;j<cmd_length;j++) 
        zCommand[j] = (char)cmd[j]; 
    zCommand[cmd_length]='\r';
    env->ReleaseStringChars(jcmd,cmd);

    WCHAR comPort[6];
    wsprintf(comPort, L"COM%d:", port);
    LPCTSTR pcCommPort = LPCTSTR( comPort );

    hCom = CreateFile( pcCommPort,
        GENERIC_READ | GENERIC_WRITE,
        0,    // exclusive-access
        NULL, // default security attributes
        OPEN_EXISTING, 
        0,    // not overlapped I/O
        NULL  
    );
    if (hCom == INVALID_HANDLE_VALUE) 
    {
        throw_executeCommand(env,"java/lang/Exception","INVALID_HANDLE_VALUE");
        return env->NewStringUTF("");
    }
    //Clear all input output buffer
    PurgeComm(hCom, PURGE_RXCLEAR | PURGE_TXCLEAR);
    //Configure DCB
    DCB PortDCB;
    // Initialize the DCBlength member. 
    PortDCB.DCBlength = sizeof (DCB); 
    // Get the default port setting information.
    GetCommState (hCom, &PortDCB);
    // Change the DCB structure settings.
    PortDCB.BaudRate = baudrate;          // Zera specified baud
    PortDCB.ByteSize = 8;
    PortDCB.StopBits = TWOSTOPBITS;
    PortDCB.Parity = PARITY_NONE;
    PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
    PortDCB.fRtsControl = RTS_CONTROL_ENABLE | (RTS_CONTROL_TOGGLE ^ RTS_CONTROL_HANDSHAKE );
    PortDCB.fAbortOnError = FALSE;
    //Set Com Timeouts
    COMMTIMEOUTS commTimeouts;
    if(!GetCommTimeouts(hCom, &commTimeouts))
    {
        throw_executeCommand(env,"java/lang/Exception","Exception GetCommTimeouts");
        return env->NewStringUTF("");
    }
    commTimeouts.ReadIntervalTimeout = 50;
    commTimeouts.ReadTotalTimeoutMultiplier = 10;
    commTimeouts.ReadTotalTimeoutConstant = 50;
    commTimeouts.WriteTotalTimeoutConstant = 50;
    commTimeouts.WriteTotalTimeoutMultiplier = 10;
    if(!SetCommTimeouts(hCom, &commTimeouts))
    {
        throw_executeCommand(env,"java/lang/Exception","Exception SetCommTimeouts");
        return env->NewStringUTF("");
    }
    if (!SetCommState (hCom, &PortDCB))
    {
          throw_executeCommand(env,"java/lang/Exception","Exception SetCommState");
          return env->NewStringUTF("");
    }
    unsigned long numBytesWritten;
    unsigned long numBytesRead;
    BOOL bWriteResult; 
    bWriteResult =  WriteFile(hCom,zCommand, sizeof (zCommand),&numBytesWritten,NULL);

    if (!bWriteResult)
    {
          throw_executeCommand(env,"java/lang/Exception","Exception WriteFile");
          return env->NewStringUTF("");
    }
    //Time to read
    string result="";
    BOOL bReadResult; //AAV,ARC,AGR1
    char    sBuffer[128];
    BOOL datafound;
    numBytesRead = 0;
    while(true)
    {

        bReadResult = ReadFile(hCom, &sBuffer, 128, &numBytesRead, NULL);

        if (numBytesRead > 0)
        {
            for (int i=0;i<(int)numBytesRead;i++)
            {
                datafound = TRUE;
                if (sBuffer[i]=='\r')
                {
                    result += '\n';
                }else
                {
                    result += sBuffer[i];
                }
            }
        }
        else
        {
            break;
        }
    }
    if (datafound)
    {
        if (hCom != INVALID_HANDLE_VALUE)
        {
            cleanup_executeCommand();
            return env->NewStringUTF(result.c_str()); 
        }
    }
    cleanup_executeCommand();
    throw_executeCommand(env,"java/lang/Exception","err");
    return env->NewStringUTF("");

0 个答案:

没有答案
相关问题