Linux下串口编程之二:读串口和写串口

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值