亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1517 | 回復(fù): 0
打印 上一主題 下一主題

linux串口編程 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-01-25 15:11 |只看該作者 |倒序瀏覽

                下面是關(guān)于最近自己的一個串口編程程序:
               
               
                /************************************
version:0.1
date   :12/01/2010
author :yangchar
************************************/
#include stdio.h>   /* Standard input/output definitions */
#include stdlib.h>  /* Standard lib */
#include string.h>  /* String function definitions */
#include unistd.h>  /* UNIX standard function definitions */
#include fcntl.h>   /* File control definitions */
#include errno.h>   /* Error number definitions */
#include termios.h> /* POSIX terminal control definitions */
#include sys/types.h>
#include sys/select.h>
#include sys/stat.h>
#define  DEBUG  1
int  open_port(const char *dev_name)
{
        int fd,val; /* File descriptor for the port */
        fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY);
        if (-1 == fd)
        {
          perror("open_port: Unable to open tty " );
        }
        else
        {
   
        if(DEBUG)
                    printf("The %s is opened \n",dev_name);
        }
           if( (val=fcntl(fd, F_SETFL, 0)) 0)
          perror("fcntl failed");
    else
    {
        if(DEBUG)
            printf("The fcntl is %d \n",val);
    }
   
    if ( isatty(fd) == 0 )
            perror("This is not a tty device ");
    else
    {
        if(DEBUG)
            printf("isatty success! \n");
    }
        return (fd);
}
   
int  set_port (int  fd , int nSpeed , int  nBits , char nEvent , int  nStop )
{
    struct  termios new_ios,old_ios;
    if ( tcgetattr ( fd , &old_ios ) !=0 )
        perror("Save the terminal error");
    else
    {
        if(DEBUG)
            printf("Save the terminal success \n");
    }
    bzero( &new_ios , sizeof( struct termios ));
    new_ios=old_ios;
    new_ios.c_cflag |= CLOCAL |CREAD ;
    new_ios.c_cflag &= ~CSIZE ;
    switch (nBits)
    {
        case 5:
            new_ios.c_cflag |=CS5 ;
            break ;
        case 6:
            new_ios.c_cflag |=CS6 ;
            break ;
        case 7:
            new_ios.c_cflag |=CS7 ;
            break ;
        case 8:
            new_ios.c_cflag |=CS8 ;
            break ;
        default:
            perror("Wrong  nBits");
            break ;
    }
   
    switch (nSpeed )
    {
        case 2400:
            cfsetispeed(&new_ios , B2400);
            cfsetospeed(&new_ios , B2400);
            break;
        case 4800:
            cfsetispeed(&new_ios , B4800);
            cfsetospeed(&new_ios , B4800);
            break;
        case 9600:
            cfsetispeed(&new_ios , B9600);
            cfsetospeed(&new_ios , B9600);
            break;
        case 19200:
            cfsetispeed(&new_ios , B19200);
            cfsetospeed(&new_ios , B19200);
            break;
        case 115200:
            cfsetispeed(&new_ios , B115200);
            cfsetospeed(&new_ios , B115200);
            break;
        case 460800:
            cfsetispeed(&new_ios , B460800);
            cfsetospeed(&new_ios , B460800);
            break;
        default:
            perror("Wrong  nSpeed");
            break;
    }
    switch (nEvent )
    {
        case 'o':
        case 'O':
            new_ios.c_cflag |= PARENB ;
            new_ios.c_cflag |= PARODD ;
            new_ios.c_iflag |= (INPCK | ISTRIP);
            break ;
        case 'e':
        case 'E':
            new_ios.c_iflag |= (INPCK | ISTRIP);
            new_ios.c_cflag |= PARENB ;
            new_ios.c_cflag &= ~PARODD ;
            break ;
        case 'n':
        case 'N':
            new_ios.c_cflag &= ~PARENB ;
            new_ios.c_iflag &= ~INPCK  ;
            break ;
        default:
            ("Wrong nEvent");
            break ;
    }
    if ( nStop == 1 )
        new_ios.c_cflag &= ~CSTOPB ;  
    else if ( nStop == 2 )
        new_ios.c_cflag |= CSTOPB ;  
    /*No hardware control*/
    new_ios.c_cflag &= ~CRTSCTS;
    new_ios.c_cc[VTIME] = 0 ;
    new_ios.c_cc[VMIN] = 0 ;
    /*row model*/
    new_ios.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  
    new_ios.c_oflag  &= ~OPOST;   
    tcflush(fd,TCIFLUSH) ;  
    if (tcsetattr(fd,TCSANOW,&new_ios) != 0 )
    {
        perror("Set the terminal error");
        tcsetattr(fd,TCSANOW,&old_ios);
        return -1 ;
    }
    else
    {
        if(DEBUG)
            printf("Set the terminal success \n");
    }
    return  0;
}
int  main (int  argc , char *argv[])
{
    int  fd ;
    int  nwrite,nread;
    int  length;
    char buff[10];
    unsigned char  data_pan_left[7] ={0xa0,0x01,0x02,0x00,0x00,0x00,0x00};
    unsigned char  data_pan_stop[7] ={0xa0,0x01,0x0f,0x00,0x00,0x00,0x00};
    unsigned char  data_pan_right[7]={0xa0,0x01,0x01,0x00,0x00,0x00,0x00};
/*
    char buff[] ="0123456789";
    length = strlen(buff);
*/   
    char device_name[] = "/dev/ttyS1";
    if((fd=open_port(device_name))  0)
    {
        perror("Open the port error");
        exit(1);
    }
    if (set_port(fd ,19200,8,'N',1) 0 )
    {
        perror("Set the port error");
        exit(1);
    }
/*
    if( (nwrite = write(fd,buff , length ))
    nwrite=write(fd,data_pan_left,7);
            printf("write %d bytes\n",nwrite);
    sleep(10);
    nwrite=write(fd,data_pan_stop,7);
            printf("write %d bytes\n",nwrite);
    sleep(10);
    nwrite=write(fd,data_pan_right,7);
            printf("write %d bytes\n",nwrite);
    sleep(10);
    nwrite=write(fd,data_pan_stop,7);
            printf("write %d bytes\n",nwrite);
/*    while((nread=read(fd,buff,2))     
    close (fd );
    return 0 ;
}
/*Note:I write the the CSTOPB to CSTOP,so the data is error*/
更多細(xì)節(jié)請看:http://digilander.libero.it/robang/rubrica/serial.htm#config
               
               

本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u3/92500/showart_2159925.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP