当前位置:网站首页>Serial port circular buffer is simple and initialization free, without heap, pointer and segmented memcpy
Serial port circular buffer is simple and initialization free, without heap, pointer and segmented memcpy
2022-07-19 06:27:00 【xp5xp6】
#include <stdio.h>
#include <stdlib.h>
#define RINGBUF_LEN 32
typedef struct ringbuf_t
{
int head;// The head pointer
int tail;// Tail pointer
int len;// length
char buf[RINGBUF_LEN];// Buffer size
}ringbuf_t;
ringbuf_t t_buf_ring;
// Buffer initialization
void init_ringbuf(void)
{
memset(&t_buf_ring, 0, sizeof(t_buf_ring));
}
// The team
int ringbuf_write(char data)
{
if(RINGBUF_LEN <= t_buf_ring.len) {
return -1;
}
t_buf_ring.buf[t_buf_ring.tail] = data;
t_buf_ring.tail = (t_buf_ring.tail + 1) % RINGBUF_LEN;
t_buf_ring.len++;
return 0;
}
// Out of the team
int ringbuf_read(char *date)
{
if(0 >= t_buf_ring.len) {
return -1;
}
*date = t_buf_ring.buf[t_buf_ring.head];
t_buf_ring.head = (t_buf_ring.head + 1) % RINGBUF_LEN;
t_buf_ring.len--;
return 0;
}
// Out of the team
int ringbuf_show()
{
int i,j;
for (i = 0, j = t_buf_ring.head; i < t_buf_ring.len; i++, j++) {
printf("%d --- %c ", i, t_buf_ring.buf[j]);
}
printf("\n");
return 0;
}
// The main function
int main()
{
char c, c_r;
int i, cnt;
//setvbuf(stdout,NULL,_IONBF,0); //pinrtf、putchar Cannot output immediately , Open this comment
//init_ringbuf();
printf("Please enter a line [blank line to terminate]> ");
do{
c=getchar();
putchar(c);
switch(c)
{
case 'Q':
goto exit;
break;
case 'R':
printf("----------read----------------\n");
printf("len - %d \n", t_buf_ring.len);
cnt = t_buf_ring.len;
for (i = 0; i < cnt; i++) {
ringbuf_read(&c_r);
printf("%d ---- %c ", i, c_r);
}
//ringbuf_show();
break;
default :
printf("----------write-----\n");
if(c!='\n')
ringbuf_write(c);
printf("%d len\n", t_buf_ring.len);
ringbuf_show();
break;
}
}while (1);
exit:
printf("exit.\n");
return 0;
}
边栏推荐
- Chrome browser settings [display the translation language icon in the upper right corner]
- [usaco06dec]the fewest coins g (hybrid backpack)
- busybox date 时间的加减
- 2022 robocom world robot developer competition - undergraduate group (provincial competition)
- Computational geometry (2)
- Unity2d learning Fox game production process 1: basic game character control, animation effects, lens control, item collection, bug optimization
- 谷歌浏览器不能手动修改cookies,cookie报红标红
- 數學基礎課2_歐拉函數,線性篩,擴歐
- Decorate Apple Tree
- 【力扣】二叉树的最大深度
猜你喜欢

【力扣】用队列实现栈

【力扣】用栈实现队列

三维凝视估计,没有明确的个人校准2018
![MySQL workbench basically uses [create a database]](/img/d2/a753e9c77bb17aaff1d0e9dc11c803.png)
MySQL workbench basically uses [create a database]

【力扣】环形链表 II
![[force buckle] copy the linked list with random pointer](/img/ed/eb54aad302da3aaac7e1d583770ca0.png)
[force buckle] copy the linked list with random pointer

Unity2d learning Fox game production process 1: basic game character control, animation effects, lens control, item collection, bug optimization

【力扣】另一棵树的子树

Learning non posture gaze deviation with head movement

用头部运动学习无姿态注视偏差
随机推荐
mapping索引属性 & 创建索的操作
你见过的最差的程序员是怎样的?
2022/07/12 学习笔记 (day05)循环
[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....
Leetcode string
[detailed tutorial installation] [configuration] auxiliary plug-ins about eslint in vscode
带透明png转换成c数组
Basic mathematics course 2_ Euler function, linear sieve, extended Euler
[BJOI2019] 排兵布阵(分组背包)
你的企业最适合哪种深度学习?
uboot 编译前的配置命令make config分析
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)
Acwing第57场周赛(AK)
[force buckle] copy the linked list with random pointer
Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
MySQL workbench basically uses [create a database]
【力扣】括号匹配
TypeScript学习
【力扣】二叉树的最大深度
Acwing game 57 (AK)