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

C ReadFile返回0字节,GetLastError()为0,c,windows-ce,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

我使用的是一个通用的写和读操作,使用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("");
long port=jport;
长波特率=jbaudrate;
int cmd_length=env->GetStringLength(jcmd);
常量jchar*cmd=env->GetStringChars(jcmd,0);
zCommand=新字符[cmd_长度+1];
for(intj=0;jreleasetringchars(jcmd,cmd);
WCHAR-comPort[6];
wsprintf(组件,L“COM%d:”,端口);
LPCTSTR pcCommPort=LPCTSTR(comPort);
hCom=创建文件(pcCommPort,
一般的读,一般的写,
0,//独占访问
NULL,//默认安全属性
开放式,
0,//未重叠的I/O
无效的
);
if(hCom==无效的句柄值)
{
抛出执行命令(env,“java/lang/Exception”,“无效的句柄值”);
返回env->NewStringUTF(“”);
}
//清除所有输入输出缓冲区
PurgeComm(hCom、PURGE_RXCLEAR | PURGE_TXCLEAR);
//配置DCB
DCB端口DCB;
//初始化DCBlength成员。
PortDCB.DCBlength=sizeof(DCB);
//获取默认端口设置信息。
GetCommState(hCom和PortDCB);
//更改DCB结构设置。
PortDCB.BaudRate=波特率;//指定的波特率
PortDCB.ByteSize=8;
PortDCB.StopBits=两个StopBits;
PortDCB.Parity=Parity\u NONE;
PortDCB.fDtrControl=DTR\u CONTROL\u ENABLE;
PortDCB.fRtsControl=RTS_CONTROL_ENABLE |(RTS_CONTROL_切换^RTS_CONTROL_握手);
PortDCB.fAbortOnError=FALSE;
//设置Com超时
通信超时通信超时;
如果(!GetCommTimeouts(hCom和commTimeouts))
{
抛出_executeCommand(env,“java/lang/Exception”,“Exception getcommtimeout”);
返回env->NewStringUTF(“”);
}
commTimeouts.ReadIntervalTimeout=50;
commTimeouts.ReadTotalTimeout乘数=10;
commTimeouts.ReadTotalTimeoutConstant=50;
commTimeouts.WriteTotalTimeoutConstant=50;
commTimeouts.WriteTotalTimeout乘数=10;
如果(!设置通信超时(hCom和通信超时))
{
抛出_executeCommand(env,“java/lang/Exception”,“Exception setcommtimeout”);
返回env->NewStringUTF(“”);
}
如果(!SetCommState(hCom和端口DCB))
{
抛出_executeCommand(env,“java/lang/Exception”,“Exception SetCommState”);
返回env->NewStringUTF(“”);
}
未签名的长代码被写入;
无符号长字节读取;
布尔结果;
bWriteResult=WriteFile(hCom,zCommand,sizeof(zCommand),&numbyteswrited,NULL);
如果(!bWriteResult)
{
抛出_executeCommand(env,“java/lang/Exception”,“Exception WriteFile”);
返回env->NewStringUTF(“”);
}
//该读书了
字符串结果=”;
BOOL bReadResult;//AAV、ARC、AGR1
char-sBuffer[128];
发现布尔数据;
numBytesRead=0;
while(true)
{
bReadResult=ReadFile(hCom,&sBuffer,128,&numBytesRead,NULL);
如果(numBytesRead>0)
{
for(int i=0;iNewStringUTF(result.c_str());
}
}
cleanup_executeCommand();
抛出执行命令(env,“java/lang/Exception”,“err”);
返回env->NewStringUTF(“”);

“if(datafound)”-“datafound”不能保证在该点上保存有意义的值。它要么为TRUE,要么未初始化/未定义。“cleanup\u executeCommand()是什么?”'do?我看不到您在任何地方检查
ReadFile
的结果,只检查读取的字节数。引用:当同步读取操作到达文件末尾时,ReadFile返回TRUE并将*lpNumberOfBytesRead设置为零。显示的代码没有对'GetLastError()的任何调用“。谢谢。我会将datafound初始化为FALSE。cleanup_executeCommand()仅用于关闭句柄。我在代码中添加了readFile的检查,它返回1。但noOfBytesRead仍然是0。”