-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodstrgen.cpp
88 lines (61 loc) · 2.07 KB
/
modstrgen.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
#include "modstrgen.h"
ModStrGen::ModStrGen()
{
}
unsigned char* ModStrGen::prepReadCommand(ParameterBox in, unsigned char DriveAddress)
{
unsigned char* modstr;
modstr = new unsigned char [8];
modstr[0] = DriveAddress;
modstr[1] = 0x04;
unsigned char downAdd = in.getAddr();
unsigned char upAdd = (unsigned char)((int)downAdd + (in.getByteCount()/2) - 1);
// qDebug() << "lowAdd: " << downAdd;
// qDebug() << "upAdd: " << upAdd;
modstr[2] = up8_fromushort(downAdd);
modstr[3] = down8_fromushort(downAdd);
unsigned char *scanrange = scanranger(upAdd, downAdd);
modstr[4] = scanrange[0];
modstr[5] = scanrange[1];
unsigned short crcref = crc16(modstr, 6);
unsigned char *crc = ushort_touchar(crcref);
modstr[6] = crc[0];
modstr[7] = crc[1];
delete scanrange;
delete crc;
return modstr;
}
unsigned char* ModStrGen::prepWriteCommand(MotorParameter in, unsigned char DriveAddress, int* length)
{
qDebug() << "ModStrGen::prepWriteCommand(): ";
unsigned char downAdd = in.getAddr();
qDebug() << "in.getAddr() result: " << in.getAddr();
qDebug() << "downAdd: " << downAdd;
unsigned char upAdd = (unsigned char)((int) downAdd + in.getSize() - 1);
qDebug() << "upAdd: " << upAdd;
unsigned char *scanrange = scanranger(upAdd, downAdd);
int datalen;
unsigned char* data = in.assembleWritableTransient(&datalen);
int cmdlen = datalen + 9;
*length = cmdlen;
unsigned char* modstr = new unsigned char[cmdlen];
modstr[0] = DriveAddress;
modstr[1] = 0x10;
modstr[2] = up8_fromushort(downAdd);
modstr[3] = down8_fromushort(downAdd);
modstr[4] = scanrange[0];
modstr[5] = scanrange[1];
modstr[6] = (unsigned char) datalen;
for (int i = 0; i < datalen; i++)
{
modstr[7 + i] = data[i];
}
unsigned short crcref = crc16(modstr, cmdlen - 2);
unsigned char *crc = ushort_touchar(crcref);
modstr[7 + datalen] = crc[0];
modstr[7 + datalen + 1] = crc[1];
delete data;
delete scanrange;
delete crc;
return modstr;
}