树莓派串口

一、设置串口功能

sudo raspi-config

选择5 Interfacing Options>>P6 Serial>>No>>YES

The serial login shell is disabled  │ │ The serial interface is enabled

 

二、安装minicom

sudo apt-get install minicom

安装好后,进行配置

minicom -s

    +-----------------------------------------------------------------
    | A -    Serial Device      : /dev/ttyS0
    | B - Lockfile Location     : /var/lock
    | C -   Callin Program      :
    | D -  Callout Program      :
    | E -    Bps/Par/Bits       : 115200 8N1
    | F - Hardware Flow Control : No
    | G - Software Flow Control : No
    |
    |    Change which setting?
    +-----------------------------------------------------------------
然后选择

            | Save setup as dfl        |
保存

 

三、树莓派接口是TTL电平,所以连一个TTL转USB才能连上电脑USB

下面是树莓派的串口测试代码

 1 #ifndef __MYSERIAL_H_
 2 #define __MYSERIAL_H_
 3 
 4 #include <iostream>
 5 
 6 class MySerial
 7 {
 8 public:
 9     MySerial();
10     int SerialOpen(int idex, int speed);
11     void SerialClose();
12     int SetOption(int fd, int nSpeed, int bBits, char nEvent, int nStop);
13     int SerialWrite(char *data, int len);
14     
15 
16 public:
17     int s_fd;
18 };
19 
20 #endif
serial.h

串口类

  1 #include "serial.h"
  2 #include <stdio.h>
  3 #include <fcntl.h>
  4 #include <unistd.h>
  5 #include <termios.h>
  6 #include <strings.h>
  7 #include <string.h>
  8 #include <errno.h>
  9 
 10 MySerial::MySerial():s_fd(-1)
 11 {
 12     s_fd = SerialOpen(0, 9600);
 13     if(s_fd < 0) {
 14         return;
 15     }
 16 }
 17 
 18 int MySerial::SerialOpen(int idex, int speed)
 19 {
 20     int fd;
 21     char ttyname[64];
 22     int flag;
 23 
 24     snprintf(ttyname, sizeof(ttyname), "/dev/ttyS%d", idex);
 25     if((fd = open(ttyname, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
 26         perror("open serial");
 27         return(-1);
 28     }
 29     /* 恢复串口为阻塞状态 */
 30     flag = fcntl(fd, F_GETFL);
 31     flag |= O_NONBLOCK;
 32     if(fcntl(fd, F_SETFL, flag) < 0)  {
 33         perror("fcntl error");
 34         printf("fd = %d,errno =%d\n", fd, errno);
 35         return(-1);
 36     }
 37     
 38     if(SetOption(fd, speed, 8, 'n', 1) < 0) {
 39         return(-1);
 40     }
 41 
 42     return fd;
 43 }
 44 
 45 void MySerial::SerialClose()
 46 {
 47     close(s_fd);
 48 }
 49 
 50 int MySerial::SetOption(int fd, int nSpeed, int nBits, char nEvent, int nStop)
 51 {
 52     struct termios newOpt, oldOpt;
 53 
 54     /* 获取参数 */
 55     if(tcgetattr(fd, &oldOpt) < 0) {
 56         perror("tcgetattr");
 57         return(-1);
 58     }
 59     bzero(&newOpt, sizeof(struct termios));
 60 
 61     /* 设置字符大小 */
 62     newOpt.c_cflag |= (CLOCAL | CREAD);    /* 本地连接,使能接收 */
 63     newOpt.c_cflag &= ~CSIZE;
 64 
 65     /* 设置停止位 */
 66     switch(nBits) {
 67     case 7:
 68         newOpt.c_cflag |= CS7;
 69         break;
 70     case 8:
 71         newOpt.c_cflag |= CS8;
 72         break;
 73     default:
 74         newOpt.c_cflag |= CS8;
 75         break;
 76     }
 77 
 78     /* 设置奇偶校验位 */
 79     switch(nEvent) {
 80     case 'o':
 81     case 'O':    //奇数
 82         newOpt.c_cflag |= (PARENB | PARODD);
 83         newOpt.c_iflag |= (INPCK | ISTRIP);
 84         break;
 85     case 'e':
 86     case 'E':
 87         newOpt.c_iflag |= (INPCK | ISTRIP);
 88         newOpt.c_cflag |= PARENB;
 89         newOpt.c_cflag &= ~PARODD;
 90         break;
 91     case 'n':
 92     case 'N':
 93         newOpt.c_cflag &= ~PARENB;
 94         break;
 95     default:
 96         newOpt.c_cflag &= ~PARENB;
 97         break;
 98     }
 99 
100     /* 设置波特率 */
101     switch(nSpeed) {
102     case 2400:
103         cfsetispeed(&newOpt, B2400);
104         cfsetospeed(&newOpt, B2400);
105         break;
106     case 4800:
107         cfsetispeed(&newOpt, B4800);
108         cfsetospeed(&newOpt, B4800);
109         break;
110     case 9600:
111         cfsetispeed(&newOpt, B9600);
112         cfsetospeed(&newOpt, B9600);
113         break;
114     case 115200:
115         cfsetispeed(&newOpt, B115200);
116         cfsetospeed(&newOpt, B115200);
117         break;
118     default:
119         cfsetispeed(&newOpt, B9600);
120         cfsetospeed(&newOpt, B9600);
121         break;
122     }
123 
124     /* 设置停止位 */
125     if(nStop == 1) {
126         newOpt.c_cflag &= ~CSTOPB;
127     } else if(nStop == 2) {
128         newOpt.c_cflag |= CSTOPB;
129     }
130 
131     /* 设置等待时间和最小接收字符 */
132     newOpt.c_cc[VTIME] = 0;
133     newOpt.c_cc[VMIN] = 0;
134 
135     tcflush(fd, TCIFLUSH);
136     if(tcsetattr(fd, TCSANOW, &newOpt) < 0) {
137         perror("serial set error");
138         close(fd);
139         return(-1);
140     }
141 
142     return 0;
143 }
144 
145 int MySerial::SerialWrite(char *data, int len)
146 {
147     int count = 0;
148     if(s_fd < 0) {
149         return -1;
150     }
151 
152     count = write(s_fd, data, len);
153     if(count == len) {
154         return count;
155     } else {
156         tcflush(s_fd, TCOFLUSH);
157         return -1;
158     }
159 }
serial.cpp

main函数

#include <iostream>
#include "serial.h"
#include <string.h>

int main(int argc, char *argv[])
{
    MySerial myserial;
    myserial.SerialWrite("Hello", strlen("Hello"));
}

 

posted @ 2018-04-02 08:52  习惯就好233  阅读(1113)  评论(0编辑  收藏  举报