加入和上层控制器协议解析,可收命令和发送命令返回
This commit is contained in:
parent
818afa73a1
commit
b132ed8a57
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"algorithm": "cpp"
|
||||||
|
}
|
||||||
|
}
|
@ -13,7 +13,7 @@ platform = espressif32
|
|||||||
board = esp32doit-devkit-v1
|
board = esp32doit-devkit-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
--upload-port = COM[14]
|
upload-port = COM[14]
|
||||||
lib_deps =
|
lib_deps =
|
||||||
bogde/HX711@^0.7.5
|
bogde/HX711@^0.7.5
|
||||||
mathertel/OneButton@^2.0.3
|
mathertel/OneButton@^2.0.3
|
||||||
|
178
src/Serialcommand.cpp
Normal file
178
src/Serialcommand.cpp
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
#include "Serialcommand.h"
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
Serialcommand::Serialcommand()
|
||||||
|
{
|
||||||
|
// 连接控制器
|
||||||
|
Serial2.begin(115200);
|
||||||
|
// clear serialport
|
||||||
|
while (Serial2.read() >= 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
CmdState = 0;
|
||||||
|
bufferIndex = 0;
|
||||||
|
recv_flag = false;
|
||||||
|
outBuffer[0] = 0xff;
|
||||||
|
outBuffer[1] = 0xff;
|
||||||
|
}
|
||||||
|
Serialcommand::~Serialcommand()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Serialcommand::Uart_Command_Rev(void)
|
||||||
|
{
|
||||||
|
while (Serial2.available() > 0)
|
||||||
|
{
|
||||||
|
ch = Serial2.read();
|
||||||
|
// printf("rev:%d\n", ch);
|
||||||
|
switch (CmdState)
|
||||||
|
{
|
||||||
|
// 开始标志1
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
if (0xFF == ch)
|
||||||
|
{
|
||||||
|
CmdState = 1;
|
||||||
|
// printf("st:%d\n", CmdState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// 开始标志2
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
if (0xFF == ch)
|
||||||
|
{
|
||||||
|
bufferIndex = 0;
|
||||||
|
CmdState = 2;
|
||||||
|
// printf("st:%d\n", CmdState);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
CmdState = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// 取长度,准备收数据
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
datalen = ch;
|
||||||
|
// printf("datalen:%d\n", datalen);
|
||||||
|
if (datalen <= inbuffersize)
|
||||||
|
{
|
||||||
|
CmdState = 3;
|
||||||
|
bufferIndex = 0;
|
||||||
|
inBuffer[bufferIndex] = ch;
|
||||||
|
// printf("buf:%d:%d\n", bufferIndex, inBuffer[bufferIndex]);
|
||||||
|
}
|
||||||
|
else // 超长
|
||||||
|
{
|
||||||
|
CmdState = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// 收数据直到结束
|
||||||
|
case 3:
|
||||||
|
{
|
||||||
|
// printf("st3:%d:%d\n", bufferIndex, datalen);
|
||||||
|
|
||||||
|
if (bufferIndex <= datalen)
|
||||||
|
{
|
||||||
|
bufferIndex++;
|
||||||
|
inBuffer[bufferIndex] = ch;
|
||||||
|
// printf("buf:%d:%d\n", bufferIndex, inBuffer[bufferIndex]);
|
||||||
|
if (bufferIndex == datalen) // 收完了
|
||||||
|
{
|
||||||
|
CmdState = 0;
|
||||||
|
recv_flag = true;
|
||||||
|
bufferIndex = 0;
|
||||||
|
return; // 不收数据了,先出去解析
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint8_t Serialcommand::getsum(const uint8_t *buffer, size_t size)
|
||||||
|
{
|
||||||
|
uint16_t usum = 0;
|
||||||
|
uint8_t chksum = 0;
|
||||||
|
for (uint8_t i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
usum += buffer[i];
|
||||||
|
}
|
||||||
|
chksum = (uint8_t)usum & 0xff; // 取低8位
|
||||||
|
return chksum;
|
||||||
|
}
|
||||||
|
bool Serialcommand::checksum()
|
||||||
|
{
|
||||||
|
uint8_t bufsum = inBuffer[datalen]; // 校验和
|
||||||
|
uint8_t chksum = getsum(inBuffer, datalen - 1); // 不算sum字节
|
||||||
|
if (chksum != bufsum)
|
||||||
|
{
|
||||||
|
printf("sum fault :data_sum:0x%02X,add_sum:0x%02X\n", bufsum, chksum);
|
||||||
|
}
|
||||||
|
return bufsum == chksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Serialcommand::sendcmdret(uint8_t cmdret)
|
||||||
|
{
|
||||||
|
// 长度
|
||||||
|
outBuffer[2] = 0x02;
|
||||||
|
// 标识命令返回
|
||||||
|
outBuffer[3] = 0x01;
|
||||||
|
// 返回数据
|
||||||
|
outBuffer[4] = cmdret;
|
||||||
|
outBuffer[5] = getsum(outBuffer + 2, 3);
|
||||||
|
Serial2.write(outBuffer, 6);
|
||||||
|
}
|
||||||
|
void Serialcommand::getcommand()
|
||||||
|
{
|
||||||
|
Uart_Command_Rev();
|
||||||
|
// 收到有效数据
|
||||||
|
if (recv_flag)
|
||||||
|
{
|
||||||
|
// printf("rev end len:%d\n", datalen);
|
||||||
|
recv_flag = false;
|
||||||
|
// 处理数据
|
||||||
|
if (checksum())
|
||||||
|
{
|
||||||
|
uint8_t commandid = inBuffer[1];
|
||||||
|
printf("rev ok cid:0x%02X\n", commandid);
|
||||||
|
switch (commandid)
|
||||||
|
{
|
||||||
|
// 放钩
|
||||||
|
case 0x01:
|
||||||
|
{
|
||||||
|
sendcmdret(0); // 成功
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// 收钩
|
||||||
|
case 0x02:
|
||||||
|
{
|
||||||
|
sendcmdret(0); // 成功
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// 停止
|
||||||
|
case 0x03:
|
||||||
|
{
|
||||||
|
sendcmdret(0); // 成功
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// 查询当前状态
|
||||||
|
case 0x04:
|
||||||
|
{
|
||||||
|
sendcmdret(0); // 发送状态
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// 设置速度
|
||||||
|
case 0x05:
|
||||||
|
{
|
||||||
|
sendcmdret(0); // 成功
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sendcmdret(100); // 失败,未知命令
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memset(inBuffer, 0x00, sizeof(inBuffer));
|
||||||
|
}
|
||||||
|
}
|
24
src/Serialcommand.h
Normal file
24
src/Serialcommand.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
class Serialcommand
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
static const int inbuffersize = 20;
|
||||||
|
int CmdState; // 指令接收状态
|
||||||
|
uint8_t ch; // 临时单个字符存储
|
||||||
|
int datalen;
|
||||||
|
bool recv_flag; // 指令接收成功标志
|
||||||
|
uint8_t inBuffer[inbuffersize]; // 接收指令缓冲区
|
||||||
|
uint8_t bufferIndex;
|
||||||
|
static const int outbuffersize = 20;
|
||||||
|
uint8_t outBuffer[outbuffersize];
|
||||||
|
void Uart_Command_Rev(void);
|
||||||
|
uint8_t getsum(const uint8_t *buffer, size_t size);
|
||||||
|
bool checksum();
|
||||||
|
void sendcmdret(uint8_t cmdret);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Serialcommand(); // 构造函数
|
||||||
|
~Serialcommand(); // 析构函数
|
||||||
|
void getcommand();
|
||||||
|
};
|
11
src/config.h
Normal file
11
src/config.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//硬件接口定义文件
|
||||||
|
|
||||||
|
// 按钮
|
||||||
|
#define BTN_UP 23 // 收线开关 接线:BTN_UP---GND
|
||||||
|
#define BTN_DOWN 22 // 放线开关
|
||||||
|
#define BTN_CT 21 // 到顶检测开关
|
||||||
|
// LED
|
||||||
|
#define LED_TIP 19 // 指示灯 接线:LED_TIP---3.3V
|
||||||
|
// 称重传感器- HX711
|
||||||
|
#define LOADCELL_DOUT_PIN 2
|
||||||
|
#define LOADCELL_SCK_PIN 4
|
90
src/main.cpp
90
src/main.cpp
@ -1,16 +1,9 @@
|
|||||||
#include "HX711.h"
|
#include "HX711.h"
|
||||||
#include "OneButton.h"
|
#include "OneButton.h"
|
||||||
|
#include "Serialcommand.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
|
||||||
// 硬件引脚定义--------------------------
|
|
||||||
// 按钮
|
|
||||||
#define BTN_UP 23 // 收线开关 接线:BTN_UP---GND
|
|
||||||
#define BTN_DOWN 22 // 放线开关
|
|
||||||
#define BTN_CT 21 // 到顶检测开关
|
|
||||||
// LED
|
|
||||||
#define LED_TIP 19 // 指示灯 接线:LED_TIP---3.3V
|
|
||||||
// 称重传感器- HX711
|
|
||||||
#define LOADCELL_DOUT_PIN 2
|
|
||||||
#define LOADCELL_SCK_PIN 4
|
|
||||||
|
|
||||||
// 角度传感器
|
// 角度传感器
|
||||||
// 收放线电机控制
|
// 收放线电机控制
|
||||||
@ -21,6 +14,7 @@ OneButton button_up(BTN_UP, true);
|
|||||||
OneButton button_down(BTN_DOWN, true);
|
OneButton button_down(BTN_DOWN, true);
|
||||||
OneButton button_checktop(BTN_CT, true);
|
OneButton button_checktop(BTN_CT, true);
|
||||||
HX711 scale;
|
HX711 scale;
|
||||||
|
Serialcommand sercomm;
|
||||||
|
|
||||||
void upbtn_click();
|
void upbtn_click();
|
||||||
void downbtn_click();
|
void downbtn_click();
|
||||||
@ -33,12 +27,7 @@ void setup()
|
|||||||
// 调试串口
|
// 调试串口
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Starting PullupDevice...");
|
Serial.println("Starting PullupDevice...");
|
||||||
// 连接控制器
|
|
||||||
Serial2.begin(115200);
|
|
||||||
// clear serialport
|
|
||||||
while (Serial2.read() >= 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化按钮
|
// 初始化按钮
|
||||||
button_up.attachClick(upbtn_click);
|
button_up.attachClick(upbtn_click);
|
||||||
@ -85,6 +74,8 @@ void loop()
|
|||||||
button_checktop.tick();
|
button_checktop.tick();
|
||||||
button_down.tick();
|
button_down.tick();
|
||||||
button_up.tick();
|
button_up.tick();
|
||||||
|
sercomm.getcommand();
|
||||||
|
|
||||||
delay(10);
|
delay(10);
|
||||||
|
|
||||||
// Serial.print("PullUnits:\t");
|
// Serial.print("PullUnits:\t");
|
||||||
@ -109,70 +100,3 @@ void upbtn_click()
|
|||||||
{
|
{
|
||||||
Serial.println("upbtn_click");
|
Serial.println("upbtn_click");
|
||||||
}
|
}
|
||||||
|
|
||||||
const int bufnum = 20;
|
|
||||||
char buffer[bufnum];
|
|
||||||
const int sendbufnum = 5;
|
|
||||||
char sendbuffer[sendbufnum] = {(char)0xFF, (char)0xFF, (char)0x00, (char)0x00, (char)0x00};
|
|
||||||
int datareadpos = 0; // 数据读取位置
|
|
||||||
void getcommand()
|
|
||||||
{
|
|
||||||
datareadpos = 0;
|
|
||||||
int headpos = 0;
|
|
||||||
|
|
||||||
// 是否有数据,有就读bufnum个
|
|
||||||
//数据头2个字节FF FF
|
|
||||||
while (Serial2.available() > 0)
|
|
||||||
{
|
|
||||||
buffer[datareadpos] = Serial2.read();
|
|
||||||
|
|
||||||
if ((datareadpos > 1) && (buffer[datareadpos] == 0xFF) && (buffer[datareadpos - 1] == 0xFF))
|
|
||||||
headpos = datareadpos - 1;
|
|
||||||
datareadpos++;
|
|
||||||
delay(20);
|
|
||||||
// 读超过buff长度退出
|
|
||||||
if (datareadpos >= bufnum)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (datareadpos > 0)
|
|
||||||
{
|
|
||||||
printf("rev[%d]:", datareadpos);
|
|
||||||
for (int i = 0; i < datareadpos; i++)
|
|
||||||
{
|
|
||||||
printf("0x%02X,", (byte)buffer[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 是否读了bufnum个数据
|
|
||||||
if (((byte)buffer[headpos] == 0xFF) && ((byte)buffer[headpos + 1] == 0xFF))
|
|
||||||
{
|
|
||||||
byte sumbyte = (byte)(buffer[headpos + 2] + buffer[headpos + 3] + buffer[headpos + 4]) & 0xFF;
|
|
||||||
if ((byte)buffer[headpos + 5] == sumbyte)
|
|
||||||
{
|
|
||||||
int cmd_type = buffer[headpos + 2];
|
|
||||||
switch (cmd_type)
|
|
||||||
{
|
|
||||||
case 0x01: // 收线
|
|
||||||
/* code */
|
|
||||||
break;
|
|
||||||
case 0x02: // 放线
|
|
||||||
/* code */
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// clear serial buffer
|
|
||||||
|
|
||||||
while (Serial2.available() > 0)
|
|
||||||
{
|
|
||||||
Serial2.read();
|
|
||||||
delay(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < bufnum; i++)
|
|
||||||
{
|
|
||||||
buffer[i] = '\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user