5.【Orangepi Zero2】串口通信

~/wiringOP/examples 底下的serialTest.c 示例程序

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wiringPi.h>
#include <wiringSerial.h>

int main ()
{
  int fd ;
  int count ;
  unsigned int nextTime ;

  if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)
  {
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

  if (wiringPiSetup () == -1)
  {
    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
    return 1 ;
  }

  nextTime = millis () + 300 ;

  for (count = 0 ; count < 256 ; )
  {
    if (millis () > nextTime)
    {
      printf ("\nOut: %3d: ", count) ;
      fflush (stdout) ;
      serialPutchar (fd, count) ;
      nextTime += 300 ;
      ++count ;
    }

    delay (3) ;

    while (serialDataAvail (fd))
    {
      printf (" -> %3d", serialGetchar (fd)) ;
      fflush (stdout) ;
    }
  }

  printf ("\n") ;
  return 0 ;
}

在这里插入图片描述

脱离 wiringPi 库 自己实现串口通信

根据用户手册修改配置文件

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

进入linux系统后,先确认下/dev下是否存在uart5的设备节点

ls /dev/ttyS5

在这里插入图片描述

mySerial2.h

int my_serialOpen (const char *device, const int baud);

void my_serialPuts(const int fd, const char *s);

int my_serialGets(const int fd, const char *buffer);

char* serialGetchar(const int fd);

mySerial2.c

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

int my_serialOpen (const char *device, const int baud)
{
  struct termios options ;
  speed_t myBaud ;
  int     status, fd ;

  	switch (baud)
  	{
    	case    9600:	myBaud =    B9600 ; break ;
    	case  115200:	myBaud =  B115200 ; break ;

    	default:
      	return -2 ;
  	}

  	if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
    return -1 ;

  	fcntl (fd, F_SETFL, O_RDWR) ;

// Get and modify current options:

  	tcgetattr (fd, &options) ;

    cfmakeraw   (&options) ;
    cfsetispeed (&options, myBaud) ;
    cfsetospeed (&options, myBaud) ;

    options.c_cflag |= (CLOCAL | CREAD) ;
    options.c_cflag &= ~PARENB ;
    options.c_cflag &= ~CSTOPB ;
    options.c_cflag &= ~CSIZE ;
    options.c_cflag |= CS8 ;
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
    options.c_oflag &= ~OPOST ;

    options.c_cc [VMIN]  =   0 ;
    options.c_cc [VTIME] = 100 ;	// Ten seconds (100 deciseconds)

  	tcsetattr (fd, TCSANOW, &options) ;

  	ioctl (fd, TIOCMGET, &status);

  	status |= TIOCM_DTR ;
  	status |= TIOCM_RTS ;

  	ioctl (fd, TIOCMSET, &status);

  	usleep (10000) ;	// 10mS

 	return fd ;
}

void my_serialPuts(const int fd, const char *s)
{
	int n_write = write(fd, s, strlen(s));

	if(n_write == -1)
	{
		perror("write");

		exit(-1);
	}

}

int my_serialGets(const int fd, char *buffer)
{
	int n_read = read(fd, buffer, 32);
	if(n_read == -1)
	{
		perror("read");

		exit(-1);
	}

	return n_read;
}

char* serialGetchar (const int fd)
{
	char *x;

  	if (read (fd, x, 1) != 1)
    exit(-1);

  	return x;
}

serialTest.c

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

#include "mySerial.h"

int fd;

void* serialSend()
{
	char *send_buf;
	send_buf = (char *)malloc(32*sizeof(send_buf));

	while(1)
	{
		memset(send_buf, '\0', sizeof(send_buf));
		fgets(send_buf, sizeof(send_buf), stdin);
		my_serialPuts (fd, send_buf);
	}
}

void* serialRecieve()
{	
	char *recieve_buf;
	recieve_buf = (char *)malloc(32*sizeof(recieve_buf));
	
//	char recieve_buf[32];

    while(1)
	{
		memset(recieve_buf, '\0', sizeof(recieve_buf));
		my_serialGets(fd, recieve_buf);
      	printf ("GET->%s\n", recieve_buf);
		fflush(stdout);
	}
}

int main ()
{
	pthread_t t1;
	pthread_t t2;

  	if ((fd = my_serialOpen ("/dev/ttyS5", 115200)) < 0)
  	{
    	fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    	
		return 1 ;
  	}

	pthread_create(&t1, NULL, serialSend, NULL);
	pthread_create(&t2, NULL, serialRecieve, NULL);
	
	while(1)
	{
		sleep(3);
	}

  printf ("\n") ;
  return 0 ;
}

运行结果

在这里插入图片描述

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值