1

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

这是执行串行功能的方法的代码。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”。我让它写入一个不接收字符的微控制器。当从 tcp/ip 的上下文中取出时,这是有效的。这里发生了一些奇怪的事情吗?

4

1 回答 1

-2

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

于 2012-11-30T23:57:27.787 回答