⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 com.c

📁 linux下的串口调试软件。 可惜
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <curses.h>#include <sys/types.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <sys/wait.h>#include <fcntl.h>#include <termios.h>#include <errno.h>#include <string.h>#include <signal.h>#include <pthread.h>#include <semaphore.h>//波特率设定#define BAUDRATE B115200#define MYLINES 32767UL//定义正确的序列埠 #define COM1 "/dev/ttyUSB0"#define FALSE 0#define TRUE 1#define MAX_RECEIVE LINES*COLSvolatile int STOP=FALSE; volatile int STOP1=FALSE; volatile int CEASE = 0; pthread_t a_thread, b_thread;sem_t bin_sem, bin_sem2, bin_sem3;int sent_end;int fd, fds, fdr, fdk;size_t res, ress, pres, pres1, sres;struct termios oldtio, newtio;unsigned char received[1000];//unsigned  char send[256]={0x68, 0x05, 0x05, 0x68, 0x0c, 0xae, 0xc6, 0x00, 0x01, 0x81, 0x16, '\0'};unsigned  char send[256]={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, '\0'};unsigned int i, ii, j, k, num = 0;FILE *sent, *receive, *comstreamr, *comstreams;void *thread1_result, *thread2_result;int lines = 0, cols = 0, colss = 0, line_big, col_big, base_line, end_line, mylines = 0,  page = 1; char u_pressed = 0;WINDOW *p1_window_ptr;WINDOW *p2_window_ptr;WINDOW *pad_window_ptr;void *thread_function1(void *arg){	  	  while(1)	  {			fdk = getch();			if(fdk == 's')				  CEASE = 1;			else if(fdk == 'r')				  CEASE = 0;			else if(fdk == 'S')			{				  STOP = TRUE;				  STOP1 = TRUE;				  /*if(ii != 0)				  {						ii = 0;						base_line = (page - 1) * (LINES - 5);						for(line_big = 0; line_big <= lines; line_big++)						{							  for(col_big = 0 ; col_big <= cols; col_big++)							  {									mvwprintw(pad_window_ptr, base_line + line_big, col_big, "%02x", received[ii]);									ii++;							  }						}				  }	*/				  mylines = mylines -lines;				  end_line = mylines;				  break;			}	  }	  pthread_exit("The sent is always stop!");}void *thread_function(void *arg){	  pad_window_ptr = newpad(MYLINES, COLS);	  p1_window_ptr = newwin(LINES - 5, COLS, 1, 0);	  ii = 0;	  mvwprintw(stdscr, 0, 0, "Received data:" );	  mvwprintw(stdscr, 0, 15, "This is the %05dth pages!", page);	  wrefresh(stdscr);	  while (STOP1==FALSE)	  {			//sem_wait(&bin_sem);			/*for(ii = 0; ii < COLS; ii++, cols += 4)			{				  res = read(fdr, received + ii, 1);				  fprintf(receive,"%#02x ", received[ii]);				  if(cols >= COLS)				  {						cols = 0;						lines++;						mylines++;						if(lines > LINES - 6)						{	 							  lines = 0;							  page++;							  werase(p1_window_ptr);							  wrefresh(p1_window_ptr);							  mvwprintw(stdscr, 0, 15, "This is the %05dth pages!", page);							  wrefresh(stdscr);				//			  scroll(p1_window_ptr);						}						if(mylines > MYLINES - 1)						{	 							  mylines = 0;							  werase(pad_window_ptr);				//			  scroll(p1_window_ptr);						}				  }				  mvwprintw(p1_window_ptr, lines, cols, "%02x", received[ii]);				  mvwprintw(pad_window_ptr, mylines, cols, "%02x", received[ii]);				  wrefresh(p1_window_ptr);				  //sem_post(&bin_sem2);			}*/			sem_wait(&bin_sem);			res = read(fdr, received + ii, 1);			fprintf(receive,"%02x ", received[ii]);			j++;			if(cols >= COLS)			{				  cols = 0;				  lines++;				  mylines++;				  if(lines > LINES - 6)				  {	 						lines = 0;						page++;						werase(p1_window_ptr);						wrefresh(p1_window_ptr);						mvwprintw(stdscr, 0, 15, "This is the %05dth pages!", page);						wrefresh(stdscr);				  }				  if(mylines > MYLINES - 1)				  {	 						mylines = 0;						werase(pad_window_ptr);				  }			}			mvwprintw(p1_window_ptr, lines, cols, "%02x", received[ii]);			wrefresh(p1_window_ptr);			mvwprintw(pad_window_ptr, mylines, cols, "%02x", received[ii]);			cols += 4;			ii++;		/*	if(ii > MAX_RECEIVE - 1)			{				  ii = 0;				  base_line = (page - 1) * (LINES - 5);				  for(line_big = 0; line_big < LINES -6; line_big++)				  {						for(col_big = 0 ; col_big < COLS; col_big += 4)						{							  mvwprintw(pad_window_ptr, base_line + line_big, col_big, "%02x", received[ii]);							  ii++;						}				  }					  ii = 0;			}*/	  			//fprintf(receive,"\nThe %d receives is OK! Received data is %d bytes!\n", j, ii);			//sem_post(&bin_sem3);	  }	  pthread_exit("Thank you for using this program!");}int main(){	  initscr();	  erase();	  refresh();	  p2_window_ptr = newwin(4, COLS, LINES - 4, 0);	  fd = open(COM1, O_RDWR); 	  tcgetattr(fd,&oldtio); //储存目前的序列埠设定 	  bzero(&newtio,sizeof(newtio)); //清除结构体以放入新的序列埠设定值 	  //BAUDRAT E: 设定 bps 的速度. 你也可以用 cfsetispeed 及 cfsetospeed 来设定.	  //CRTSCTS : 输出资料的硬体流量控制 (只能在具完整线路的缆线下工作参考 Serial-HOWTO 第七节),此处不使用。	  //CS8     : 8n1 (8 位元, 不做同位元检查,1 个终止位元)	  //CLOCAL  : 本地连线, 不具数据机控制功能	  //CREAD   : 致能接收字元*/	  newtio.c_cflag = BAUDRATE /*| CRTSCTS */| CS8 | CLOCAL | CREAD;	  //校验位,偶校验	  newtio.c_cflag |=  PARENB;	  newtio.c_cflag &= ~PARODD;	  newtio.c_iflag |= INPCK;	  newtio.c_iflag |= IGNBRK;	  //IGNPAR  : 忽略经同位元检查後, 错误的位元组	  //ICRNL   : 比 CR 对应成 NL (否则当输入讯号有 CR 时不会终止输入)在不然把装置设定成 raw 模式(没有其它的输入处理)	  //newtio.c_iflag = IGNPAR | ICRNL;	  //Raw 模式输出.	  newtio.c_oflag &= ~OPOST ;	  //newtio.c_oflag = 0 ;	  //ICANON  : 致能标准输入, 使所有回应机能停用, 并不送出信号以叫用程式 	  newtio.c_lflag &= ~ICANON;	  newtio.c_lflag &= ~ECHO;	  newtio.c_cc[VTIME] = 0;     // 不使用分割字元组的计时器 	  newtio.c_cc[VMIN]  = 1;     // 在读取到 1 个字元前先停止 	  tcflush(fd, TCIFLUSH);	  tcsetattr(fd, TCSANOW, &newtio);	  close(fd);	  fds = open(COM1, O_WRONLY);	  fdr = open(COM1, O_RDONLY);	  j = 0x00000000;	  k = 0x00000000;	  sent = fopen ("datasave/sent.txt","w+");	  receive = fopen ("datasave/receive.txt", "w+");	  cbreak();	  noecho(); 	  pres = pthread_create(&a_thread, NULL, thread_function, NULL);	  if(pres != 0)	  {			perror("Thread0 creation failed!");			exit(EXIT_FAILURE);	  }	  pres1 = pthread_create(&b_thread, NULL, thread_function1, NULL);	  if(pres1 != 0)	  {			perror("Thread1 creation failed!");			exit(EXIT_FAILURE);	  }	  sres = sem_init(&bin_sem, 0 ,0);	  if(sres != 0)	  {			perror("Semaphore initialization failed!");			exit(EXIT_FAILURE);	  }	  /*sres = sem_init(&bin_sem2, 0 ,1);	  if(sres != 0)	  {			perror("Semaphore initialization failed!");			exit(EXIT_FAILURE);	  }	  sres = sem_init(&bin_sem3, 0 ,1);	  if(sres != 0)	  {			perror("Semaphore initialization failed!");			exit(EXIT_FAILURE);	  }*/	  mvwprintw(p2_window_ptr, 1, 0, "Sent data:" );	  for(i = 0, colss = 11; i < 11; i++, colss += 4)	  {			//sem_wait(&bin_sem2);			mvwprintw(p2_window_ptr, 1, colss, "%02x", send[i]);			wrefresh(p2_window_ptr);	  }	  colss += 2;	  while (STOP == FALSE)	  {		    if(CEASE == 0)			{				  k++;		//	sem_wait(&bin_sem3);				  ress = write(fds, send, 11); 				  if(ress != 11)				  {						mvwprintw(p2_window_ptr, 1, colss, "Can not sent OK!");						wrefresh(p2_window_ptr);				  }				  for(i = 0; i < 11; i++)						fprintf(sent,"%#02x ", send[i]);				  fprintf(sent,"\nThe %d sent is OK! Sent data is %d bytes!\n", k, i);				  sem_post(&bin_sem);				  usleep(800);			}	  }	  pres1 = pthread_join(b_thread, &thread2_result);	  if(pres1 != 0)	  {			perror("Thread1 join failed!");			exit(EXIT_FAILURE);	  }	  mvwprintw(p2_window_ptr, 2, 0, "%s", (char *)thread2_result);	  wrefresh(p2_window_ptr);	  while(1)	  {			fd = getch();			if(fd == 'u')			{				  if(mylines >= LINES - 5)				  {						mylines -= (LINES -5);						page--;				  }				  else				  {						mylines = 0;						page = 1;				  }				  u_pressed = 1; 				  werase(p1_window_ptr);				  wrefresh(p1_window_ptr);				  mvwprintw(stdscr, 0, 15, "This is the %05dth pages!", page);				  wrefresh(stdscr);				  prefresh(pad_window_ptr, mylines, 0, 1, 0, LINES - 5, COLS - 1);			}			else if(fd == 'd')			{			      if(mylines >= end_line)				  {						if(mylines == end_line)						{							  if(u_pressed == 1) 									mylines++;							  continue;						}						else							  continue;				  }				  else				  {						mylines += (LINES - 5);						page++;				  }				  werase(p1_window_ptr);				  wrefresh(p1_window_ptr);				  mvwprintw(stdscr, 0, 15, "This is the %05dth pages!", page);				  wrefresh(stdscr);				  prefresh(pad_window_ptr, mylines, 0, 1, 0, LINES - 5, COLS - 1);			}			else if(fd == 'H')			{				  pthread_cancel(a_thread);				  break;			}	  }	  mvwprintw(p2_window_ptr, 3, 0, "%s", "Thank you for using this program!");	  wrefresh(p2_window_ptr);	  sleep(2);	  delwin(p1_window_ptr);		  delwin(p2_window_ptr);		  sem_destroy(&bin_sem);	  delwin(pad_window_ptr);		  endwin();	  close(fds);	  close(fdr);	  fclose(sent);	  fclose(receive); //回存旧的序列埠设定值 	  fd = open(COM1, O_RDWR); 	  tcsetattr(fd,TCSANOW,&oldtio);	  close(fd);	  exit (EXIT_SUCCESS);} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -