Linux下串口协议控制51单片机


/***************************************************************************

writen by jingshui 7-17 2011 12:53
说明:这是一个linux下串口,多线程测试程序
Version  0.26
***************************************************************************/
# include <stdio.h>
# include <unistd.h>
# include <stdlib.h>
# include <termios.h>
# include <fcntl.h>
# include <string.h>
# include <sys/time.h>
# include <sys/types.h>
# include <pthread.h>

int fd;
pthread_t thread[2];
pthread_mutex_t mutex;

/****************************************************************************
结构体说明:传送控制单片机的信息
成员1:select 选择功能模块
成员2:control 控制单片机相应的动作
*****************************************************************************/
struct protocal
{
    unsigned char select;
    unsigned char control;
};
struct protocal ptr[14]={{0xa1,0x01},{0xa1,0x02},{0xa1,0x03},{0xa1,0x04},{0xa1,0x05},
                        {0xa1,0x06},{0xa1,0x07},{0xa1,0x08},{0xb2,0x00},{0xb2,0x03},
                        {0xb2,0x06},{0xb2,0x09},{0xb2,0x0e},{0xc3,0xd4}};

/********************************************************************
功能说明:设置linux串口参数
传入参数:   
fd             nspeed     nbit        nevent      nstop
文件句柄     波特率     数据位     奇偶校验     停止位
返回值: fd 文件句柄
********************************************************************/                       
int set_port(int fd ,int nspeed ,int nbits ,char nevent ,int nstop)
{
    struct termios newtio,oldtio;
    if (tcgetattr(fd,&oldtio)!= 0)
    {
        perror("setup serial");
        return -1;
    }
    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;
    }
    switch (nevent)
    {
        case 'N':
            newtio.c_cflag &= ~PARENB;
            break;
    }
    switch (nspeed)
    {
        case 9600:
            cfsetispeed(&newtio,B9600);
            cfsetospeed(&newtio,B9600);
            break;
    }
    switch (nstop)
    {
        case 1:
            newtio.c_cflag &= ~CSTOPB;
            break;
        case 2:
            newtio.c_cflag |= CSTOPB;
            break;
    }
    newtio.c_cc[VTIME]= 0;
    newtio.c_cc[VMIN]= 14;
    tcflush(fd,TCIFLUSH);
    if ((tcsetattr(fd,TCSANOW,&newtio))!= 0)
    {
        perror("com set");
        return -1;
    }
    return 0;
}

int open_port(int fd,int comport)
{
    if (comport == 1){
        fd = open ("/dev/ttyUSB0",O_RDWR);
        if ( -1 == fd ){
            perror("can't open serial port");
            return -1;
        }
    }
    return fd;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/psfdd.html