This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ipnc368 rs485(uart0) communication abnormal



Hi, everyone

In ipnc368 I programing the rs485(uart0, sn65hvd11dr rs485 transever) to communication with my PC, but it is abnormal.

when PC send to ipnc, ipnc received data more than pc sent one or two bytes, and only first received byte is correct, following bytes are error(not equal to the sent).

In oscilloscope, signal waveform at  sn65hvd11dr rs485 transever pin 1(dm368_RXD0) is correct completely(include, baudrate, start bit, data bits, stop bit, .....)

when ipnc send to PC, signal waveform at  sn65hvd11dr rs485 transever pin 4(dm368_TXD0) is wrong. For baudrate=9600, no parity check, 8 data bits, 1 stop bit, 4 bytes long data, total waveform time is less than 3ms, the expection is 1/9600*4*(1+8+1) = 4.16ms.

 

according above test and measure results, I think uart driver have some problem, it seems the dm368 uart circuit's clock settings wrong. But my console(uart1) works perfectly with window xp hyper terminal.

I think the uart0 and uart1 should be use the same dirver code(It seems 8250 driver). But the fact is uart1(console) is ok, uart0 is abnormal. I DON'T change any kernel or dirver code from APPRO.

My hardware connection:    PC RS232------------- RS232/RS485 converter-------------------IPNC368 RS485 port

My test progam:

int OpenTrigCtrlPort(int baudrate, int stopbits, int parity)
{
    int fd;
    int speed, res;

    struct termios old_cfg, new_cfg;

    fd = open("/dev/tts/0", O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd < 0)
    {
        OSA_printf("Open tts/0 fail:%d\n", fd);
     return -1;
    }

   res = tcgetattr(fd, &old_cfg);
   if(res != 0)
   {
       OSA_printf("tcgetattr tts/0 fail:%d\n", res);
    return -2;
   }

   new_cfg = old_cfg;
   cfmakeraw(&new_cfg);

   switch(baudrate)
   {
       case 2400:
         speed = B2400;
    break;
       case 4800:
         speed = B4800;
    break;
       case 9600:
         speed = B9600;
    break;
       case 19200:
         speed = B19200;
    break;
       case 38400:
         speed = B38400;
    break;

       case 115200:
    default:
         speed = B115200;

   }
   cfsetispeed(&new_cfg, speed);
   cfsetospeed(&new_cfg, speed);

   new_cfg.c_cflag &= ~CSIZE;
   new_cfg.c_cflag |= CS8;
   new_cfg.c_cflag |= CLOCAL | CREAD | HUPCL;
   new_cfg.c_cflag &= ~CMSPAR;
   new_cfg.c_cflag &= ~CRTSCTS;

   switch(stopbits)
   {
       case 2:
        new_cfg.c_cflag |= CSTOPB;
    break;

    case 1:
    default:
        new_cfg.c_cflag &= ~CSTOPB;
   }

   switch(parity)
   {
        case 'e':
     case 'E':
     new_cfg.c_cflag |= PARENB;
     new_cfg.c_cflag &= ~PARODD;
     new_cfg.c_cflag |= INPCK;
     break;

     case 'o':
     case 'O':
     new_cfg.c_cflag |= PARENB;
     new_cfg.c_cflag |= PARODD;
     new_cfg.c_cflag |= INPCK;
     break;

     case 'n':
     case 'N':
     default:
     new_cfg.c_cflag &= ~PARENB;
     new_cfg.c_cflag &= ~INPCK;
     break;
   }

   new_cfg.c_cc[VTIME] = 0;//100;
   new_cfg.c_cc[VMIN] = 4;

   tcflush(fd, TCIOFLUSH);

   res = tcsetattr(fd, TCSANOW, &new_cfg);
   if(res != 0)
   {
       OSA_printf("tcsetattr fail:%d\n", res);
    return -3;
   }
   res = isatty(fd);
   OSA_printf("tts/0 isatty:%d\n", res);
   res = isatty(STDIN_FILENO);
   OSA_printf("STDIN isatty:%d\n", res);

   fcntl(fd, F_SETFL, 0);
  
   return fd;
}

// In a seperate running posix thread

fd = OpenTrigCtrlPort(9600, 1, 'N');
//CSL_gpioSetMode(&gCSL_gpioHndl, GIO_RS485_CTRL, CSL_GPIO_OUTPUT);
//CSL_gpioClr(&gCSL_gpioHndl, GIO_RS485_CTRL); // for receive
CSL_gpioSet(&gCSL_gpioHndl, GIO_RS485_CTRL); // for send


while(fd >= 0)
{
#if 0
       SnapshotTrigPktGetAndParse(fd);
    if(SnapshotTrigPktGetAndParsedOnePkt())
    {
        /* Get Pkt, maybe need a queue to buffer the comm burst */
        pSnapshotTrigInfos = SnapshotTrigPktGetPayload();
        OSA_printf("SnapshotTrig:%d,%d\n", pSnapshotTrigInfos->snapshot, pSnapshotTrigInfos->channel);
        if(pSnapshotTrigInfos->snapshot)
        {
             SnapShotJpegTrig_Set(pSnapshotTrigInfos->channel, pSnapshotTrigInfos->status, pSnapshotTrigInfos->cntrl_lamp);
        }
        SnapshotTrigPktParseCtrlReset();
    }
#else

             res1 = read(fd, buf1, 16);
//             res2 = write(fd, buf1, 4);
//             sleep(5);
printf("res1:%d, res2:%d, d:%d,%d,%d\n", res1, res2, buf1[0], buf1[1], buf1[2]);
#endif
}

I had tried vary different VTIME and VIN combinations, but result is same.

 

can anyone get some help?

 

 

  • INPCK is for new_cfg.c_iflag , not for new_cfg.c_cflag.

    I am freshman for linux programming, It seems it is easy to make this mistake.

  • hi

    according to what you said, i did .

    but when i read ,it always returns 0.,nothing data recive.

    i use

    CSL_gpioClr(&gCSL_gpioHndl, GIO_RS485_CTRL);

    set.

     

    void *TransCtrlThread(void *arg)
    {
     char recvbuf[256];
     int recvlen = 0;

     printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
     while(1)
     {
      sleep(30);

      memset(recvbuf, 0, sizeof(recvbuf));
      recvlen = 0;

      recvlen = read(fd, recvbuf, sizeof(recvbuf) - 1);

      printf("\naaaaaaaa recvlen = %d\n", recvlen);

      if(recvlen < 0)
      {
       perror("aaaaaaaa errno:");
       printf("aaaaaaaa recvlen < 0\n");
       continue;
      }
      
      printf("\naaaaaaaa recvbuf = %s\n", recvbuf);
      usleep(10000);
     }
     printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
     pthread_exit(NULL);
    }

     

    can you help me ?

    thanks a lot!