Serial communication in C ++

I am running Ubuntu 9.10 and it seems to be having problems with termios. So I can start minicomputing the serial port to 57600 Baud, 8N1, without controlling the flow of hardware or software, and it works great. I dial @ 17 5 and my device answers. When I try to configure my serial port in my C ++ code, I get no response. I know that the software communicates with the port because the LED turns on.

Here is my main one:

int main(void)
{
  int fd; /* File descriptor for the port */

  fd = open("/dev/keyspan1", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
    {
      /*
       * Could not open the port.
       */

      perror("open_port: Unable to open /dev/ttyS0 - ");
    }
  else
    fcntl(fd, F_SETFL, 0);

  /*****************************CHANGE PORT OPTIONS***************************/
  struct termios options;

  /*
   * Get the current options for the port...
   */

  tcgetattr(fd, &options);

  /*
   * Set the baud rates to 57600...
   */

  cfsetispeed(&options, B57600);
  cfsetospeed(&options, B57600);

  /*
   * Enable the receiver and set local mode...
   */

  options.c_cflag |= (CLOCAL | CREAD);

  /*
   * Set the new options for the port...
   */


  tcsetattr(fd, TCSANOW, &options);
  /***********************************END PORT OPTIONS***********************/

  int n;
  n = write(fd, "@17 5 \r", 7);
  if (n < 0)
    fputs("write() of 8 bytes failed!\n", stderr);

  char buff[20];

  sleep(1);

  n = read(fd, buff, 10);

  printf("Returned = %d\n", n);

  close(fd);

  return(0);
}

Any suggestions would be appreciated. Thank.

+3
source share
1 answer

, . cfmakeraw() . , cfmakeraw , , , .

cfmakeraw - Posix. , cfmakeraw , .

+2

Source: https://habr.com/ru/post/1763275/


All Articles