現時点では、いくつかのプロセッサが通信している間に、私は缶バスを使ってプロジェクトを進めています。主制御装置(ビーグルボーン)は、缶バスを使用して他の装置を制御する。ソケットを使うことができますlinuxフレームワーク私は、他のデバイスから送信されたcanメッセージを読み取るプロセスを書いています。今は、私がFIFO buferに入れたメッセージを入れて、メッセージを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;
}
しかし、私はそれをうまくやることができます。私は問題は、内部のすべてのcan_frame構造体が構造体であり、問題(例えばint、charなど)ですが、この問題を解決する方法がわかりません。
can_frame構造体を内部に格納できるFIFOバッファを作成するにはどうすればよいですか?私はあなたが不完全に変更した、
can_buff = FIFOバッファまあ
私は機能を編集しました! can_buffer_pushは大丈夫ですか?これを完了するのを手伝ってもらえますか? –
私はテストプログラムを書いている瞬間に大丈夫です。すべてのあなたのtime.Canあなたが間違ったコードを修正することができますありがとう? –