无法在序列上重复写入字符串



在Ubuntu Focal PC上,我正在通过USB将字符串写入另一台设备(在我的情况下是Arduino Due(。我发送的字符串在另一端接收良好,但我不能再写了。原因是我的程序在第一次写入时就挂断了,无法退出。它很好地发送了第一个字符串,但不知何故挂断了。大多数可以更改的端口设置都是指接收数据,但我只是在发送,所以我真的不知道为什么我的write((会被占用。我的代码相当标准:

// * * * * * * Serial functions * * * * * 
int set_interface_attribs(int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                printf("error %d from tcgetattr", errno);
                return -1;
        }
        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);
        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as 00 chars
        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 20;            // 0.5 seconds read timeout
        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;
        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                printf("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}//end set_interface_attribs()

void set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                printf("error %d from tggetattr", errno);
                return;
        }
        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                printf("error %d setting term attributes", errno);
}//end set_blocking()

 
// * * * * *  Main * * * * * * * * * *

int main(int argc, char** argv)
{
  
  printf("entering main n");
  
 
  char *portname = "/dev/ttyACM0";
  //std::string portname = "/dev/ttyACM0";
 
  int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0)
  {
        printf("error %d opening %s: %s", errno, portname, strerror (errno));
        return 0;
  }
  set_interface_attribs (fd, 9600, 0);  // set speed to 9600 bps, 8n1 (no parity)
  set_blocking (fd, 0);                // set no blocking
 
  
  while(true)
  { 
    printf("entering write() n");
    
    int n = write(fd, "#MCM;-061.456;976543.2@", 23);  // sends well 1st time but gets hung up    
    if(n<=0) 
    {
        cout << n << "write error n";
        return 0;
    } 
    usleep ((7 + 25) * 100);             // sleep enough to transmit the 7 plus
                                     
    sleep(1000); //just for testing    
  }//end while()
   
   return 0;
}//end main()

问题已解决。睡眠时间(1000(是1000秒,而不是我想象的1000毫秒。此外,我在字符串中添加了两个字符:'\r'和'\n',因此我将23增加到了25。现在工作正常。

相关内容

  • 没有找到相关文章

最新更新