C语言顺序表在串口中的应用
首先我们要知道什么是顺序表:顺序表是一个线性的储存结构,我们呢先在这里介绍一下顺序表是怎么增删改查吧
#include <stdio.h>
#include <stdlib.h>
#define eleType
typedef struct {
eleType *data; // 顺序表的数据域
size_t size; // 顺序表的个数
size_t capacity; // 顺序表的容量
}ListNode;
/*
函数作用:
为顺序表初始化
*/
void ListNodeInit(ListNode *list, size_t capacity)
{
list->capacity = capacity; // 顺序表初始化容量
list->data = (eleType *)malloc(sizeof(eleType) * capacity);
/* 为顺序表申请内存*/
list->size = 0; // 初始化个数为0
}
/*
函数作用:
销毁函数
*/
void ListNodeDestroy(ListNode *list)
{
free(list->data); /* 释放指针 */
list->data = NULL; /* 将指针置空 */
list->capacity = 0; /* 容量置空,让它没有储存的能力 */
list->size = 0; /* 个数清零 */
}
/*
函数作用:
添加元素
*/
void ListNodeAdd(ListNode *list, size_t index, eleType data)
{
if (index < 0 || index >= list->size)
{
return ; // 索引不合法返回出去
}
if (list->sizesize == list->capacity)
{
/* 这里如果容量不够就为顺序表扩容 */
list->data = (eleType)malloc(sizeof(eleType) * (capacity *= 2));
}
for (int i = list->size; i >= index; --i)
{
list->data[i - 1] = data[i]; /* 把数据全部后移 */
}
list->data[index - 1] = data; /* 插入数据 */
++list->size; /* 个数自增 */
}
/*
函数作用:
删除元素
*/
void ListnodeDelete(ListNode *lsit, size_t index)
{
if (indexx < 0 || index >= list->size)
{
return ; /* 索引不合法就返回出去 */
}
for (int i = index; i <= list->size; ++i )
{
/* 将数据往前移,覆盖掉原理啊的数据,以达到删除元素的目的 */
list->data[i] = data[i + 1];
}
--list->size; /* 在个数自减 */
}
/*
函数作用:
修改元素
*/
void ListNodeALert(ListNode *list, size_t index, eleType data)
{
if (index < 0 || index >= list->size)
{
return ;
}
list->data[index - 1] = data;
}
/*
函数作用:
查询元素
*/
eleTypeListNodeRead(ListNode*list, size_t index)
{
if(index < 0 || index >= list->size)
{
return ;
}
return list->data[index - 1];
}
这就是顺序表的实现过程,那么我们该怎么利用顺序表到唇口中呢?
这时候我们就需要知道串口的数据是怎么接受的?以51为列
extern usigned char value; /* 定义一个全局变量来接收数据 */
void Serial_Init(void)
{
SCON |= 0X50;
TMOD |= 0X20;
PCON = 0X80;
TH1 = 0XFA;
TL1 = TH1;
ES = 1;
EA = 1;
TR1 = 1; /* 设置串口波特率为9600 */
}
void Send_Data(unsigned char dat)
{
ES = 0;
TI = 0;
SBUF = dat;
while(!TI);
TI = 0;
ES = 1; /* 发送一个字节 */
}
void SendString(unsigned char *sttr)
{
while(*str != '\0')
{
Sned_Data(*(str++)); /* 发送多个字节 */
}
}
void Input_Str(void) interrupt 4
{
if(RI)
{
RI = 0;
value = SBUF;
}else if(TI){
TI = 0;
}
}
这里我们可以看到,我们可以发送遗传数据,但是并不能接收一串数据,那么我们该怎
么办呢?这里就要用到我们的顺序表了,这里我就只编写接受函数了,就不编写其他的
元素了
#define SerialMax 20 /* 宏定义一个最大接受数据 */
typedef struct {
unsigned char *data; /* 用于储存串口接收的数据 */
unsigned char flag:1; /* 接收标志位 */
unsigned char size;
} Serial;
Serial serial;
/*
这里记得把申请内存在Init函数中添加否则就是使用空指针会造成内存泄漏
*/
void Serial_Init(Serial *str)
{
SCON |= 0X50;
TMOD |= 0X20;
PCON = 0X80;
TH1 = 0XFA;
TL1 = TH1;
ES = 1;
EA = 1;
TR1 = 1; /* 设置串口波特率为9600 */
str->str = (unsigned char *)maollc(sizeof(unsigned char) * SerialMax );
}
void SerialDestroy(Serial *str)
{
free(str->str);
str->str = (unsigned char *)malloc(sizeof(unsigned char) * SerialMax );
str->str = NULL; /* 销毁内存并且重新申请内存*/
}
void SerialAdd(Serial* str, unsigned char data)
{
str->str[str->size++] = data; /* 储存数据 */
if (str->size == SerialMax )
{
str->falg = 1;
return ; /* 标志位置1,并且返回出去 */
}
}
void Serial_RecePtion(void) interrupt 4
{
usnigned char value;
if(RI)
{
RI = 0;
value = SBUF;
if (!str.flag)
{
/* 我们不是一直更新数据,首先要判断是否需要更新数据 */
SerialAdd(&str, value);
}
}else if(TI){
TI = 0;
}
}
这里我们呢要注意,这里我们的标志位没有清零,所以我们需要在主函数内将它清零,
并且我们要注意,这里我们清楚标志位的时候要把元素个数清除,或者你在标志位置1
的同时将元素个数清零,并且需要重新接收数据的时候要记得调用SerialDestroy函数
重新清除数据