This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Can't read from serial port without writing to it before


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]