linux USB 轉串口

來源:互聯網
上載者:User

1. 核心配置

Device Drivers  --->

     [*] USB support  ---> 

          USB Serial Converter support  --->

                <*> USB Serial Converter support

                <*>   USB Prolific 2303 Single Port Serial Driver

 

2. 編譯核心,嵌入式板子載入核心,進入核心後,查看是否產生對應的裝置節點

#ls /dev/ttyUSB0

如果沒有上述節點,手動建立

#mknod /dev/ttyUSB0 c 188 0

 

3. 編寫程式,測試USB轉串口是否能正常讀寫

 

int Test_com(char *pCom)
{
 fd_set fds_r;
 struct termios Opt;
 int fdtty0=0;
 struct timeval tm = {1, 0};
 int err=0;
 char readbuf = '0';
 char tmp[16] = {0};
 char buf1='t';
 char data1='s'; 
 
 fdtty0=open(pCom, O_SYNC | O_RDWR );//"/dev/ttyS2"
 if (fdtty0 < 0)
 {
  printf( "open %s failed!/n", pCom);
  goto ERR;
 }
 
 if (tcgetattr(fdtty0, &Opt) < 0)
  goto ERR;
 
 /* If you uncomment ISIG and BRKINT below, then ^C will be ignored.*/
 Opt.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG | ECHOE);
 Opt.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON | BRKINT);
 Opt.c_iflag &= ~(CSIZE | PARENB | CSTOPB);
 Opt.c_oflag &= ~(ONLCR);//for not translate from NL to CR NL
 Opt.c_cflag = CS8 | B115200 | CLOCAL | CREAD;
 Opt.c_cc[VMIN] = 0;
 Opt.c_cc[VTIME] = 0;

 tcflush(fdtty0, TCIFLUSH);
 if(tcsetattr(fdtty0, TCSAFLUSH, &Opt) < 0)
  goto ERR;
 
 FD_ZERO(&fds_r);
 FD_SET(fdtty0, &fds_r);

 if(write(fdtty0, &buf1, 1) < 0)
 {
  printf("write version error/n");
  return 0;
 }
 
 err=select(fdtty0+1, &fds_r, NULL, NULL, &tm);
 
 if (err < 0)
 {  /* failure */
  printf("select Error/n");
 }
 else if (err == 0)
 {  /* time out; */
  printf("select Timeout/n");
 }
 
 if (FD_ISSET(fdtty0, &fds_r))
 {
  //err = read(fdtty0, &data1,1);
  
  err = read(fdtty0, tmp,sizeof(tmp));
  printf( "err: %d, tmp: %s/n" ,err,tmp );
  
  if (err < 0)
  {
   printf("read error/n");
  }
 }

ERR:
 if (fdtty0 > 0)
  close(fdtty0);
 fdtty0 = 0;
 
 if(buf1==tmp[0])
 {
  printf("buf1:%c,tmp[0]:%c, return 1/n",buf1,tmp[0]);  
  return 1;
 }
 else
 {
  printf("buf1:%c,tmp[0]:%c, return 0/n",buf1,tmp[0]);
  return 0;
 } 
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.