我的脚本是关于在启动时打开设备。
实际上,我认为不是关于代码错误,而是关于何时放置my_app 在/etc/rc3.d/my_app我没有在后台进程中运行它使它在循环中停留并且无法使用Ctrl + c / Ctrl + z来取消它,因为它在启动终端上运行。
有什么想法打断我的应用程序以退出无限循环? - 我认为是尝试直接访问数据存储并在etc / rc3.d上禁用/删除我的符号链接my_app但我不能这样做。
这是我的代码。
#include <stdio.h>
#include <fcntl.h> /* File Control Definitions */
#include <termios.h> /* POSIX Terminal Control Definitions*/
#include <unistd.h> /* UNIX Standard Definitions */
#include <errno.h> /* ERROR Number Definitions */
#include <termios.h>
#include <time.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define BAUDRATE B19200 // 19200 use for input r ,, 115200
unsigned char ClearScreen[] = {254,88};
void usbreset(){
FILE *fp;
char *command;
//char *command2;
//char boss[100];
char path[1035];
//commnad = "sudo su";
command = (char *) malloc(30);
//command2 = (char *) malloc(30);
command ="lsusb | grep 1b3d:012c";
char command2[100] = "./rest ";
fp = popen(command,"r");
while (fgets(path, sizeof(path)-1, fp) != NULL) {
// printf("\n %s", path);
}
printf("%zu", strlen(path));
char final[strlen(path)-1];
printf("%s\n", path);
printf("%zu \n", strlen(path));
int x=0;
while(x < strlen(path)-1){
printf("%x ",path[x]);
final[x] = path[x];
x++;
}
printf("%s\n",final);
printf("%zu\n",strlen(final) );
// final is out put of lsusb -t
char str[40] = " ";
strcpy(str,final);
const char s[2] = " "; // token split
char *token;
//char a[22];
char bus[3] = " ";
char dev[3] = " ";
/* get the first token */
token = strtok(str, s);
//printf("%c\n", token[0]);
/* walk through other tokens */
int count = 0;
while( token != NULL ) {
if(count == 1){
strcpy(bus,token);
} else if(count == 3){
strncpy(dev,token,3);
}
token = strtok(NULL, s);
count += 1;
}
char reset_command[100] = "/dev/bus/usb/";
strcat(reset_command,bus);
strcat(reset_command,"/");
strcat(reset_command,dev);
strcat(command2,reset_command);
printf("%s\n", command2 );
fp = popen(command2,"r");
fclose(fp);
}
int main(){
//FILE *openfile;
//openfile = fopen("logfile.txt","r+b");
int fd;
FILE *fp;
char *command;
char path[16];
command ="ls /dev/ttyUSB*";
fp = popen(command,"r");
while (fgets(path, sizeof(path)-1, fp) != NULL) {
// printf("\n %s", path);
}
char final[strlen(path)-1];
int x=0;
while(x < strlen(path)-1){
//printf("%x ",path[x]);
final[x] = path[x];
x++;
}
fclose(fp);
struct termios term;
fd = open(final, O_RDWR | O_NOCTTY | O_NONBLOCK);
if(fd == 1){
printf("Error! in Opening ttyUSB0\n");
}else{
printf("Device Detected!\n");
printf("Opening Successfully\n");
}
term.c_cflag = BAUDRATE | CS8 | CSTOPB | CLOCAL | CREAD;
term.c_iflag = 0;
term.c_oflag = 0;
term.c_lflag = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&term);
write(fd,ClearScreen,sizeof(ClearScreen));
//int lcd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
//fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
int err_cnt = 0;
printf("start\n");
//return 0;
while(1){
char buffer[100];
ssize_t length = read(fd, &buffer, sizeof(buffer));
if(length == -1){
//printf("Error reading from serial port\n");
//printf("%zu\n",length);
printf("%s\n",buffer );
err_cnt += 1;
printf("%d\n",err_cnt );
usbreset();
}else{
err_cnt = 0;
printf("%c", buffer[length]);
}
buffer[length] = '\0';
}
return 0;
}