X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:from:to:date:subject:message-id:content-type
	:content-transfer-encoding:mime-version; q=dns; s=default; b=Wh9
	0FSBMREk5aPv6tUUiYKEe1NW2HfDnl8OMfcW4Cf4KB6sksIT855bgI3E8V5snqDU
	NQK05ag7I3G42D/8N2PtDtYTyc7OqnMz/Fl3rsuX/MCqq1IwIZO3GkU1USIUIzDi
	ayVAVw88dw1Uo17h/CX5jOwqrw7n5tPnKJL4AmIk=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:from:to:date:subject:message-id:content-type
	:content-transfer-encoding:mime-version; s=default; bh=uZfQhTTHn
	RwnG+x/0S3JI0AXc/0=; b=OPnuSlOmRK4xVi1Xiqh8DloSXLe5y6N6STPzn6FKg
	i0T9PiiaFlvHqJFXz/6CDKBawOe++dgwrOg8uqA50l0uI7rL7YLtT1lkxwseXVBP
	1yeIby8yOHj6hdEGWd35IZhUEZnCHkdR3CW+OOvOEj3KJvisgtILYbK4vzo2ruSe
	K4=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=1.9 required=5.0 tests=AWL,BAYES_40,RP_MATCHES_RCVD autolearn=ham version=3.3.2
X-HELO: mail.ubitronix.com
From: Manuel Wienand <Manuel.Wienand@ubitronix.com>
To: "cygwin@cygwin.com" <cygwin@cygwin.com>
Date: Tue, 13 May 2014 15:31:28 +0200
Subject: Can't read from serial port without writing to it before
Message-ID: <0C11C5BF0B29FD43A8D0250F711D497FB4844C32B3@ex01-ubitronix.ubitronix.local>
Content-Type: text/plain; charset="iso-2022-jp"
MIME-Version: 1.0
X-IsSubscribed: yes
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id s4DDVvAD017200

Hello!

I have got the problem, that I can$B!F(Bt 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$B!G(Bt 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.
$B"M(B 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


