博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux下串口编程之二:读串口和写串口
阅读量:4102 次
发布时间:2019-05-25

本文共 1811 字,大约阅读时间需要 6 分钟。

1,打开串口

      /**打开串口,dev 串口设备名, mode 打开方式,**/

int opendev(char *dev,mode_t mode)

    {
          int fd;
          fd = open(dev, mode);
          if (-1 == fd){   
                perror("Can't Open Serial Port");
                return -1; 
          }
          else{
                 fcntl(fd, F_SETFL, FNDELAY);
                 return fd;
          }
     }

2,写串口

    #define FALSE  -1

    #define TRUE   0
    #define NET_PORT 19988
    #define MAX_BUF_SIZE 4096
    struct net2net_buf{
           int len;
           char buf[MAX_BUF_SIZE];
     };

     #define RSDEV_NAME "/dev/ttyS1"

     int main(void)

     {
            int rsfd = 0;
            int nwrite;
            char input_buf[64];
            rsfd = opendev(RSDEV_NAME,O_RDWR | O_NOCTTY | O_NDELAY);
            if(rsfd < 0){
                  printf("open error:/n");
                  exit(-1);
            }
            set_speed(rsfd,9600);    /*设置速率B9600*/
            if (set_parity(rsfd,8,1,'N') == FALSE){ /*8位数据位,一位停止位*/
                   printf("Set Parity Error/n");
                   exit (-1);
            }
            while(1)

           {

                   fgets(input_buf,sizeof(input_buf),stdin);
                   printf("input_buf = %s", input_buf);
                   nwrite = write(rsfd, input_buf, strlen(input_buf));
                   if ( nwrite == -1 ) {
                        perror("ERROR!");
                        exit(1);
                   }

                   else {

                        printf("ret=%d/n", nwrite);
                   }
            } 
      }

3,读串口

      int read_rs232(int rsfd)

      {
             int     retbytes = 0,nread = 0;
             int all_bytes = 0;
             struct  net2net_buf netbuf;
             int i;
             memset(&netbuf,0,sizeof(netbuf));
             while((nread = read(rsfd, &netbuf.buf[retbytes], 512))>0)
             {
                    printf("nread:%d/n",nread);
                    retbytes += nread;
                    all_bytes  += nread;
              }
              return all_bytes;
        }
        int main(void)
       {
              int rsfd = 0;
              int nwrite;
              fd_set  fdR;
              struct timeval  timev;
              int n = 0;
              char input_buf[64];
              rsfd = opendev(RSDEV_NAME,O_RDWR | O_NOCTTY | O_NDELAY);
              if(rsfd < 0){
                   printf("open error:/n");
                   exit(-1);
              }
              set_speed(rsfd,9600);    /*设置速率B9600*/
              if (set_parity(rsfd,8,1,'N') == FALSE){ /*8位数据位,一位停止位*/
                    printf("Set Parity Error/n");
                    exit (-1);
              }
              while(1) {
                    FD_ZERO(&fdR);
                    FD_SET(rsfd, &fdR);
                    timev.tv_sec  = 0;
                    timev.tv_usec = 100000;
                    n = select(rsfd+1, &fdR, NULL, NULL, &timev);
                    if(n <= 0)
                         continue;
                    if (FD_ISSET(rsfd,&fdR)) {
                         printf("rs232 recv/n");
                         read_rs232(rsfd);
                    }
               } 
       }

转载地址:http://lbbsi.baihongyu.com/

你可能感兴趣的文章
AS3中的TextField文本事件
查看>>
C++多继承相关
查看>>
屏蔽ADB自动更新
查看>>
PHP与MYSQL事务处理(附:mysql存储过程基本函数)
查看>>
纯AS3倒影类
查看>>
Flash AS3)actionScript代码制作文字渐变+描边
查看>>
【转】AS3)灰化显示对象(DisplayObject)的方法 / 图片灰化
查看>>
AS3内存泄露
查看>>
AS3——Socket
查看>>
Dic和Object
查看>>
AS3基础
查看>>
AS3事件详解
查看>>
AS3:dispatchEvent与事件流,点击事件的穿透性
查看>>
AS3事件之旅
查看>>
AS3数字取整
查看>>
Windows中cmd操作mysql
查看>>
《As3 Expert》_1 :数组操作符的用途,如何访问不可访问的东东?
查看>>
《AS3 Expert》_2:FB与FD代码智能提示的隐匿陷阱
查看>>
《AS3 Expert》_3:为什么for不能有序遍历数组的所有元素?
查看>>
AS3实用函数、AS3常用算法大全
查看>>