2016-10-06 9 views
0

uart를 통해 전송할 프레임 내용에 대해 아래에 정의 된 구조체가 있습니다.구조체를 C 언어의 문자 배열로 변환

build_uart_frame()을 사용하여 프레임을 채 웁니다.

저는 uart를 통해 프레임을 전송하는 기능이 write_device()입니다.

내 질문은 char 배열에 대한 포인터를 기대하므로 write_device()으로 구조체를 전달하는 방법입니다.

stuct 변수 rdatachar array으로 변환해야합니까, 아니면 제가 잘못 접근해야합니까?

이 질문은 내가 가지고있는 다른 게시물과 관련이 있습니다 (귀하의 허락을 받았는지 확실하지 않습니다). 나는 이것이 코드 작성 서비스가 아니라는 것을 이해한다. 나는 질문하는 것을 피하려고 노력한다. 그러나 나는 내 깊이에서 조금 벗어난다.

그 질문이 여기에 게시됩니다 : related question

많은 감사

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; 
} 
+1

표준 변호사 나를 찍은 것'write_device ((의 char *) (RDATA)를 sizeof (uart_frame_t을)); ' – LPs

+0

@LPs 문제는 실제로이를 수행하는 데 적합한 방법이 없다는 것입니다. 나는 똑같이 할 것이다. 구조체 패킹이 문제가되지 않는지 확인해야하며,이 경우에는 그렇지 않습니다. –

+0

@ KlasLindbäck 표준 변호사는 비트 연산자를 기반으로 직렬화 및 역 직렬화를 사용한다고 할 수 있습니다. ;) – LPs

답변

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; 

}