-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialClient.cpp
More file actions
85 lines (71 loc) · 1.78 KB
/
Copy pathserialClient.cpp
File metadata and controls
85 lines (71 loc) · 1.78 KB
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
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "assert.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <map>
/* baudrate settings are defined in <asm/termbits.h>, which is
included by <termios.h> */
#define BAUDRATE B115200
/* change this definition for the correct port */
#define MODEMDEVICE "/dev/ttyUSB1"
int main(int argc, char *argv[])
{
//Setup the serial
int fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {
printf("Error opening device\n");
perror(MODEMDEVICE);
return -1;
}
struct termios oldtio,newtio;
tcgetattr(fd,&oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
newtio.c_cflag = BAUDRATE; // | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = 1; //IGNPAR;
newtio.c_oflag = 0; //Raw Output
newtio.c_lflag = 0;
/*
now clean the modem line and activate the settings for the port
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
char buffer[1+1+(50*3)];
buffer[0] = 3; //Starts at 2
buffer[1] = 50;
for(int i=0; i<1; i++)
{
buffer[2+(i*3)] = 0;
buffer[2+(i*3)+1] = 0;
buffer[2+(i*3)+2] = 255;
}
for(int i=1; i<4; i++)
{
buffer[2+(i*3)] = 255;
buffer[2+(i*3)+1] = 0;
buffer[2+(i*3)+2] = 255;
}
for(int i=4; i<50; i++)
{
buffer[2+(i*3)] = 0;
buffer[2+(i*3)+1] = 255;
buffer[2+(i*3)+2] = 0;
}
getchar();
write(fd, buffer, 1+1+(50*3));
usleep(100000);
int ret = read(fd, buffer, 255);
buffer[ret] = 0;
printf("Got %i --%s--\n", ret, buffer);
/* restore the old port settings */
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
exit(0);
}