基于cocos2dx3.1引擎C++编写的android平台串口通信接口

这篇博客介绍了一种使用C++基于cocos2dx3.1引擎在Android平台上实现串口通信的方法,包括打开、设置和关闭串口的函数,以及发送和接收数据的实现。代码示例展示了如何在cocos2dx项目中初始化串口并进行数据交换。
摘要由CSDN通过智能技术生成
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 ==
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值