-
Notifications
You must be signed in to change notification settings - Fork 0
/
w8s2p3-pi.cpp
82 lines (65 loc) · 1.6 KB
/
w8s2p3-pi.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include "serial.h"
typedef struct
{
// Uncomment the line below for step 7 of activity 4.
char c;
int32_t x;
int32_t y;
} TData;
// Replace with port name of the Arduino
#define PORT_NAME "/dev/ttyACM0"
// Sleep time to wait for Arduino to boot up
#define SLEEP_TIME 2
int main()
{
TData test;
startSerial(PORT_NAME, B9600, 8, 'N', 1, 5);
// The Arduino reboots when you connect to its
// serial port. We sleep 2 seconds to wait for it
// to finish rebooting.
printf("Sleeping %d seconds to wait for Arduino to boot\n",
SLEEP_TIME);
sleep(SLEEP_TIME);
while(1)
{
printf("Sending start character\n");
char ch='s';
serialWrite(&ch, sizeof(ch));
// Now read in the data structure sent
int len;
char buffer[128];
char tmpHold[128];
int count=0;
int targetSize=0;
// Read port
len = serialRead(buffer);
// First byte is the size
targetSize=buffer[0];
printf("Size of TData on this machine is %d bytes\n", sizeof(TData));
printf("Size of TData on the Arduino is %d bytes\n", targetSize);
int i;
for(i=1; i<len; i++)
tmpHold[count++] = buffer[i];
while(count<targetSize)
{
len = serialRead(buffer);
if(len > 0)
{
memcpy(&tmpHold[count], buffer, len);
count+=len;
}
}
// Now we convert to our data structure
memcpy(&test, tmpHold, sizeof(TData));
printf("x received is %d\n", test.x);
printf("y received is %d\n", test.y);
// Uncomment the line below for Step 7 of Activity 4
// in Week 8 Studio 2.
printf("c received is %c\n", test.c);
sleep(1);
}
}