-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialManager.h
More file actions
109 lines (86 loc) · 3.63 KB
/
SerialManager.h
File metadata and controls
109 lines (86 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once
#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QByteArray>
class SerialManager : public QObject {
Q_OBJECT
public:
explicit SerialManager(QObject *parent = nullptr);
~SerialManager();
QList<QSerialPortInfo> availablePorts() const;
bool open(const QString &portName, qint32 baudRate = 115200);
void close();
bool isOpen() const;
// ===== 下行发送 =====
// CAN (Channel 0x01)
void sendCanFrame(uint32_t id, bool extended, bool rtr, const QByteArray &data);
// USART (Channel 0x10 / 0x11)
void sendUsartData(uint8_t channel, const QByteArray &data);
// SPI1 (Channel 0x20)
void sendSpiRequest(const QByteArray &writeData, uint8_t readLen);
// I2C (Channel 0x30 / 0x31)
void sendI2cWrite(uint8_t channel, uint8_t devAddr, const QByteArray &data);
void sendI2cRead(uint8_t channel, uint8_t devAddr, uint8_t readLen);
void sendI2cMemWrite(uint8_t channel, uint8_t devAddr, uint32_t memAddr,
uint8_t memAddrLen, const QByteArray &data);
void sendI2cMemRead(uint8_t channel, uint8_t devAddr, uint32_t memAddr,
uint8_t memAddrLen, uint8_t readLen);
// ADC (Channel 0x40)
void sendAdcStart();
void sendAdcStop();
void sendAdcSetRate(uint16_t psc, uint16_t per);
// System (Channel 0xF0)
void sendSetUsart1Baud(uint32_t baud);
void sendSetUsart2Baud(uint32_t baud);
void sendSetCanBaud(uint32_t baud);
void sendSetI2c1Speed(uint32_t speed);
void sendSetI2c2Speed(uint32_t speed);
void sendSetSpi1Presc(uint8_t presc);
void sendQueryInfo();
signals:
void portOpened(const QString &portName);
void portClosed();
void errorOccurred(const QString &error);
// ===== 上行接收 =====
void rawDataReceived(const QByteArray &data);
// CAN (Channel 0x01)
void canFrameReceived(uint32_t id, bool extended, bool rtr, const QByteArray &data);
// USART (Channel 0x10 / 0x11)
void usartDataReceived(uint8_t channel, const QByteArray &data);
// SPI1 (Channel 0x20)
void spiResponseReceived(uint8_t result, const QByteArray &readData);
// I2C (Channel 0x30 / 0x31)
void i2cResponseReceived(uint8_t channel, uint8_t result, const QByteArray &readData);
// ADC (Channel 0x40)
void adcDataReceived(uint16_t seqNum, const QByteArray &rawData);
// System Status (Channel 0xF1)
void deviceInfoReceived(uint8_t verMajor, uint8_t verMinor, uint8_t chCount);
void sysCmdResult(uint8_t cmd, uint8_t result);
private slots:
void onReadyRead();
void onError(QSerialPort::SerialPortError error);
private:
void processBuffer();
void compactBuffer();
QByteArray buildFrame(uint8_t channel, const QByteArray &payload);
void handleUplink(uint8_t channel, const QByteArray &payload);
// 帧格式常量
static constexpr uint8_t HEADER_DOWN = 0xAA;
static constexpr uint8_t HEADER_UP = 0xBB;
static constexpr int FRAME_HEADER_SIZE = 4; // Header + Channel + LenL + LenH
static constexpr int COMPACT_THRESHOLD = 4096; // 累积偏移超过此值时压缩缓冲区
// 通道定义
static constexpr uint8_t CH_CAN = 0x01;
static constexpr uint8_t CH_USART1 = 0x10;
static constexpr uint8_t CH_USART2 = 0x11;
static constexpr uint8_t CH_SPI1 = 0x20;
static constexpr uint8_t CH_I2C1 = 0x30;
static constexpr uint8_t CH_I2C2 = 0x31;
static constexpr uint8_t CH_ADC = 0x40;
static constexpr uint8_t CH_SYS_CMD = 0xF0;
static constexpr uint8_t CH_SYS_STS = 0xF1;
QSerialPort *m_serial;
QByteArray m_buffer;
int m_readPos = 0;
};