繁体   English   中英

数组值更改为 0

[英]Array values changed to 0

我正在通过消息队列传递一个数组:

double data[20];
if(i!=20)
{
    data[i] = getInstance()->real_time_data.f_ChannelData;
    i++;
}
else
{
    i = 0;
    MsgQueueCommunicator::getInstance()->Write_Array_To_Queue(s_hMsgQueue_Communication, data);
}

Write_Array_To_Queue()看起来像这样:

BOOL MsgQueueCommunicator::Write_Array_To_Queue(HANDLE hq,double data[20])
{
    return WriteMsgQueue(hq,(LPVOID)data, 160,INFINITE,0);
}

从队列中读取与写入几乎相同:

BOOL MsgQueueCommunicator::Read_Buffer_From_Queue(HANDLE hq,double data[20])
{
    DWORD dwBytesRead;
    DWORD dwFlags;
    return ReadMsgQueue(hq, (LPVOID)data, 160, &dwBytesRead, INFINITE, &dwFlags);
}

要在两端初始化队列,我使用以下函数:

HANDLE MsgQueueCommunicator::InitMessageQueue(bool IsRead,wchar16_t* wQueueName)
{
  MSGQUEUEOPTIONS msgopts;
  msgopts.dwSize        = sizeof(MSGQUEUEOPTIONS);   
  msgopts.dwFlags       = MSGQUEUE_ALLOW_BROKEN;//0;
  msgopts.dwMaxMessages = 0;
  msgopts.cbMaxMessage  = 160;   
  msgopts.bReadAccess   = IsRead;   
  HANDLE hq = CreateMsgQueue(wQueueName, &msgopts);
  return hq;
}

我遇到的问题是数组的最后 3 个值偶尔会为 0。我通过在写入队列之前和之后打印数组中的值来检查并注意到差异。 我不确定问题出在哪里。 我尝试更改目标缓冲区的字节数,打印出读取的字节数以确保它是正确的,但我还没有找到根本原因。

这是我打印出值时看到的片段:

6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088488 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088490 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088490 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088490 PID:7bb0046 TID:6a50052 Channel Data (USB): 0
6088490 PID:7bb0046 TID:6a50052 Channel Data (USB): 0
6088492 PID:7bb0046 TID:6a50052 Channel Data (USB): 0
6088528 PID:7bb0046 TID:6a50052 Bytes Read: 160
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088528 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088530 PID:7bb0046 TID:6a50052 Channel Data (USB): 0
6088530 PID:7bb0046 TID:6a50052 Channel Data (USB): 0
6088530 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Bytes Read: 160
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088568 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088569 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088569 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088570 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088570 PID:7bb0046 TID:6a50052 Channel Data (USB): 0
6088570 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100
6088570 PID:7bb0046 TID:6a50052 Channel Data (USB): 1100

0 不是有意的,但 1100 是。 我不确定我做错了什么(如果有的话),但是是否可以通过消息队列发送数组而不会出现内存问题?

注意:这是为 Platform Builder (WEC7) 和 VS2008 开发的。

这看起来像是进程间通信中的计时问题。 由于相对时间,读取成功但字符数少于发送的字符数。
您需要检查短读并重试以完成所需的数量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM