我正在开发一个代码,其中我需要使用指针将一个函数(“ Read()”)的值读入另一个函数(“ compare”)。需要访问函数“ Read()”中存在的“ buffer”的值。我尝试了以下方法,我是指针实现的新手,因此无法尝试太多。 “缓冲区”将有9个元素,我已在“ Read()”函数的“ adata”中复制了这些元素,需要在“比较”函数中具有该数据。 以下是我的代码段:
int16_t *Read (uint8_t *buffer, const uint16_t length)
{
static uint32_t totalBytes = 0;
static uint32_t respNo = 0;
int i;
GPIO_ClearValue(PMAP_GPIO_PORT_DIR_RS485, PMAP_GPIO_PIN_DIR_RS485);
UartRxFlush(UARW_UART_INDEX_RS485); //this function needs to be executed after the GPIO_ClearValue in order to remove the "zero" value from the buffer
respNo++;
int counter = 0; // counts n. valid bytes put in slave response buffer[]
do
{
OSTimeDly(2);
int8_t newBytesRcv = UartReceive(UARW_UART_INDEX_RS485,
(uint8_t*)&(buffer[counter]), length-counter);
totalBytes += newBytesRcv;
counter = totalBytes;
}
while (counter < length);
totalBytes = 0;
printf("\n");
printf("Byte received........");
int16_t adata[9] = {0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
for (i=0; i<9;i++)
{
printf("%d ",buffer[i]);
adata[i] = buffer[i];
}
printf("\n");
}
int16_t Compare(uint8_t * message, uint16_t len)
{
rs485_message_t rs485Msg;
int StP15Vmsg[9] = {0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34};
int adata[9] = {0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
uint8_t buffer[9];
const uint8_t length;
for (int j=0; j<=9; j++)
{
adata[j] = *Read(buffer, j);
}
if(compareArray(FWmsg,arduinodata,7)==0)
{
char res;
uint8_t add, fwcommand, fwaction;
uint16_t fwvalue;
rs485_message_t rs485Msg;
proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue);
res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue);
}
}
请指导。...
谢谢
答案 0 :(得分:0)
注意:这不是 stricto sensu 的“答案”,而是更多评论列表
关于阅读
签名是
int16_t *Read (uint8_t *buffer, const uint16_t length)
但是该函数从不返回值,在这种情况下,可能要返回读取的字节数
int16_t Read (uint8_t *buffer, const uint16_t length)
静态变量 respNo 仅增加而从未使用过,可以删除这两行:
static uint32_t respNo = 0; respNo++;
没有理由让 totalBytes 静态,因为在打印缓冲区之前将其值重置为0,因此从调用到下一个没有历史性。它必须是 local 。
计数器是无用的,因为它始终总是 totalBytes
UartReceive 始终成功,并且从不返回表示错误的值?
在
printf("\n"); printf("Byte received........"); int16_t adata[9] = {0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30}; for (i=0; i<9;i++) { printf("%d ",buffer[i]); adata[i] = buffer[i]; } printf("\n");
可能循环必须从0到totalBytes - 1
才能与上面的代码兼容:
printf("\nByte received : "); for (i=0; i<totalBytes;i++) { printf("%u ",buffer[i]); } putchar('\n');
关于比较
在
rs485_message_t rs485Msg; int StP15Vmsg[9] = {0x09,0x31,0x33,0x30,0x30,0x30,0x30,0x30,0x34}; int adata[9] = {0x09,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30}; uint8_t buffer[9]; const uint8_t length; for (int j=0; j<=9; j++) { adata[j] = *Read(buffer, j); }
rs485Msg 和 StP15Vmsg 没用,将其删除
我不明白您为什么在循环中多次调用 Read ,每次长度都增加,所以您将在 buffer 上重写多次。当您保存 Read (当前谁返回值)的返回值时,我也不明白
也许您只想要那个:
uint8_t buffer[9];
uint8_t length = Read(buffer, 9);
在
if(compareArray(FWmsg,arduinodata,7)==0) { char res; uint8_t add, fwcommand, fwaction; uint16_t fwvalue; rs485_message_t rs485Msg; proto485ComposeMsg(&rs485Msg, &add, &fwcommand, &fwaction, &fwvalue); res = GetABFWversion(&add, &fwcommand, &fwaction, &fwvalue); }
我没有与上面的代码链接,并且设置了 res 但从未使用
比较从未进行比较,为什么将其命名为 Compare ? 它从不返回值,应该返回什么值?