/* !<3 minicom */ #include #include #include #include #include #include //#include #include 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 */