-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patharduinoSendReceive_multiple.ino
70 lines (59 loc) · 1.67 KB
/
arduinoSendReceive_multiple.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
// Create a union to easily convert float to byte
typedef union{
float number;
uint8_t bytes[4];
} FLOATUNION_t;
// Create the variables you want to receive
FLOATUNION_t myValue1;
FLOATUNION_t myValue2;
FLOATUNION_t myValue3;
FLOATUNION_t myValue4;
FLOATUNION_t myValue5;
FLOATUNION_t myValue6;
// Create the variables to send
FLOATUNION_t send1;
FLOATUNION_t send2;
FLOATUNION_t send3;
void setup() {
// initialize serial, use the same boudrate in the Simulink Config block
Serial.begin(115200);
}
void loop(){
// Get the floats from serial
myValue1.number = getFloat(); // Give your float a value
myValue2.number = getFloat(); // Give your float a value
myValue3.number = getFloat(); // Give your float a value
myValue4.number = getFloat(); // Give your float a value
myValue5.number = getFloat(); // Give your float a value
myValue6.number = getFloat(); // Give your float a value
// Do whatever you want here
// Send some variables back
send1.number = myValue1.number+myValue2.number;
send2.number = myValue3.number+myValue4.number;
send3.number = myValue5.number+myValue6.number;
// Print header: Important to avoid sync errors!
Serial.write('A');
// Print float data
for (int i=0; i<4; i++){
Serial.write(send1.bytes[i]);
}
for (int i=0; i<4; i++){
Serial.write(send2.bytes[i]);
}
for (int i=0; i<4; i++){
Serial.write(send3.bytes[i]);
}
// Print terminator
Serial.print('\n');
// Use the same delay in the Serial Receive block
delay(50);
}
float getFloat(){
int cont = 0;
FLOATUNION_t f;
while (cont < 4 ){
f.bytes[cont] = Serial.read() ;
cont = cont +1;
}
return f.number;
}