Linux USB to serial port

Source: Internet
Author: User

1. Kernel configuration

Device Drivers --->

[*] USB support --->

USB serial converter support --->

<*> USB serial converter support

<*> USB prolific 2303 single port serial driver

 

2. Compile the kernel. Load the kernel on the embedded board. after entering the kernel, check whether the corresponding device node is generated.

# Ls/dev/ttyusb0

If you do not have the above nodes, manually create

# Mknod/dev/ttyusb0 C 188 0

 

3. Write a program to test whether the USB-to-serial port can be read and written normally.

 

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;
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.