You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
2.6 KiB
C++
117 lines
2.6 KiB
C++
#include <jni.h>
|
|
#include <string>
|
|
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <sys/ioctl.h>
|
|
#include <linux/spi/spidev.h>
|
|
#include <android/log.h>
|
|
#include <stdio.h>
|
|
#include <dirent.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <time.h>
|
|
#include "GPIOControl.h"
|
|
#include "SerialComm.h"
|
|
|
|
|
|
static void set_baudrate (struct termios *opt, unsigned int baudrate)
|
|
{
|
|
cfsetispeed(opt, baudrate);
|
|
cfsetospeed(opt, baudrate);
|
|
}
|
|
|
|
static void set_data_bit (struct termios *opt, unsigned int databit)
|
|
{
|
|
opt->c_cflag &= ~CSIZE;
|
|
switch (databit)
|
|
{
|
|
case 8:
|
|
opt->c_cflag |= CS8;
|
|
break;
|
|
case 7:
|
|
opt->c_cflag |= CS7;
|
|
break;
|
|
case 6:
|
|
opt->c_cflag |= CS6;
|
|
break;
|
|
case 5:
|
|
opt->c_cflag |= CS5;
|
|
break;
|
|
default:
|
|
opt->c_cflag |= CS8;
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void set_parity (struct termios *opt, char parity)
|
|
{
|
|
switch (parity)
|
|
{
|
|
case'N':/* 无校验 */
|
|
case 'n':
|
|
opt->c_cflag &= ~PARENB;
|
|
break;
|
|
case'E':/*偶校验*/
|
|
case 'e':
|
|
opt->c_cflag |= PARENB;
|
|
opt->c_cflag &= ~PARODD;
|
|
break;
|
|
case'O':/* 奇校验 */
|
|
case 'o':
|
|
opt->c_cflag |= PARENB;
|
|
opt->c_cflag |= ~PARODD;
|
|
break;
|
|
default: /*其它选择为无校验 */
|
|
opt->c_cflag &= ~PARENB;
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void set_stopbit (struct termios *opt, const int stopbit)
|
|
{
|
|
switch (stopbit)
|
|
{
|
|
case 10:
|
|
opt->c_cflag &= ~CSTOPB;/*1 位停止位 t */
|
|
break;
|
|
case 15:
|
|
opt->c_cflag &= ~CSTOPB;/*1.5 位停止位 */
|
|
break;
|
|
case 20:
|
|
opt->c_cflag |= CSTOPB; /*2 位停止位 */
|
|
break;
|
|
default:
|
|
opt->c_cflag &= ~CSTOPB; /*1 位停止位 */
|
|
break;
|
|
}
|
|
}
|
|
|
|
int set_port_attr (int fd, unsigned int baudrate, int databit, const int stopbit, char parity, int vtime, int vmin )
|
|
{
|
|
struct termios opt;
|
|
tcgetattr(fd, &opt);
|
|
set_baudrate(&opt, baudrate);
|
|
//opt.c_cflag |= CLOCAL|CREAD; /*|CRTSCTS */
|
|
opt.c_lflag &= ~(ICANON | ECHO |ECHOE |ISIG);
|
|
set_data_bit(&opt, databit);
|
|
set_parity(&opt, parity);
|
|
set_stopbit(&opt, stopbit);
|
|
opt.c_iflag &=~(INLCR|ICRNL);
|
|
opt.c_iflag &=~(IXON);/* 流控*/
|
|
opt.c_oflag = 0;
|
|
//opt.c_lflag |= 0;
|
|
opt.c_oflag &= ~OPOST;
|
|
opt.c_cc[VTIME] = vtime;
|
|
opt.c_cc[VMIN] = vmin;
|
|
tcflush (fd, TCIFLUSH);
|
|
return (tcsetattr (fd, TCSANOW, &opt));
|
|
}
|
|
|
|
|
|
|
|
|