实例介绍
【实例截图】
【核心代码】
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <utils/Log.h>
#include "minicom.h"
#define BAUDRATE 115200
#define FALSE -1
#define TRUE 0
int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300};
static int fd;
void set_speed(int fd, int speed)
{
unsigned int i;
struct termios options;
if( tcgetattr( fd,&op4tions)==FALSE)//get termios
{
perror("tcgetattr");
return;
}
for ( i= 0;i<sizeof(speed_arr)/sizeof(int);i )
{
if(speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);//flush I/O buffer
cfsetispeed(&options, speed_arr[i]);//set input speed 设置输入波特率
cfsetospeed(&options, speed_arr[i]);//set output speed
if(tcsetattr(fd, TCSANOW, &options) == FALSE)//set termios now
{
perror("tcsetattr");
return;
}
tcflush(fd,TCIOFLUSH);//flush I/O buffer
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if( tcgetattr(fd,&options)==FALSE)//get termios 对终端设备的操作模式的设置和获取
{
perror("tcgetattr");
return(FALSE);
}
//set databits
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
//set parity
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;//unable parity 奇偶校验位使能
options.c_iflag &= ~INPCK;//unable input parity 使能帧和奇偶校验错误检查
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);//enable odd parity 使能时字符被接受
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;//set one stop bit
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB; //两个停止位
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
//set input parity
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150;//timeout
options.c_cc[VMIN] = 0;
if (tcsetattr(fd,TCSANOW,&options) == FALSE)
{
perror("tcsetattr");
return (FALSE);
}
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
return (TRUE); 4
}
int usb_open(void)
{
//open
LOGD("------%s-----\n", __FUNCTION__);
fd = open("/dev/ttyUSB0", O_RDWR);
if (fd < 0)
{
LOGE("open error: %s\n", strerror(errno));
exit(1);
}
//set
set_speed(fd,BAUDRATE);
if (set_Parity(fd,8,1,'N') == FALSE)
{
LOGE("open error: %s\n", strerror(errno));
exit (1);
}
return 0;
}
int usb_device_control(int flag)
{
LOGD("------%s-----\n", __FUNCTION__);
char on = '0';
int ret;
if(flag){
on = '1';
ret = write(fd, &on, 1);
if(ret < 0)
{
LOGE("write on error: %s\n", strerror(errno));
return -1;
}
}else{
on = '0';
ret = write(fd, &on, 1);
if(ret < 0)
{
LOGE("write on error: %s\n", strerror(errno));
return -1;
}
}
return 0;
}
int usb_close(struct hw_device_t* device)
{
if(device != NULL)
{
free(device);
}
close(fd);
return 0;
}
int usb_module_open(const struct hw_module_t *module,const char* id, struct hw_device_t** device)
{
LOGD("------%s-----\n", __FUNCTION__);
//实例化device对象
struct usb_hw_device_t *usbdev;
usbdev = (struct usb_hw_device_t *)malloc(sizeof(struct usb_hw_device_t));
if(usbdev == NULL)
{
LOGE("malloc error\n");
return -1;
}
usbdev->common.tag = HARDWARE_DEVICE_TAG;
usbdev->common.version = 1;
usbdev->common.module = module;
usbdev->common.close = usb_close;
usbdev->open = usb_open;
usbdev->control_dev = usb_device_control;
//将usbdev传递给一个jni
*device = (struct hw_device_t *)usbdev;
return 0;
}
//定义一个方法集合
struct hw_module_methods_t usb_module_methods = {
open : usb_module_open,
};
//用自定义的module对象定义HMI(hal的入口)
struct usb_hw_module_t HMI = {
common : {
tag : HARDWARE_MODULE_TAG,
version_major : 1,
version_minor : 0,
id : MINICOM_ID,
name : "this is PL2303 hal for fs210",
author : "caiii",
methods : &usb_module_methods,
},
};
标签: 串口
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论