Linux UART Program

Source: Internet
Author: User
Tags vmin

I use the Jetson TX1 Development Board is Linux system out of the serial port file may be different from the other is not different can be used.

I'm installing QT5.

Create a none QT C project, developed in C language

During debugging two days results or send and receive display is not the same because the rs22 voltage and the UART TTL voltage mismatch due to the Development Board is not with max232 so to add the adapter board (UART to TTL) measured is 3.3V so to add a TTL to RS232 adapter board So that the serial port of our host will be able to receive data

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>

int set_opt (int fd,int nspeed, int nBits, char nevent, int nstop)
{
struct Termios newtio,oldtio;
if (tcgetattr (fd,&oldtio)! = 0) {
Perror ("Setupserial 1");
return-1;
}
Bzero (&newtio, sizeof (Newtio));
Newtio.c_cflag |= clocal | Cread; Clocal: Ignore modem control line cread: Open recipient
Newtio.c_cflag &= ~csize; The character length mask. Values are: CS5,CS6,CS7 or CS8

Switch (nBits)
{
Case 7:
Newtio.c_cflag |= CS7;
Break
Case 8:
Newtio.c_cflag |= CS8;
Break
}

Switch (nevent)
{
Case ' O ':
Newtio.c_cflag |= Parenb; Allows output to produce parity information and input to parity
Newtio.c_cflag |= parodd; Input and output are odd and verified
Newtio.c_iflag |= (INPCK | Istrip); Inpack: Enable input parity detection; Istrip: remove Eighth bit
Break
Case ' E ':
Newtio.c_iflag |= (INPCK | Istrip);
Newtio.c_cflag |= Parenb;
Newtio.c_cflag &= ~parodd;
Break
Case ' N ':
Newtio.c_cflag &= ~parenb;
Break
}

Switch (nspeed)
{
Case 2400:
Cfsetispeed (&newtio, B2400);
Cfsetospeed (&newtio, B2400);
Break
Case 4800:
Cfsetispeed (&newtio, B4800);
Cfsetospeed (&newtio, B4800);
Break
Case 9600:
Cfsetispeed (&newtio, B9600);
Cfsetospeed (&newtio, B9600);
Break
Case 115200:
Cfsetispeed (&newtio, B115200);
Cfsetospeed (&newtio, B115200);
Break
Case 460800:
Cfsetispeed (&newtio, B460800);
Cfsetospeed (&newtio, B460800);
Break
Default
Cfsetispeed (&newtio, B9600);
Cfsetospeed (&newtio, B9600);
Break
}

if (nstop = = 1)
Newtio.c_cflag &= ~CSTOPB; CSTOPB: Set two stop bits instead of a
else if (nstop = = 2)
Newtio.c_cflag |= CSTOPB;

Newtio.c_cc[vtime] = 0; Vtime: Delay in non-cannoical mode reading, in one-tenth second bit units
Newtio.c_cc[vmin] = 0; VMIN: Minimum number of characters read in non-canonical mode
Tcflush (Fd,tciflush); Changing the output of all objects written to the FD reference takes effect and all accepted but unread inputs are discarded before the change occurs.
if ((Tcsetattr (fd,tcsanow,&newtio))!=0)//tcsanow: Change occurs immediately
{
Perror ("com set error");
return-1;
}
printf ("Set done!\n\r");
return 0;
}

int main (void)
{
int Fd1,nset,ret;
int nread;
Char buf[10]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
Char buf1[];
FD1 = open ("/dev/ttyths0", O_RDWR);
FD1 = open ("/dev/ttys0", O_RDWR | O_noctty);
if (fd1 = =-1) {
printf ("No such/dev/ttys0 exist\n");
Exit (1);
}

printf ("Open ttyS0 success!! \ n ");

Nset = set_opt (FD1, 115200, 8, ' N ', 1);
if (Nset = =-1)
Exit (1);
printf ("SET ttyS0 success!! \ n ");
printf ("Enter the loop!! \ n ");

while (1)

{
memset (buf1, 0, sizeof (BUF1));
ret = Write (FD1, buf, 10);
if (Ret > 0) {
printf ("Write success! Wait Data receive\n ");
}
/*
Nread = Read (FD1, buf1, sizeof (BUF1));

if (Nread > 0) {
printf ("Redatad:nread =%s\n\n\r", buf1);
}
*/
Sleep (1);
}
Close (FD1);

return 0;
}

Linux UART Program

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.