public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Manuel Wienand <Manuel.Wienand@ubitronix.com>
To: "cygwin@cygwin.com" <cygwin@cygwin.com>
Subject: Can't read from serial port without writing to it before
Date: Tue, 13 May 2014 14:19:00 -0000	[thread overview]
Message-ID: <0C11C5BF0B29FD43A8D0250F711D497FB4844C32B3@ex01-ubitronix.ubitronix.local> (raw)

Hello!

I have got the problem, that I can‘t read from a serial port without writing at least one byte to it before.
Another workaround is to use an terminal application (I tried with HTerm) and connect (and then disconnect again) to the COM port, this seems to fix it for the next run.
Closing the COM port properly before exiting the program doesn’t seem to have any effect on the faulty behaviour.

Test case:
PC:
Win7 (x64), Cygwin 1.7.29 (x32)

Using a serial cable to connect to a development board which is just spamming data over the serial.
⇒ PC reads only, board writes only.
The code of the PC program is attached at the end of this mail.

I hope you are able to reproduce and fix this problem.

Thanks in advance.

Manuel



#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <malloc.h>

int kbport = -1;

int posix_com_close(int port)
{
  int rtnval = 0;
  if (port != -1)
  {
    rtnval = (int) close(port);
  }

  return(rtnval);
}

int posix_com_open(char *devicename)
{
  int rtnval = 0;
  struct termios t;

  int local_databits = CS8; // 8 Databits.
  int local_stopbits = 0;   // 1 Stopbit
  int local_parity = 0;     // No parity
  int local_rate = B9600;   // 9600 Baud

  /* Open the com port for read/write access */

  rtnval = open(devicename,O_RDWR);

  t.c_iflag = IGNPAR;
  t.c_oflag = 0;
  t.c_cflag &= ~CSIZE; /* Zero out the CSIZE bits */
  t.c_cflag = CLOCAL | local_databits | local_stopbits | local_parity;
  t.c_lflag = IEXTEN;

  if ((cfsetispeed(&t,local_rate) != -1) && (cfsetospeed(&t,local_rate) != -1))
  {
    if (tcsetattr(rtnval,TCSANOW,&t) == -1)
      rtnval = -1;
  }
  else
    rtnval = -1;

  return(rtnval);
}

char posix_com_read(int port, char *dst)
{

  if (port != -1)
    return(read(port,dst,1) == 1);
  else
    return(0);
}

char posix_com_write(int port, char src)
{
  if (port != -1)
    return(write(port,&src,1) == 1);
  else
    return(0);
}


int main(void)
{
  int myport = 0;
  char inchar = 0;
  int i=0;

  myport = posix_com_open("/dev/ttyS0");

  puts("Starting to receive...");fflush(stdout);
  char test = 'A';
  //posix_com_write(myport, test); // When using this line, reading will always work.

  if (myport != -1)
  {

    while (i<20)
    //while (1) // Since closing the COM port doesn't help, we can use this instead.
    {
      i++;
      if (posix_com_read(myport,&inchar))
      {
        if (isprint(inchar))
          printf("%c",inchar);
        else
          printf("<%03d>",inchar);
        fflush(stdout);
      }

    }
    posix_com_close(myport);  // Doesn't help.
    return EXIT_SUCCESS;
  }

  return EXIT_SUCCESS;
}


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

                 reply	other threads:[~2014-05-13 13:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0C11C5BF0B29FD43A8D0250F711D497FB4844C32B3@ex01-ubitronix.ubitronix.local \
    --to=manuel.wienand@ubitronix.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).