1.serial.h头文件
#ifndef __SERIAL_H__
#define __SERIAL_H__
//包含头文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <pthread.h>
//
extern "C" {
int initSerialPort(void);
int open_port(int fd,int comport);
int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop);
int closeSerialPort(int fd);
int sendDate(char *pbuf,int size);
int receiverDate(char *pbuf,int size);
}
#endif
/*从头文件中的函数定义不难看出,函数的功能,使用过程如下:
(1) 打开串口设备,调用函数open_port();
(2) 设置串口读写的波特率、停止位、校验位、数据位等.,调用函数set_opt();
(3) 向串口写入数据,调用函数sendDate();
(4) 从串口读出数据,调用函数receiverDate();
(5) 操作完成后,需要调用函数closeSerialPort()来释放申请的串口信息接口; */
2. serial.cpp文件
#include <stdio.h>
#include <sys/ioctl.h>
#include "serial.h"
//打印
#include "android/log.h"
static const char *TAG="HelloWorldScene";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
static int fd = 0;
/*****************************
* 功能:初始化设置串口
*****************************/
int initSerialPort(void)
{
fd = open_port(fd,2);
if(fd ==