Embedded Linux serial Programming (II.)

Source: Internet
Author: User

/*com_writer.c*/
#include "uart_api.h"

int main (void)
{
int FD;
Char Buff[buffer_size];
if ((Fd=open_port (Host_com_port)) <0)/* Open serial port */
{
Perror ("Open serial error");
return 1;
}
if (Set_com_config (fd,115200,8, ' N ', 1) <0)/* Configure serial Port */
{
Perror ("Set_com_config error");
return 1;
}
Do
{
printf ("Input Some words (enter ' quit ' to exit):");
memset (buff,0,buffer_size);
if (fgets (Buff,buffer_size,stdin) ==null)
{
Perror ("fgets");
Break
}
Write (Fd,buff,strlen (buff));
}while (strncmp (Buff, "quit", 4));
Close (FD);
return 0;

}

/*com_reader.c*/
#include "uart_api.h"
#define Com_type gnr_com
int main (void)
{
int FD;
Char Buff[buffer_size];
if ((Fd=open_port (Target_com_port)) <0)
{
Perror ("Open serial error");
return 1;
}
if (Set_com_config (fd,115200,8, ' N ', 1) <0)/* Configure serial Port */
{
Perror ("Set_com_config error");
return 1;
}
Do
{
memset (buff,0,buffer_size);
if (read (fd,buff,buffer_size) >0)
{
printf ("The Receive words are:%s", buff);
}
}while (strncmp (Buff, "quit", 4));
Close (FD);
return 0;
}

/*uart_api.h*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#define HOST_COM_PORT 1
#define BUFFER_SIZE 4096
#define MAX_COM_NUM 5
#define TARGET_COM_PORT 1
extern int open_port (int com_port);
extern int set_com_config (int fd,int baud_rate, int data_bits,char parity,int stop_bits);

Embedded Linux serial Programming (II.)

Related Article

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.