-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino_source.ino
133 lines (123 loc) · 3.18 KB
/
Arduino_source.ino
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
#include <SoftwareSerial.h>
#include <Arduino.h>
#include "checksum.h"
//#include <SoftwareSerial.h>
//#include <Arduino.h>
#include <dummy.h>
#define PACKET_SEQ_LOC 2
#define K1_LOC_BYTE1 22
#define SL1_LOC_BYTE1 24
#define S1_LOC_BYTE1 26
#define S2_LOC_BYTE1 28
#define CGO3_RX_PIN 32
#define CGO3_TX_PIN 33
uint8_t initpacket[]={0xFE,0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x02,0x00,0x01,0x00,0x00};
uint8_t outputmsg[]={0xFE,0x1A,0x00,0x01,0x00,0x02,0x00,0x01,0x9D,0x0A,0xE6,0xFF,0xFD,0xFF,0x97,0x25,0x1F,0x00,0x01,0x00,0x00,0x00,0x02,0x08,0x56,0x0D,0x00,0x08,0xAB,0x02,0x88,0x08,0xF4,0x01,0x00,0x00};
uint8_t packet_seq = 0;
uint8_t k1_command[2];
uint8_t sl1_command[2];
uint8_t s1_command[2];
uint8_t s2_command[2];
SoftwareSerial CGO3Serial(CGO3_RX_PIN,CGO3_TX_PIN, false);
void setup() {
// put your setup code here, to run once:
uint8_t k;
Serial.begin(115200);
CGO3Serial.begin(115200);
delay(100);
while (CGO3Serial.available()<1) {
for (int i=0;i<5;i=i+1) {
updatemsg(initpacket);
Serial.println(".");
CGO3Serial.write(initpacket,13);
}
}
s1_command[0]=0x00;
s1_command[1]=0x08;
s2_command[0]=0x00;
s2_command[1]=0x08;
sl1_command[0]=0x00;
sl1_command[1]=0x08;
k1_command[0]=0x00;
k1_command[1]=0x08;
}
void loop() {
uint8_t k;
char t;
if (Serial.available() > 0) {
t=Serial.read();
switch (t) {
case 'c':
sl1_command[0]=0x02;
sl1_command[1]=0x08;
break;
case 'u':
sl1_command[0]=0xAB;
sl1_command[1]=0x02;
break;
case 'd':
sl1_command[0]=0x56;
sl1_command[1]=0x0d;
break;
case 'm':
k1_command[0]=0x02;
k1_command[1]=0x08;
break;
case 'r':
k1_command[0]=0xAB;
k1_command[1]=0x02;
break;
case 'l':
k1_command[0]=0x56;
k1_command[1]=0x0d;
break;
case 'a':
s1_command[0]=0xAB;
s1_command[1]=0x02;
break;
case 'v':
s1_command[0]=0x54;
s1_command[1]=0x0D;
break;
case 'f':
s2_command[0]=0xAB;
s2_command[1]=0x02;
break;
case 'g':
s2_command[0]=0x54;
s2_command[1]=0x0D;
break;
}
}
updatemsg(outputmsg);
CGO3Serial.write(outputmsg,outputmsg[1]+10);
}
void updatemsg(uint8_t* msgptr) {
uint8_t c;
uint16_t tempchecksum;
msgptr[PACKET_SEQ_LOC]=packet_seq;
if (msgptr[1]==0x1A) {
msgptr[K1_LOC_BYTE1]=k1_command[0];
msgptr[K1_LOC_BYTE1+1]=k1_command[1];
msgptr[SL1_LOC_BYTE1]=sl1_command[0];
msgptr[SL1_LOC_BYTE1+1]=sl1_command[1];
msgptr[S1_LOC_BYTE1]=s1_command[0];
msgptr[S1_LOC_BYTE1+1]=s1_command[1];
msgptr[S2_LOC_BYTE1]=s2_command[0];
msgptr[S2_LOC_BYTE1+1]=s2_command[1];
}
crc_init(&tempchecksum);
//length from packet + 10 header bytes - CRC
for (uint8_t j=1;j<msgptr[1]+8;j=j+1) {
crc_accumulate(msgptr[j],&tempchecksum);
}
//add the CRC_EXTRA Byte which seems to be 0
crc_accumulate(0,&tempchecksum);
msgptr[msgptr[1]+8]=tempchecksum & 0x00FF;
msgptr[msgptr[1]+9]=((tempchecksum >> 8)&0x00FF);
if (packet_seq==255) {
packet_seq=0;
} else {
packet_seq=packet_seq+1;
}
}