📄 superserial.c
字号:
#include "superserial.h"
#include "shell.h"
static atom WrRevBuf_SS, RdRevBuf_SS;
static unsigned char RevBuf_SS[100];
#define SIZERevBuf_SS (sizeof(RevBuf_SS)/sizeof(RevBuf_SS[0]))
#define SIZECmd_SS (SIZERevBuf_SS/3)
static unsigned int CmdLen_SS;
static unsigned char CmdFlag_SS;
static unsigned char CmdBuf_SS[SIZECmd_SS+1];
static atom WrSendBuf_SS, RdSendBuf_SS;
static volatile unsigned char SendFlag_SS;
static unsigned char SendBuf_SS[200];
#define SIZESendBuf_SS (sizeof(SendBuf_SS)/sizeof(SendBuf_SS[0]))
/////
unsigned char TrigSend_SS(void)
{
register atom rd;
rd = RdSendBuf_SS;
if (rd == WrSendBuf_SS)
{
SendFlag_SS = 0;
return 0;
}
// 发送
SCIDataSFR = SendBuf_SS[rd++];
if (rd >= SIZESendBuf_SS)
{
rd = 0;
}
RdSendBuf_SS = rd;
return 1;
}
void Init_SS(void)
{
unsigned int i;
RdRevBuf_SS = WrRevBuf_SS = 0;
for (i=0; i<SIZERevBuf_SS; i++)
{
RevBuf_SS[i] = 0;
}
CmdLen_SS = 0;
CmdFlag_SS = 0;
for (i=0; i<SIZECmd_SS; i++)
{
CmdBuf_SS[i] = 0;
}
WrSendBuf_SS = RdSendBuf_SS = 0;
SendFlag_SS = 0;
for (i=0; i<SIZESendBuf_SS; i++)
{
SendBuf_SS[i] = 0;
}
return;
}
// 接收串口数据
void RevData_SS(unsigned char data)
{
register atom wr;
wr = WrRevBuf_SS;
// 数据防误
if (wr >= SIZERevBuf_SS)
{
RdRevBuf_SS = WrRevBuf_SS = 0;
return;
}
// 接收数据
RevBuf_SS[wr++] = data;
if (wr >= SIZERevBuf_SS)
{
wr = 0;
}
// 数据阻塞处理
if (wr != RdRevBuf_SS)
{
WrRevBuf_SS = wr;
}
return;
}
// 处理接收命令
void DspRevBuf_SS(void)
{
register atom rd;
register unsigned char data;
rd = RdRevBuf_SS;
// 处理字符并回显
while (rd != WrRevBuf_SS)
{
data = RevBuf_SS[rd++];
if (rd >= SIZERevBuf_SS)
{
rd = 0;
}
RdRevBuf_SS = rd;
switch (data)
{
case '\r':// 回车符
if (0 != CmdFlag_SS)
{
if (CmdLen_SS < SIZECmd_SS)
{
CmdBuf_SS[CmdLen_SS++] = data;
}
}
CmdFlag_SS = 1;
SendData_SS(data);
break;
case '\n':// 换行符
if (1 == CmdFlag_SS)
{// 已搜索到回车
CmdBuf_SS[CmdLen_SS] = '\0';
shell(CmdBuf_SS);
CmdFlag_SS = 0;
CmdLen_SS = 0;
CmdBuf_SS[0] = '\0';
}
else
{
CmdFlag_SS = 0;
if (CmdLen_SS < SIZECmd_SS)
{
CmdBuf_SS[CmdLen_SS++] = data;
}
}
SendData_SS(data);
break;
case '\b':// 退格符
CmdFlag_SS = 0;
if (0 != CmdLen_SS)
{
CmdLen_SS--;
SendData_SS(data);
}
break;
case '\t':// 制表符
CmdFlag_SS = 0;
if (CmdLen_SS < SIZECmd_SS)
{
CmdBuf_SS[CmdLen_SS++] = '\x20';
SendData_SS(data);
}
break;
default:
CmdFlag_SS = 0;
if (CmdLen_SS < SIZECmd_SS)
{
CmdBuf_SS[CmdLen_SS++] = data;
SendData_SS(data);
}
break;
}
}
return;
}
// 串口发送单字节
void SendData_SS(unsigned char data)
{
register atom wr;
wr = WrSendBuf_SS;
if (wr >= SIZESendBuf_SS)
{
RdSendBuf_SS = WrSendBuf_SS = 0;
}
SendBuf_SS[wr++] = data;
if (wr >= SIZESendBuf_SS)
{
wr = 0;
}
if (wr != RdSendBuf_SS)
{
WrSendBuf_SS = wr;
}
// 判别是否处于发送状态
if (0 == SendFlag_SS)
{
SendFlag_SS = 1;
// 触发串口发送
TrigSend_SS();
}
return;
}
// 发送字符串
void SendString_SS(unsigned char * str)
{
register unsigned int i;
register unsigned char data;
i = 0;
data = str[i];
while ('\0' != data)
{
SendData_SS(data);
i++;
}
return;
}
// 发送数据
void Sendf_SS(unsigned int len, unsigned char * pBuf)
{
register unsigned int i;
for (i=0; i<len; i++)
{
SendData_SS(pBuf[i]);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -