當時我正在開發一個項目,在這個項目上有幾個處理器在使用can bus之間進行通信。主控制器(小獵犬骨骼)使用罐式公交車控制其他設備。使用套接字可以在linux框架中編寫一個讀取從其他設備發送的can消息的進程,現在我想將我得到的消息放入FIFO緩衝區中,然後將消息放入obrabotam中。 所以我需要在裏面寫入can_frame結構的FIFO緩衝區。如何製作一個帶有can_frame結構的FIFO緩衝區?
例如:
struct can_buffer {
struct can_frame *frames;
int head;
int tail;
int size;
};
can_buffer new_can_buffer (size_t capacity)
{
can_buffer rb = malloc(sizeof(struct can_buffer));
if (rb) {
/* One byte is used for detecting the full condition. */
rb->size = capacity + 1;
rb->frames = malloc(rb->size * sizeof(struct can_frame));
if (rb->frames)
can_buffer_reset(rb);
else {
free(rb);
return 0;
}
}
return rb;
}
size_t can_buffer_size(const struct can_buffer *rb)
{
return rb->size;
}
size_t can_buffer_capacity(const struct can_buffer *rb)
{
return can_buffer_buffer_size(rb) - 1;
}
size_t can_buffer_free(const struct can_buffer *rb)
{
if (rb->head >= rb->tail)
return can_buffer_capacity(rb) - (rb->head - rb->tail);
else
return rb->tail - rb->head - 1;
}
int can_buffer_is_full(const struct can_buffer *rb)
{
return can_buffer_free(rb) == 0;
}
int can_buffer_is_empty(const struct can_buffer *rb)
{
return can_buffer_free(rb) ==can_buffer_capacity(rb);
}
void can_buffer_reset(can_buffer rb)
{
rb->head = rb->tail = 0;
}
......... ........
/*添加消息到隊列的末尾。 */
void can_buffer_push(struct can_buffer *cb, struct can_frame *frame)
{
memcpy(&cb->frames[cb->tail], frame, sizeof(struct can_frame));
cb->tail = (cb->tail + 1) % cb->size;
}
/* Retrieve message from the start of the queue. */
can_frame *can_buffer_pop(struct can_buffer *cb)
{
struct can_frame *frame;
memcpy(frame, &cb->frames[cb->head], sizeof(struct can_frame));
cb->head = (cb->head + 1) % cb->size;
return frame;
}
但我canoot成功地做到了。我認爲問題是每個can_frame結構裏面都是一個結構,這就是問題(例如int,char等),但我不知道如何解決這個問題。
如何製作一個FIFO緩衝區,可以存儲can_frame結構?
我需要寫在C lagnuage
主我打電話
can_buffer can_buff;
can_buff = new_can_buffer(100);
can_buffer_push(can_buff,frame);
幀= can_frame我收到
can_buff = FIFO緩衝器
我剛編輯的功能! can_buffer_push可以嗎?你能幫我完成嗎? –
好的,我寫了一個測試程序。感謝所有的時間。你能糾正錯誤的代碼嗎?請幫助我!謝謝 –