Ubuntu Serial Test

Source: Internet
Author: User
Tags function definition unsupported

1>. Virtual serial port test (http://www.xappsoftware.com/wordpress/2013/10/07/using-virtual-serial-ports-on-linux-ubuntu/?) GOBACK=%2EGDE_65688_MEMBER_5792872722853814274#%21)
Virtual serial port tool is available under Windows. Ubuntu also has the following:
Install Virtual serial port:

sudo apt-get install Socat

Create and connect 2 virtual serial ports

The path is under:/dev/pts.
There is no before the creation, you can use the ls/dev/pts test.
Open a new terminal and enter the command to open the virtual serial 5:

# sudo CAT/DEV/PTS/5

Open another terminal, send the data to the virtual serial 6, because 6 and 5 are interlinked, so 5 will receive the data.

# sudo echo "Hello World" >/DEV/PTS/6

That's all.
See figure below:

The virtual serial port is not created, of course, see the following figure:

2> and Minicom Communications.
See figure below:
(Minicom Open the virtual serial 7, to the virtual serial 28 send characters, Minicom will receive)

3>
Finally use their own code to test, the serial source is as follows (reference http://blog.csdn.net/u010925447/article/details/72763052):
Uart.c

#include <stdio.h>/* standard input/output definition */#include <stdlib.h>//Standard function library definition */#include <unistd.h>      /*unix standard function definition */#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> /* File Control definition */#include <termios.h>/*ppsix terminal control definition/#include <errno.h>/* error number Definition *//************ /#define FALSE-1 #define TRUE 0/************************
    /#if 0 int main (void) {int fd;
struct Termios Opt;
    Tcgetattr (FD, &opt);     Cfsetispeed (&opt,b19200);
    /* Set to 19200bps*/cfsetospeed (&opt,b19200);
    Tcsetattr (fd,tcanow,&opt);
    * Read and write to open the serial port/fd = open ("/dev/ttys0", O_RDWR); if ( -1 = = FD) {perror ("hint Error").
    "); #endif/** * @brief Set serial communication rate * @param FD type int open serial file handle * @param speed type int serial speed * @return void */INT Speed_arr[] = {B38400,B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300,};
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300,}; 
 void Set_speed (int fd, int speed) {int i; 
 int status;
 struct Termios Opt; 
 Tcgetattr (FD, &opt);  for (i= 0;  I < sizeof (Speed_arr)/sizeof (int);     
   i++) {if (speed = = Name_arr[i]) {Tcflush (fd, Tcioflush);  
   Cfsetispeed (&opt, speed_arr[i]);   
   Cfsetospeed (&opt, speed_arr[i]);  
   Status = Tcsetattr (FD, Tcsanow, &opt);  
    if (status!= 0) {perror ("tcsetattr fd1");     
   Return   
  } tcflush (Fd,tcioflush); /** * @brief set serial data bits, stop bits and parity bits * @param FD type int open serial file handle * @param databits type int data bits are 7 or 8 * @pa  RAM stopbits type int stop bit value is 1 or 2 * @param parity type int parity type value is n,e,o,,s/int set_parity (int fd,int databits,int Stopbits,int parity) {struct TERmios options;     
      if (tcgetattr (fd,&options)!= 0) {perror ("Setupserial 1");  
     return (FALSE); 
     } options.c_cflag &= ~csize; 
      Switch (databits)/* Set the number of data digits/{case 7:options.c_cflag |= CS7;
     Break
      Case 8:options.c_cflag |= CS8;   
     Break default:fprintf (stderr, "Unsupported data Sizen");  
     return (FALSE);   Switch (parity) {case ' n ': Case ' n ': Options.c_cflag &= ~parenb;     /* Clear parity Enable */Options.c_iflag &= ~INPCK;  
     /* Enable Parity checking * * break; Case ' O ': Case ' o ': Options.c_cflag |= (parodd | PARENB);             /* Set to the Magic Magic Test/Options.c_iflag |= INPCK;  
     /* disnable Parity checking * * break;     Case ' E ': Case ' e ': Options.c_cflag |= Parenb;   /* Enable Parity * * Options.c_cflag &= ~parodd; /* Conversion to even-parity */options.c_iFlag |= INPCK;
     /* disnable Parity checking * * break;
      Case "s": Case ' s ':/*as no parity*/options.c_cflag &= ~parenb;  
     Options.c_cflag &= ~cstopb;break;    
      default:fprintf (stderr, "Unsupported Parityn");  
     return (FALSE);  
      /* Set Stop bit */switch (stopbits) {case 1:options.c_cflag &= ~CSTOPB;  
     Break  
        Case 2:options.c_cflag |= CSTOPB;
     Break  
       default:fprintf (stderr, "Unsupported Stop Bitsn"); 
    return (FALSE); 
    }/* Set input parity option */if (parity!= ' n ') options.c_iflag |= inpck;
    Tcflush (Fd,tciflush); Options.c_cc[vtime] = 150; /* Set timeout seconds*/options.c_cc[vmin] = 0;  /* Update the options and do it now/if (tcsetattr (fd,tcsanow,&options)!= 0) {perror ("setupserial   
     3 ");  
    return (FALSE);  
return (TRUE); /** code Description: Using the serial port test, the number of sentIt is a character, but does not send a string ending symbol, so after receiving, the end symbol is appended.
I tested the use of SCM to send data to the second serial port, the test passed.         * * int Opendev (char *dev) {int fd = open (Dev, O_RDWR); //| O_noctty |
  O_ndelay if ( -1 = = FD) {perror ("Can" "T Open serial Port");  
 return-1;
else return FD;
 int main (int argc, char **argv) {int fd;
 int nread;
 Char buff[512]={0}; Char *dev = "/DEV/PTS/7";//"/DEV/TTYS1";
 Serial port Two fd = Opendev (dev);
 Set_speed (fd,19200);
  if (set_parity (fd,8,1, ' N ') = = FALSE) {printf ("Set Parity Errorn");
 Exit (0); 
 while (1)//loop reads data {while (nread = read (FD, Buff,)) >0) {printf ("nlen=%d\n", nread);   
  Buff[nread+1] = ' the ';   
 printf ("rx:%s\n", buff);  
 }//close (FD);
Exit (0);    }//write:char buffer[1024];int Length;int nbyte;nbyte = write (fd, buffer, Length)//read:char Buff[1024];int Len;int readbyte = read (Fd,buff,len);

Uart.c
Compilation:
gcc uart.c-o uart
Execution (this serial port is open with virtual serial port 7):
./uart
Then send the data to the virtual serial port 28, the UART program will print the length and data received. Just the length seems wrong ...
The following figure:

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.