title: Linux UART transmitting Problems
body: <p>I'm trying to communicate with an external IC via UART/tty in C. I'm using a nanoPI neo2 (allwinner h5) and friendlyARM's own Linux OS (which is a modified Ubuntu core).</p>
  
  <p>I'v tried to get it working, but the TX output of the pi is ?logical inverted?. You can see what I mean in the oscilloscope pictures. There you can see if I select the "Polarity Invert"- Mode the data is ok, but in normal mode it isn't working.</p>
  
  <p>In the past, I got around that by setting the invert Bit of the MCU (e.g. PIC MCU), but the IC I'm using now doesn't support this feature.</p>
  
  <p><a href="http://wiki.friendlyarm.com/wiki/index.php/NanoPi_NEO2" rel="nofollow noreferrer">http://wiki.friendlyarm.com/wiki/index.php/NanoPi_NEO2</a></p>
  
  <p><a href="https://hieber.xyz/DSX_QuickPrint3_1.jpg" rel="nofollow noreferrer">oscilloscope picture 1</a></p>
  
  <p><a href="https://hieber.xyz/DSX_QuickPrint2_1.jpg" rel="nofollow noreferrer">oscilloscope picture 2</a></p>
  
  <p><a href="https://gist.github.com/jerome-labidurie/5dde0ec105fe88aaf5fa8f3c54f4b07d" rel="nofollow noreferrer">c source</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  //#include &lt;errno.h&gt;
  //#include &lt;termios.h&gt;
  //#include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  
  
  #include &lt;fcntl.h&gt;  /* File Control Definitions          */
  #include &lt;termios.h&gt;/* POSIX Terminal Control Definitions*/
  #include &lt;unistd.h&gt; /* UNIX Standard Definitions         */
  #include &lt;errno.h&gt;  /* ERROR Number Definitions          */
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/signal.h&gt;
  /* source : http://stackoverflow.com/a/6947758 */
  
  int fd;
  
  int set_interface_attribs (int fd, int speed, int parity) {
          struct termios tty;
          memset (&amp;tty, 0, sizeof tty);
          if (tcgetattr (fd, &amp;tty) != 0) {
                  perror ("error from tcgetattr");
                  return -1;
          }
  
          cfsetospeed (&amp;tty, speed);
          cfsetispeed (&amp;tty, speed);
  
          tty.c_cflag = (tty.c_cflag &amp; ~CSIZE) | CS8;     // 8-bit chars
          // disable IGNBRK for mismatched speed tests; otherwise receive break
          // as \000 chars
          tty.c_iflag &amp;= ~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] = 5;            // 0.5 seconds read timeout
  
          tty.c_iflag &amp;= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  
          tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                          // enable reading
          tty.c_cflag &amp;= ~(PARENB | PARODD);      // shut off parity
          tty.c_cflag |= parity;
          tty.c_cflag &amp;= ~CSTOPB;
          tty.c_cflag &amp;= ~CRTSCTS;
  
          if (tcsetattr (fd, TCSANOW, &amp;tty) != 0) {
                  perror ("error from tcsetattr");
                  return -1;
          }
          return 0;
  }
  
  void set_blocking (int fd, int should_block) {
          struct termios tty;
          memset (&amp;tty, 0, sizeof tty);
          if (tcgetattr (fd, &amp;tty) != 0)
          {
                  perror ("error from tggetattr");
                  return;
          }
  
          tty.c_cc[VMIN]  = should_block ? 1 : 0;
          tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
  
          if (tcsetattr (fd, TCSANOW, &amp;tty) != 0)
                  perror ("error setting term attributes");
  }
  
  int main()
  {
      char *portname = "/dev/ttyS0";
      int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
      if (fd &lt; 0)
      {
              //perror ("error %d opening %s: %s", errno, portname, strerror (errno));
              return;
      }
      set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
      set_blocking (fd, 0);                // set no blocking
      write (fd, "hello!\n", 7);           // send 7 character greeting
      usleep ((7 + 25) * 100);             // sleep enough to transmit the 7 plus
                                           // receive 25:  approx 100 uS per char transmit
      char buf [100];
      int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read
  
  }
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p>Axel</p>