代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。