-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlyc.c
227 lines (183 loc) · 4.77 KB
/
lyc.c
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include "rxvx700.h" /* Yamaha RX-Vx700 protocol codes */
#define STX 0x02
#define ETX 0x03
#define DC1 0x11
int fd;
//Initialize serial port
int init_port()
{
int portstatus = 0;
struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 9600...'textB' un
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and setSTX local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//options.c_cflag |= SerialDataBitsInterp(8); /* CS8 - Selects 8 data bits */
options.c_cflag &= ~CRTSCTS; // disable hardware flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable XON XOFF (for transmit and receive)
//options.c_cflag |= CRTSCTS; /* enable hardware flow control */
options.c_cc[VMIN] = 0; //min carachters to be read
options.c_cc[VTIME] = 0; //Time to wait for data (tenths of seconds)
// Set the new options for the port...
//tcsetattr(fd, TCSANOW, &options);
//Set the new options for the port...
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &options)==-1)
{
perror("On tcsetattr:");
portstatus = -1;
}
else
portstatus = 1;
return portstatus;
}
/*
* send command to reciver
*/
void send_cmd(const char *n) {
int len = strlen(n);
// create command
char cmd[len + 2];
cmd[0]=STX;
cmd[len + 1]=ETX;
// copy string to char array
int i;
for (i = 0; i < strlen(n); i++) {
cmd[i + 1] = n[i];
}
/*
// debug
printf("CMD:");
for (i = 0; i < sizeof(cmd); i++) {
printf("%c|", cmd[i]);
}
printf("\n");
*/
write(fd, cmd, sizeof(cmd));
}
/*
* send init command to reciver
*/
void init_cmd() {
char b[5];
b[0]=DC1;
b[1]=0;
b[2]=0;
b[3]=1;
b[4]=ETX;
int n = write(fd, b, sizeof(b));
}
/*
* open serial port
*/
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyUSB0 --- \n");
} else {
fcntl(fd, F_SETFL, 0);
}
return (fd);
}
/*
* decode command
*/
int run_cmd(char *cmd1, char *cmd2) {
int l = 0;
// check parameter
if(cmd1 != NULL) {
l += strlen(cmd1);
}
if(cmd2 != NULL) {
l += strlen(cmd2);
}
// put parameter together
char c[l + 2];
strcpy(c, cmd1);
if(cmd2 != NULL) {
strcat(c, " ");
strcat(c, cmd2);
}
int i;
int size = (int)(sizeof(cmd) / sizeof(*cmd));
for(i = 0; i < size; i++) {
// search command
if(strcmp(c, cmd[i][0]) == 0) {
// todo calculate volume
// int i = (int)strtol(cmd[i][1], (char **)NULL, 10);
char code[strlen(cmd[i][1]) + 1];
strcpy(code, cmd[i][1]);
char *pch;
pch = strtok(code, " ");
while (pch != NULL) {
send_cmd(pch);
pch = strtok(NULL, " ");
}
return 0;
}
}
return 1;
}
/*
* main
*/
int main(int argc, char **argv) {
if(argc == 1) {
return 1;
}
fd = open_port();
if(fd == -1) {
printf("Error opening serial port\n");
} else {
if(init_port() == -1) {
sleep(.5);
printf("Error initializing serial port\n");
close(fd);
return 0;
}
sleep(.5);
init_cmd();
/*
// todo: recive response from reciver
if (n < 0) {
fputs("write() of X bytes failed!\n", stderr);
} else {
printf("Successfully wrote 8 bytes\n");
sleep(.5);
char buffer[200];
int n = read(fd, buffer, sizeof(buffer));
sleep(.5);
if (n < 0) {
fputs("read failed!\n", stderr);
} else {
printf("Successfully read from serial port -- %s\n", buffer);
}
}
*/
sleep(.5);
if(run_cmd(argv[1], argv[2]) == 1) {
printf("Command not found\n");
}
sleep(.5);
close(fd);
}
return 0;
}