-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsender.cpp
224 lines (197 loc) · 5.08 KB
/
sender.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "sender.h"
Sender::Sender(QObject *parent) : QObject(parent)
{
//Initial values
currentLine=0;
totalLineNum=0;
baudrate=115200;
resendNum = 0;
resending = false;
sendingChecksum=false;
paused=false;
sending=false;
readyReceive = false;
printer = new QSerialPort(this);
sendTimer = new QTimer(this);
//Fetch settings
QSettings settings(this);
sendTimer->setInterval(settings.value("core/senderinterval", 2).toInt());
sendingChecksum = settings.value("core/checksums", 0).toBool();
dtr = settings.value("core/dtr", 1).toBool();
flowcontrol = settings.value("core/flowcontrol", 0).toInt();
sendTimer->start();
connect(printer, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(receivedError(QSerialPort::SerialPortError)));
connect(printer, &QSerialPort::readyRead, this, &Sender::receivedData);
connect(sendTimer, &QTimer::timeout, this, &Sender::sendNext);
}
Sender::~Sender()
{
closePort();
sendTimer->stop();
}
//Mainloop of sending
void Sender::sendNext()
{
if(printer->isWritable() && readyReceive)
{
//Checksums
if(sendingChecksum && resending)
{
if(resendNum < sentCommands.size())
{
sendLine(sentCommands.at(resendNum));
resendNum++;
}
else if(resendNum == sentCommands.size())
{
resending = false;
resendNum = 0;
}
else if(resendNum > sentCommands.size())
{
sendLine("M110 N0");
totalLineNum = 0;
resendNum = 0;
sentCommands.clear();
}
return;
}
if(!userCommands.isEmpty()) //Inject user command
{
sendLine(userCommands.dequeue());
readyReceive = false;
return;
}
else if(sending && !paused) //Send line of gcode
{
FileProgress p;
if(currentLine >= gcode.size()) //check if we are at the end of array
{
sending = false;
currentLine = 0;
if(sendingChecksum) sendLine("M110 N0");
p.P = gcode.size();
p.T = gcode.size();
emit reportProgress(p);
return;
}
sendLine(gcode.at(currentLine));
currentLine++;
readyReceive=false;
p.P = currentLine;
p.T = gcode.size();
emit reportProgress(p);
}
}
}
bool Sender::sendLine(QString line)
{
sentCommands.clear();
if(printer->isOpen())
{
if(sendingChecksum)
{
if(line.contains("M110")) totalLineNum = 0;
sentCommands.append(line);
//Checksum algorithm from RepRap wiki
line = "N"+QString::number(totalLineNum)+line+"*";
int cs = 0;
for(int i = 0; line.at(i) != '*'; i++) cs = cs ^ line.at(i).toLatin1();
cs &= 0xff;
line += QString::number(cs);
totalLineNum++;
}
if(printer->write(line.toUtf8()+'\n')) return true;
else return false;
}
else return false;
}
void Sender::openPort(QSerialPortInfo i)
{
printer->setPort(i);
if(!printer->isOpen() && printer->open(QIODevice::ReadWrite))
{
//Moved here to be compatible with Qt 5.2.1
printer->setDataTerminalReady(dtr);
if(!printer->setBaudRate(baudrate))
emit baudrateSetFailed(baudrate);
printer->setFlowControl(static_cast<QSerialPort::FlowControl>(flowcontrol));
}
readyReceive = true;
}
void Sender::closePort()
{
if(printer->isOpen()) printer->close();
}
void Sender::startPrinting()
{
currentLine = 0;
paused = false;
sending = true;
}
void Sender::stopPrinting()
{
currentLine = 0;
paused = false;
sending = false;
}
void Sender::pause(bool p)
{
paused = p;
}
void Sender::setBaudrate(int b)
{
baudrate = b;
}
void Sender::setFile(QVector <QString> f)
{
gcode = f;
}
void Sender::injectCommand(QString command)
{
if(!userCommands.contains(command)) userCommands.enqueue(command);
}
void Sender::receivedOkWait()
{
readyReceive = true;
}
void Sender::receivedOkNum(int)
{
readyReceive = true;
}
void Sender::receivedStart()
{
readyReceive = true;
}
void Sender::flushInjectionBuffer()
{
userCommands.clear();
}
void Sender::receivedResend(int r)
{
if(sendingChecksum)
{
resending = true;
resendNum = r;
}
else currentLine--;
}
void Sender::receivedData()
{
if(printer->canReadLine())
{
QByteArray data = printer->readLine();
if(data == "") return;
emit dataReceived(data);
//Yeah, yeah, I know. This class is called "Sender", but checking this here is faster.
if(data.startsWith("ok") || data.startsWith("wait")) readyReceive=true;
}
}
void Sender::receivedError(QSerialPort::SerialPortError error)
{
if(error > 0)
{
closePort();
emit errorReceived(error);
}
}