#include <sys/sysinfo.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/ipc.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <assert.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include <sys/msg.h>
#include <termios.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <math.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <dirent.h>
#include <ctype.h>
#include <dirent.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/stat.h>
#include "osType.h"
static int uart_dev_open(const char *path);
static int uart_dev_close(void);
static int uart_fd = -1;
static int uart_dev_open(const char *path)
{
int ret = osFailed;
uart_fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
if (uart_fd<0)
{
printf("Open Uart1 dev error!\n");
}
else
{
ret=osSucceed;
}
return ret;
}
static int uart_dev_close(void)
{
int ret = osSucceed;
close(uart_fd);
return ret;
}
static int uart_set_opt(int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr(uart_fd,&oldtio) != 0)
{
perror("SetupSerial 1");
return osFailed;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
default:
return osFailed;
}
switch( nEvent )
{
case 'O': //奇校验
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E': //偶校验
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N': //无校验
newtio.c_cflag &= ~PARENB;
break;
default:
return osFailed;
}
switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 38400:
cfsetispeed(&newtio, B38400);
cfsetospeed(&newtio, B38400);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
{
newtio.c_cflag &= ~CSTOPB;
}
else if ( nStop == 2 )
{
newtio.c_cflag |= CSTOPB;
}
newtio.c_cc[VTIME] = 2; //设置多长时间超时 百毫秒
newtio.c_cc[VMIN] = 0; //设置最少字节个数
tcflush(uart_fd,TCIFLUSH);
if((tcsetattr(uart_fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return osFailed;
}
printf("set done!\n");
return osSucceed;
}
int uart_dev_read(unsigned char *pdata, int len)
{ int ret;
ret = read(uart_fd,pdata,len);
return ret;
}
int uart_dev_write(char unsigned *data, int unsigned len)
{
int ret;
ret = write(uart_fd, data, len);
if(ret != len)
{
return osFailed;
}
return osSucceed;
}
int uart_dev_init(char *path)
{
int ret = osFailed;
if(osFailed==uart_dev_open(path))
{
goto ERROR0_EXIT;
}
if(osFailed==uart_set_opt(38400,8,'N',1))
{
goto ERROR1_EXIT;
}
ret = osSucceed;
return ret;
ERROR1_EXIT:
uart_dev_close();
ERROR0_EXIT:
return ret;
}