NewGen
2019-03-14 linux Serial Port Check and Coding 본문
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
-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.
'LINUX' 카테고리의 다른 글
find, grep 을 이용한 파일검색, 문자열 포함 파일 찾기. (0) | 2021.06.17 |
---|---|
Shared folder was caused permission denied on CentOS of Samba connection (0) | 2020.01.10 |
QT everywhere 설치... (0) | 2019.05.29 |
드라이버 개발 혹은 usb 장치 개발 후 lsusb 했을때 VID,PID 안보여서 추가해야 할 경우 제조사 및 상품코드가 변경된 관계로ㅡ lsusb 시 1111:22ff 로만 검색되며, 자신의 회사명, 제조사 명 및 디.. (0) | 2019.02.17 |
블루투스 마우스 연결문제 (0) | 2019.02.17 |