NewGen

2019-03-14 linux Serial Port Check and Coding 본문

LINUX

2019-03-14 linux Serial Port Check and Coding

Deep Learning 2019. 3. 14. 14:09


2019-03-14 linux Serial Port Check and Coding



-check device : cat /proc/tty/driver/serial


Devices is detected like below pic.

There are assigned physically.


5:~  : not real





-check kernel driver  : dmesg | grep tty




-check port : setserial -a /dev/tty0

setserial is not default program.

install this and try again



after install,

check again..




-use stty command : stty -F /dev/ttyS3



-use stty commane and set baud rate : stty 115200 < /dev/ttyS0

-more detail info : stty -a < /dev/ttyS3


-set default port configuration : setserial /dev/ttyS0


-set default port configuration with stty : stty sane < /dev/ttyS0


-basic port programming. (c++)

example :


int check_port()

{

       struct termios tio;

       //struct termios stdio;

       //struct termios old_stdio;

       int tty_fd=0;


       unsigned char buf[5]={0x10,0x04};

unsigned char rcv=0;

       //tcgetattr(STDOUT_FILENO,&old_stdio);


       //printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);

#if 0

       memset(&stdio,0,sizeof(stdio));

       stdio.c_iflag=0;

       stdio.c_oflag=0;

       stdio.c_cflag=0;

       stdio.c_lflag=0;

       stdio.c_cc[VMIN]=1;

       stdio.c_cc[VTIME]=0;

       tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);

       tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);

       fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);       // make the reads non-blocking

#endif


       memset(&tio,0,sizeof(tio));

       tio.c_iflag=0;

       tio.c_oflag=0;

       tio.c_cflag=CS8|CREAD|CLOCAL;           // 8n1, see termios.h for more information

       tio.c_lflag=0;

       tio.c_cc[VMIN]=1;

       tio.c_cc[VTIME]=100;

bool bFound = 0;

//QString port_name;

for (int i=0; i<MAX_RS232_PORT_NUM; i++)

{

   char str_port[16];

   memset(str_port,0,sizeof(str_port));

   

   sprintf(str_port,"/dev/ttyS%d",i);

   

   

   tty_fd=open(str_port, O_RDWR | O_NONBLOCK);      

   if(!tty_fd)

   {

     printf("port open error or does not exist!!\n");

   }else

   {

    cfsetospeed(&tio,B115200);            // 115200 baud

cfsetispeed(&tio,B115200);            // 115200 baud


tcsetattr(tty_fd,TCSANOW,&tio);

printf("Check %s... \n",str_port);

int n=0;

int k=0;

buf[2] = 0x02;

while (!rcv)

{

k=write(tty_fd,buf,3);              // if new data is available on the serial port, print it out

//printf("send k=%d",k);

if (read(tty_fd,&rcv,1)>0)

{

 //printf("Rcv=0x%x\n",rcv);

//check value whatever you want...

 if(((rcv & 0x10 )== 0x10) && ((rcv & 0x02 )== 0x02))

 {

   bFound = 1;

   break;

 }

}

//printf("\nCheck prt ...");

sleep(1);

n++;

if(n>3) break;

//if (read(STDIN_FILENO,&c,1)>0)  write(tty_fd,&c,1);      // if new data is available on the console, send it to the serial port

}        


close(tty_fd);

   }

   

   if(bFound)

   {

printf("Printer Detected!!\n");

return i;

   }

}

       //tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);


       return 0xFF;

}




end of example code



-Useful Program : minicom.




Comments