gistfile1.txt
· 5.2 KiB · Text
Raw
/* !<3 minicom */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <termios.h>
//#include <errno.h>
#include <unistd.h>
char* fixstrings_old(char* str) {
char *pr = str, *pw = str;
while (*pr) {
*pw = *pr++;
pw += (*pw != 13);
}
// *pw = '\0';
return str;
}
char *fixstrings (char *s) {
char *p1;
for (p1=s; *p1; p1++) {
if (*p1 == 10){ // <---- Error here
*p1 == '1';} // <---- And here...
}
}
/* int
set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf ("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf ("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
void
set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf ("error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
printf ("error %d setting term attributes", errno);
}
*/
// set_interface_attribs (mamed, B9600, 0);
// set_blocking (mamed, 0);
int main(int argc, char *argv[])
{
if ( argc < 3 ) { printf("Usage: %s PORT COMMAND\n or %s PORT COMMAND TIMEOUT [PATTERN]\n\n TIMEOUT can be \n 0 wait indefinitely for OK/ERROR, \n -1 wait indefinitely for OK, \n -11 wait indefinitely for custom pattern\n or any value between 1 and 2147483647 in ms.\n",argv[0],argv[0]); return; }
// int mamed = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY); //
int mamed = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK);
struct termios tty;
cfsetospeed (&tty, (speed_t)B115200);
cfsetispeed (&tty, (speed_t)B115200);
/* Setting other Port Stuff */
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tty.c_oflag &= OCRNL;
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 20;
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush( mamed, TCIFLUSH );
tcsetattr ( mamed, TCSANOW, &tty );
fd_set set;
char* strana ;
struct timeval timeout;
int owait=0;
char* opattern="\r\nOK\r\n";
if ( argc > 3 ) {
owait=atoi(argv[3]);
if ( owait < 1 ) {
timeout.tv_usec = 0;
timeout.tv_sec = 3600;
if ( owait == -11 ) opattern=argv[4];
}
else {
timeout.tv_usec = owait*1000;
timeout.tv_sec = 0;
}
}
else {
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
}
// printf("Timeout: %d ms\n",timeout.tv_usec/1000);
char reslt[255];
char* command= argv[2];
command = strcat (command,"\r\n");
int nowread=0;
if (mamed == -1) { printf("error: port is not port..."); } else fcntl(mamed, F_SETFL, 0);
FD_ZERO(&set);
FD_SET(mamed, &set);
if ( write(mamed, command , strlen(argv[2]))+1 < 0 ) printf("write port failed\n") ; //else printf("write port ok %s\n",command);
nowread=1;
while(nowread>0) {
nowread = read(mamed,reslt,255);
reslt[nowread]=0;
printf("%s",reslt);
switch(owait) {
case 0:
if ( strstr(reslt,"\r\nERROR") != NULL ) nowread=0;
case -11 ... -1 :
if ( strstr(reslt,opattern) != NULL ) nowread=0;
break;
default:
if ( strstr(reslt,"\r\nERROR") != NULL ) nowread=0;
if ( strstr(reslt,opattern) != NULL ) nowread=0;
if ( select(mamed + 1, &set, NULL, NULL, &timeout) == 0 ) nowread=0;
}
}
close (mamed);
}
/* Написала и взбесилась .
28I2015
Пост ненависти к minicom
Редакция вторая,
улучшенная.
20IV2016
*/
1 | /* !<3 minicom */ |
2 | #include <stdio.h> |
3 | #include <fcntl.h> |
4 | #include <string.h> |
5 | #include <sys/types.h> |
6 | #include <sys/time.h> |
7 | #include <termios.h> |
8 | //#include <errno.h> |
9 | #include <unistd.h> |
10 | |
11 | char* fixstrings_old(char* str) { |
12 | char *pr = str, *pw = str; |
13 | while (*pr) { |
14 | *pw = *pr++; |
15 | pw += (*pw != 13); |
16 | } |
17 | // *pw = '\0'; |
18 | return str; |
19 | } |
20 | |
21 | char *fixstrings (char *s) { |
22 | |
23 | char *p1; |
24 | |
25 | for (p1=s; *p1; p1++) { |
26 | if (*p1 == 10){ // <---- Error here |
27 | *p1 == '1';} // <---- And here... |
28 | } |
29 | } |
30 | |
31 | /* int |
32 | set_interface_attribs (int fd, int speed, int parity) |
33 | { |
34 | struct termios tty; |
35 | memset (&tty, 0, sizeof tty); |
36 | if (tcgetattr (fd, &tty) != 0) |
37 | { |
38 | printf ("error %d from tcgetattr", errno); |
39 | return -1; |
40 | } |
41 | |
42 | cfsetospeed (&tty, speed); |
43 | cfsetispeed (&tty, speed); |
44 | |
45 | tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars |
46 | // disable IGNBRK for mismatched speed tests; otherwise receive break |
47 | // as \000 chars |
48 | tty.c_iflag &= ~IGNBRK; // disable break processing |
49 | tty.c_lflag = 0; // no signaling chars, no echo, |
50 | // no canonical processing |
51 | tty.c_oflag = 0; // no remapping, no delays |
52 | tty.c_cc[VMIN] = 0; // read doesn't block |
53 | tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout |
54 | |
55 | tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl |
56 | |
57 | tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, |
58 | // enable reading |
59 | tty.c_cflag &= ~(PARENB | PARODD); // shut off parity |
60 | tty.c_cflag |= parity; |
61 | tty.c_cflag &= ~CSTOPB; |
62 | tty.c_cflag &= ~CRTSCTS; |
63 | |
64 | if (tcsetattr (fd, TCSANOW, &tty) != 0) |
65 | { |
66 | printf ("error %d from tcsetattr", errno); |
67 | return -1; |
68 | } |
69 | return 0; |
70 | } |
71 | |
72 | void |
73 | set_blocking (int fd, int should_block) |
74 | { |
75 | struct termios tty; |
76 | memset (&tty, 0, sizeof tty); |
77 | if (tcgetattr (fd, &tty) != 0) |
78 | { |
79 | printf ("error %d from tggetattr", errno); |
80 | return; |
81 | } |
82 | |
83 | tty.c_cc[VMIN] = should_block ? 1 : 0; |
84 | tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout |
85 | |
86 | if (tcsetattr (fd, TCSANOW, &tty) != 0) |
87 | printf ("error %d setting term attributes", errno); |
88 | } |
89 | */ |
90 | |
91 | // set_interface_attribs (mamed, B9600, 0); |
92 | // set_blocking (mamed, 0); |
93 | |
94 | int main(int argc, char *argv[]) |
95 | { |
96 | if ( argc < 3 ) { printf("Usage: %s PORT COMMAND\n or %s PORT COMMAND TIMEOUT [PATTERN]\n\n TIMEOUT can be \n 0 wait indefinitely for OK/ERROR, \n -1 wait indefinitely for OK, \n -11 wait indefinitely for custom pattern\n or any value between 1 and 2147483647 in ms.\n",argv[0],argv[0]); return; } |
97 | // int mamed = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY); // |
98 | int mamed = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK); |
99 | |
100 | struct termios tty; |
101 | cfsetospeed (&tty, (speed_t)B115200); |
102 | cfsetispeed (&tty, (speed_t)B115200); |
103 | |
104 | /* Setting other Port Stuff */ |
105 | tty.c_cflag |= (CLOCAL | CREAD); |
106 | tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
107 | tty.c_oflag &= OCRNL; |
108 | tty.c_cc[VMIN] = 0; |
109 | tty.c_cc[VTIME] = 20; |
110 | |
111 | /* Make raw */ |
112 | cfmakeraw(&tty); |
113 | |
114 | /* Flush Port, then applies attributes */ |
115 | tcflush( mamed, TCIFLUSH ); |
116 | tcsetattr ( mamed, TCSANOW, &tty ); |
117 | |
118 | |
119 | fd_set set; |
120 | char* strana ; |
121 | struct timeval timeout; |
122 | int owait=0; |
123 | char* opattern="\r\nOK\r\n"; |
124 | if ( argc > 3 ) { |
125 | owait=atoi(argv[3]); |
126 | if ( owait < 1 ) { |
127 | timeout.tv_usec = 0; |
128 | timeout.tv_sec = 3600; |
129 | if ( owait == -11 ) opattern=argv[4]; |
130 | } |
131 | else { |
132 | timeout.tv_usec = owait*1000; |
133 | timeout.tv_sec = 0; |
134 | } |
135 | } |
136 | else { |
137 | timeout.tv_sec = 0; |
138 | timeout.tv_usec = 100000; |
139 | } |
140 | // printf("Timeout: %d ms\n",timeout.tv_usec/1000); |
141 | char reslt[255]; |
142 | char* command= argv[2]; |
143 | command = strcat (command,"\r\n"); |
144 | int nowread=0; |
145 | if (mamed == -1) { printf("error: port is not port..."); } else fcntl(mamed, F_SETFL, 0); |
146 | FD_ZERO(&set); |
147 | FD_SET(mamed, &set); |
148 | if ( write(mamed, command , strlen(argv[2]))+1 < 0 ) printf("write port failed\n") ; //else printf("write port ok %s\n",command); |
149 | nowread=1; |
150 | while(nowread>0) { |
151 | nowread = read(mamed,reslt,255); |
152 | reslt[nowread]=0; |
153 | printf("%s",reslt); |
154 | switch(owait) { |
155 | case 0: |
156 | if ( strstr(reslt,"\r\nERROR") != NULL ) nowread=0; |
157 | case -11 ... -1 : |
158 | if ( strstr(reslt,opattern) != NULL ) nowread=0; |
159 | break; |
160 | default: |
161 | if ( strstr(reslt,"\r\nERROR") != NULL ) nowread=0; |
162 | if ( strstr(reslt,opattern) != NULL ) nowread=0; |
163 | if ( select(mamed + 1, &set, NULL, NULL, &timeout) == 0 ) nowread=0; |
164 | } |
165 | } |
166 | close (mamed); |
167 | } |
168 | /* Написала и взбесилась . |
169 | 28I2015 |
170 | |
171 | Пост ненависти к minicom |
172 | Редакция вторая, |
173 | улучшенная. |
174 | 20IV2016 |
175 | */ |
176 |