简体   繁体   中英

write to serial port while receiving tcp data

I am writing a program that is supposed to accept information from both a tcp/ip port and a serial port. It is also supposed to write to the serial port as well. I have two programs (one for serial and one for tcp/ip) but I am attempting to place them into one whole program. The result I am getting is that I will only read the serial data when tcp/ip data is read. This isn't such a problem. The big problem is that the write function doesn't seem to be working at all. Does anyone know why?

Here is the code to the method that performs the serial functions. the main method creates the tcp/ip socket and calls this method:

    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: here is the poxixComWrite() method:

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

Again, this program IS working except for the PosixComWrite() method. It does not throw an error.it returns "1". I have it writing to a micro-controller which doesn't receive the char. This WAS working when taken out of the context of the tcp/ip. Is there some weird happenings going on here?

Try to suspend the process, which writes for a few milliseconds just before writing. It will the context switch and will eliminate some of the problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM