linux 串口

#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;
}

 

转载于:https://www.cnblogs.com/lort/p/8781318.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值