【全志H616】-2 写一个自己的串口

【全志H616】-2 写一个自己的串口

1、基本命令

  • 重启
sudo reboot
  • Linux系统下一个文件夹的文件复制到另一个文件夹下
cp flags.c /home/user05/lab09/flags_revised.c
//复制当前文件夹下的 flags.c 文件到 lab09 文件夹下flags_recised.c 文件

cp oled_demo.c /home/orangepi/hardwareSoft/oled_demo.c
  • 引脚命令
gpio readall

在这里插入图片描述

2、来写一个自己的串口

2.1 串口收发实验1

//serialTest.c:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
int fd;
void *Sendhandler()
{
  char *sendBuf;
  sendBuf = (char *)malloc(32*sizeof(32));

	while(1)
	{
    memset(sendBuf,'\0',32);
    scanf("%s",sendBuf);
    while(1)
    {
	    serialPutchar (fd, *sendBuf++) ;//发数据
    }
  }
}

void* Revhandler()
{
  while(1)
  {
    while (serialDataAvail (fd))
    {
      printf ("%c", serialGetchar (fd)) ;//收参数
      fflush (stdout) ;
    }
  }

}

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


  pthread_t idSend;
  pthread_t idRev;
  if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0)//串口6,波特率
  {
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

  pthread_create(&idSend,NULL,Sendhandler,NULL);
  pthread_create(&idRev,NULL,Revhandler,NULL);

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

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

2.5 写一个自己的串口通信

  • 这一块有3个文件,分别是uartTool.huartTool.cuarttest.c;
//uartTool.h
#ifndef __UARTTOOL_H
#define __UARTTOOL_H
int myserialOpen (const char *device, const int baud);
void serialSendstring (const int fd, const char *s);
int serialGetstring (const int fd, char *buffer);
#endif
  • uartTool.h中,除了函数声明,其余都是固定的,#ifndef __UARTTOOL_H 和 #define __UARTTOOL_H后面跟的就是文件名
//uartTool.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>
#include "uartTool.h"//先到当前文件夹查看
#include "wiringSerial.h"

int myserialOpen (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:
  }
  
  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) ;//get

  //标准化的操作 一般都是固定的
  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);//获得整型控制字TIOCMGET get出来
  status |= TIOCM_DTR ;
  status |= TIOCM_RTS ;//控制字加上标志位
  ioctl (fd, TIOCMSET, &status);//写入内核

  usleep (10000) ;	// 10mS

  return fd ;
}

void serialSendstring (const int fd, const char *s)
{
	int ret;
  ret = write (fd, s, strlen (s));
  if (ret < 0)
 		printf("Serial Putchar Error\n");
}

int serialGetstring (const int fd, char *buffer)
{
	int n_read;
	n_read = read(fd,buffer,32);
	return n_read;
}
//uarttest.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>
#include <unistd.h>
#include <pthread.h>
#include "uartTool.h"

int fd;
void* readSerial()
{
    char buffer[32];
    while(1)
    {
        memset(buffer,'\0',sizeof(buffer));
        serialGetstring(fd,buffer);
        printf("GET->%s\n",buffer);
    }
}

void* sendSerial()
{
    char buffer[32];
    while(1)
    {
        memset(buffer,'\0',sizeof(buffer));
        scanf("%s",buffer);
        serialSendstring(fd, buffer);
    }
}

int main(int argc, char **argv)
{
    char deviceName[32] = {'\0'};
    pthread_t readt;
    pthread_t sendt;

	if(argc < 2)
    {
        printf("usag:%s /dev/ttyS?\n",argv[0]); //端口号
        return -1;
    }

    strcpy(deviceName,argv[1]);//端口号给 deviceName
    if((fd = myserialOpen(deviceName, 115200)) == -1)
    {
        printf("open %s error\n",deviceName);
        return -1;
    }

    pthread_create(&readt,NULL,readSerial,NULL);
    pthread_create(&sendt,NULL,sendSerial,NULL);

    while(1){
        sleep(10);
    }
}
  • 实验结果
    在这里插入图片描述
  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值