Newer
Older
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
#pragma once
#include <cstdint>
#include <QSerialPort>
#include <QByteArray>
#include <QTimer>
class Machine : public QObject
{
Q_OBJECT
enum class Command : uint8_t
{
kAddPoints = 0,
kDeletePoints,
kStopMove,
kStartMove,
kSetParam,
kGotoXY,
kMovedXdY,
kGotoNextPoint,
kSetNPoint,
kSetPos,
};
public:
#pragma pack(push, 1)
struct SlaveInfo
{
uint8_t sequence;
uint16_t status;
uint16_t spaceSize;
uint16_t freeSpace;
int16_t posX;
int16_t posY;
uint16_t nPoint;
};
#pragma pack(pop)
Machine(unsigned msTimeout, unsigned retries);
void connectPort(QString portname=QString());
void disconnectPort();
virtual ~Machine();
public slots:
signals:
void stateChanged(SlaveInfo info);
void connectionChanged(bool connected);
public:
bool ack();
void addPoints(const uint8_t* xys, unsigned points);
void deletePoints();
void stopMove();
void startMove();
void setParam();
void gotoXY(int16_t x, int16_t y);
void movedXdY(int16_t dx, int16_t dy);
void gotoNextPoint();
void setNPoint(uint16_t nPoint);
void setPos(int16_t x, int16_t y);
private:
void handleReadyRead();
void handleError(QSerialPort::SerialPortError serialPortError);
void handleAliveTimeout();
void sendCommand(Command cmd, uint8_t len=1);
void onMsg(uint8_t* buffer);
public:
SlaveInfo info;
private:
QSerialPort serial;
QTimer timerAlive;
QString portName;
bool connected;
uint8_t sequence;
uint8_t sequenceAck;
QByteArray msgBuffer;
QByteArray cmdBuffer;
unsigned msTimeout;
unsigned retries;
};