2 Star 1 Fork 5

風過^^晌午/Uart_to_uart

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
uart_api.c 3.70 KB
一键复制 编辑 原始数据 按行查看 历史
風過^^晌午 提交于 2019-12-04 14:05 . 安卓下串口相互转发
#include "uart_api.h"
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define bzero(buffer, size) memset(buffer, 0, size)
#define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))
fd_set mRd;
struct timeval mTimeout;
static speed_t getBaudrate(int baudrate)
{
switch(baudrate){
case 0: return B0;
case 50: return B50;
case 75: return B75;
case 110: return B110;
case 134: return B134;
case 150: return B150;
case 200: return B200;
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 1800: return B1800;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
case 230400: return B230400;
case 460800: return B460800;
case 500000: return B500000;
case 576000: return B576000;
case 921600: return B921600;
case 1000000: return B1000000;
case 1152000: return B1152000;
case 1500000: return B1500000;
case 2000000: return B2000000;
case 2500000: return B2500000;
case 3000000: return B3000000;
case 3500000: return B3500000;
case 4000000: return B4000000;
default: return -1;
}
}
int uart_open(char *devname)
{
int fd=0;
fd=open(devname,O_RDWR | O_NOCTTY | O_NDELAY);
if (fd > 0)
{
/*ָΪ״̬*/
if(fcntl(fd, F_SETFL, 0)<0)printf("fcntl failed!\n");
else printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
}
else
{
printf("Can't open %s\n",devname);
fd = -1;
//exit(1);
}
return fd;
}
static void xraw(int fd, const char *name, speed_t speed,
struct termios *original)
{
struct termios t;
if (tcgetattr(fd, &t)) printf("tcgetattr %s", name);
*original = t;
cfmakeraw(&t);
if (speed) cfsetspeed(&t, speed);
if (tcsetattr(fd, TCSAFLUSH, &t)) printf("tcsetattr %s", name);
}
int uart_cfg(int fd,int inSpeed)
{
int speeds[] = {50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800,
500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
2500000, 3000000, 3500000, 4000000};
struct termios newtio,oldtio;
static struct termios original_fd_state;
int i, speed = inSpeed;
for (i = 0; i < (int)ARRAY_LEN(speeds); i++) if (speeds[i] == speed) break;
if (i == ARRAY_LEN(speeds)) printf("unknown speed: %d", speed);
speed = i+1+4081*(i>15);
if (-1==(i = fcntl(fd, F_GETFL, 0)) || fcntl(fd, F_SETFL, i&~O_NDELAY))
printf("fcntl error:%s %d \n",__FUNCTION__,__LINE__);
// Set both input and output to raw mode.
xraw(fd, "fd",speed,&original_fd_state);
return 0;
}
int uart_send(int fd,unsigned char *pcnBuf,int inLen)
{
int count = 0;
while (count<inLen) {
int i = write(fd, count+pcnBuf, inLen-count);
if (i<1) return i;
count += i;
}
return count;
}
int uart_recv(int fd,unsigned char *pcnBuf,int inLen)
{
int count = 0;
while (count<inLen) {
int i = read(fd, (char *)pcnBuf+count, inLen-count);
if (!i) break;
if (i<0) return i;
count += i;
}
return count;
}
int uart_mselect(int fd,int inTimeoutMs)
{
int retval;
FD_ZERO(&mRd);
FD_SET(fd,&mRd);
mTimeout.tv_sec = inTimeoutMs/1000;
mTimeout.tv_usec = (inTimeoutMs%1000)*1000;
retval = select(fd+1, &mRd, NULL, NULL, &mTimeout);
return (retval);
}
int uart_mflush(int fd)
{
return tcflush(fd,TCIFLUSH);
}
int uart_close(int fd)
{
close(fd);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/opensource_ipcamera/Uart_to_uart.git
git@gitee.com:opensource_ipcamera/Uart_to_uart.git
opensource_ipcamera
Uart_to_uart
Uart_to_uart
master

搜索帮助