在接收tcp数据时写入串行端口

时间:2012-11-30 23:53:26

标签: c sockets serial-port posix

我正在编写一个程序,它应该接受来自tcp / ip端口和串行端口的信息。它也应该写入串口。我有两个程序(一个用于串口,一个用于tcp / ip)但我试图将它们放入一个完整的程序中。我得到的结果是,我只会在读取tcp / ip数据时读取串行数据。这不是一个问题。最大的问题是写入功能似乎根本不起作用。有谁知道为什么?

以下是执行串行功能的方法的代码。 main方法创建tcp / ip套接字并调用此方法:

    int work()
    {

      //set up the serial port
       int result = 0;
       int portID = -1;
       char *device = "/dev/ttyUSB1";
       int rate = convertRate("115200");
       char parity = convertParity("N");
       int databits = convertDatabits("8");
       int stopbits = convertStopbits("1");
       portID = posixComOpen(device,rate,parity,databits,stopbits);


      maxslot = listener;
      do
      {

        fd_set set;
        int i;

        FD_ZERO(&set);

        FD_SET(1,        &set);
        FD_SET(listener, &set);
        for(i=0; i < SLOTCOUNT; i++)
        {
          if(slots[i])
          {
            FD_SET(slots[i], &set);
          }
        }

        int koliko = select(maxslot+1, &set, NULL, NULL, NULL);
        if(koliko==-1)
          err(5, "select()");

        if(FD_ISSET(1, &set))
        {

          int size;
          if(ioctl(1, FIONREAD, &size) != -1){
            char *buf;

        //    printf("Reading %d bytes...\n", size);
            buf = malloc(size+1);
            read(1, buf, size);
            buf[size] = 0;
        //    printf("Received %s\n", buf);

            broadcast_message(0, buf);

            free(buf);
          }
          else
            err(7, "ioctl() stdio");

        }
        if(FD_ISSET(listener, &set))
        {
          int accepted = accept(listener, NULL, NULL);
          if(accepted == -1)
            err(6, "accept()");

          slots[accepted] = accepted;
          if(accepted > maxslot)
            maxslot = accepted;
        }

        for(i = 0; i < SLOTCOUNT; i++)
        {
          if(slots[i] && FD_ISSET(slots[i], &set))
          {
        //    printf("Received on %d\n", i);


            int size;


            if(ioctl(i, FIONREAD, &size) != -1){

              if(size > 0)
              {
                char *buf;

        //        printf("Reading %d bytes...\n", size);
                buf = malloc(size+1);
                read(i, buf, size);
                buf[size] = 0;
        //        printf("Received %s\n", buf);

                broadcast_message(i, buf);

                free(buf);
              }
              else
              {
                shutdown(i, SHUT_RDWR);
                close(i);

                slots[i] = 0;

        //        printf("Disconnect on %d\n", i);
              }
            }
            else
              err(8, "ioctl() net");


          }
        }
        //recieve and send data to serial
char input =0;

        while(posixComDataReady(portID) && posixComRead(portID, &input)) {
        printf("%c", input);
        //fflush(stdout);
          } //while

         /* Write character to Vex */
         if(posixComWrite(portID, 'x') < 0)
           printf("Error Writing char %c",'x');
         //end serial code

      } while(1);
      return 0;
    }

EDiT:这是poxixComWrite()方法:

char posixComWrite(int port, char src) {
return (write(port, &src, 1) == 1);
} //posixComWrite 

同样,除了PosixComWrite()方法之外,这个程序正在工作。它不会抛出错误。它返回“1”。我把它写到一个没有收到char的微控制器上。当从tcp / ip的上下文中取出时,此WAS正在工作。这里发生了一些奇怪的事情吗?

1 个答案:

答案 0 :(得分:-2)

尝试暂停进程,该进程在写入之前写入几毫秒。它将上下文切换并消除一些问题。