I have a struct defined below for the contents of a frame to be sent over uart.
I populate the frame using build_uart_frame()
.
I have a function write_device()
to transmit the frame over uart.
My question is, how do I pass the struct to write_device()
as it is expecting a pointer to a char
array.
Should I try convert the stuct variable rdata
to a char array
or am I approaching this wrong?
This question is related to another post that I have (I'm not sure if I your allowed do this). I understand that this is not a code writing service, I do try avoid asking questions but I'm a bit out of my depth.
That question is posted here: related question
Many thanks
typedef struct uart_frame {
uint8_t sof; /* 1 byte */
uint8_t len; /* 1 bytes */
uint8_t cmd0; /* 1 byte */
uint8_t cmd1;
char data[11];
unsigned char fcs; /* 1 byte */
} uart_frame_t;
//------------------------------------------------------------------------------
// Global uart frame
uart_frame_t rdata;
//------------------------------------------------------------------------------
// Populate the frame
int build_uart_frame() {
uart_frame_t *rd = &rdata; //pointer variable 'rd' of type uart_frame
// common header codes
rd->sof = 0xFE;
rd->len = 11;
rd->cmd0 = 0x22;
rd->cmd0 = 0x05;
snprintf(rd->data, sizeof(rd->data), "%s", "Hello World");
rd->fcs = calcFCS((unsigned char *)rd, sizeof(uart_frame_t) - 1);
return 0;
}
//--------------------------------------------------------------------------
int write_device(char *txbuf, int size) {
DWORD BytesWritten;
isolator_status = FT_Write(isolator_handle, txbuf, size, &BytesWritten);
if (isolator_status == FT_OK) {
return 0;
}
return -1;
}
//--------------------------------------------------------------------------
int main() {
build_uart_frame();
write_device(??);
return 0;
}
int main() {
char* tempBuf = (char*) &rdata;
/* Clear the buffer*/
memset( (char*)rdata, 0x00, sizeof(rdata));
build_uart_frame();
write_device(tempBuf, sizeof(uart_frame_t));
/* You can have problem with BYTE alignement so you can check the result of sizeof(uart_frame_t) if it is really equal to declaration */
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.