我正在尝试使用LPCOpen提供的VCOM exaple来测试我的LPC4370 cortex-m4 micro(LPC Link2评估板)和我的计算机之间的通信。我只是想从matlab发送数据,将它们复制到int32_t数组中并将它们发送回主机。
这是我尝试过的。来自Matlab:
fwrite(serial_object,raw_data,'int32');
对于C部分,我只用相关代码总结:
#define RAW_SIZE 1024
unsigned char uint8_t;
typedef int int32_t;
// My Buffer
__DATA(RAM) int32_t buffer_rt_s[RAW_SIZE] = {0};
// VCOM-UART rx buffer
static uint8_t g_rxBuff[4*RAW_SIZE];
int i;
int main()
{
vcom_bread((uint8_t*)&g_rxBuff[0], 4*RAW_SIZE);
for(i=0; i < RAW_SIZE; i=i+1)
{
buffer_rt_s[i]=(int32_t*)(&g_rxBuff[0]+i*4);
}
vcom_write((uint8_t*)&buffer_rt_s[0], 4*RAW_SIZE);
}
我在这里想做什么
for(i=0; i < RAW_SIZE; i=i+1)
{
buffer_rt_s[i]=(int32_t*)(&g_rxBuff[0]+i*4);
}
是在每次迭代时访问前进4个字节的接收缓冲区。所以我要求提供地址
(&g_rxBuff[0]+i*4)
然后对于该位置的内容,指定我想要一个32位数字(即4个字节):
(int32_t*)
Results form Matlab plot.对我来说,这似乎很像我正在读取接收缓冲区的地址而不是内容。请注意,我已经使用了matlab的vcom函数,我确信它们正在工作。
任何帮助,将不胜感激。安德里亚
这里有很多不同的问题。
g_rxBuff
不一定分配在偶数地址上,接收的协议也不一定在偶数地址上分配整数。意味着通过int32_t
访问它的内容可能会导致访问错位。uint8_t
指针访问访问int32_t
数组始终是严格的别名违规错误。 (作为特例,你可以反过来做。)见What is the strict aliasing rule?要避免后两个问题,您可以在特定地址分配内存,然后使用memcpy硬拷贝所有内存。或者,使用一些union
tricks:
typedef union
{
uint8_t u8 [4];
int32_t i32;
} i32_t;
所以,在按照@Lundin提示后,我在这里发布了对我有用的解决方案。这是我的联合声明:这确保数据是4字节对齐的。
#define RAW_SIZE 64
/* my nt32 buff */
__DATA(RAM) int32_t buffer_rt_s[RAW_SIZE] = {0};
union Buffer {
uint8_t g_rxBuff[4*RAW_SIZE];
int32_t g_rxBuff32[RAW_SIZE];
};
这是我对LPCOpen USB Driver的使用
/* Union init */
Buffer b;
/* read on byte at a time*/
vcom_bread(&(b.g_rxBuff[0]), 4*RAW_SIZE);
/* do something with the data */
buffer_rt_s[i]=b.g_rxBuff32[i];
/* write - note that this cast is legal */
vcom_write((uint8_t*)&buffer_rt_s[0], 4*RAW_SIZE);
我希望这可以帮助其他人使用这种驱动程序:)