ts-7000
[Top] [All Lists]

Re: [ts-7000] Serial port configuration problem TS-7250

To:
Subject: Re: [ts-7000] Serial port configuration problem TS-7250
From: "Don W. Carr" <>
Date: Fri, 16 Jun 2006 14:14:14 -0500
You might try my routine, it works at higher baud rates on the TS-7300. Use a timeout of zero if you want it to wait forever on a read (slave).

int rt_open_serial(const char *port, int baud_rate, float timeout)
{
  /* The following 3 could be parameters. */
  int tmp_parity = 0; // no parity
  int tmp_data_bits = 8; // 8 data bits
  int tmp_stop_bits = 1; // 1 stop bit
  /***********/

  int fd;
  struct termios rt_tio;

  /* open the serial port */
  fd = open(port,O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY) ;
  if(fd < 0)
  {
    char buf[60];
    snprintf(buf, sizeof(buf), "Opening %s", port);
    perror(buf) ;
    exit(-1) ;
    //return -1 ;
  }
  if (0 == isatty(fd))
  {
    printf("This is NOT a tty device, will not set baud rate\n");
    return fd; // It is not a tty, you won't be able to set the baud rate
  }
  else
  {
    printf("This is a tty device\n");
  }

  /* save the old serial port settings */
  if (tcgetattr (fd,&saved_tty_parameters) < 0)
  {
    perror("Can't get terminal parameters ");
    exit(-1) ;
    //return -1 ;
  }
  /* set the fields */
  bzero(&rt_tio,sizeof(&rt_tio));

  switch (baud_rate)
  {
    case 0:
      rt_tio.c_cflag = B0;
      break;
    case 50:
      rt_tio.c_cflag = B50;
      break;
    case 75:
      rt_tio.c_cflag = B75;
      break;
    case 110:
      rt_tio.c_cflag = B110;
      break;
    case 134:
      rt_tio.c_cflag = B134;
      break;
    case 150:
      rt_tio.c_cflag = B150;
      break;
    case 200:
      rt_tio.c_cflag = B200;
      break;
    case 300:
      rt_tio.c_cflag = B300;
      break;
    case 600:
      rt_tio.c_cflag = B600;
      break;
    case 1200:
      rt_tio.c_cflag = B1200;
      break;
    case 1800:
      rt_tio.c_cflag = B1800;
      break;
    case 2400:
      rt_tio.c_cflag = B2400;
      break;
    case 4800:
      rt_tio.c_cflag = B4800;
      break;
    case 9600:
      rt_tio.c_cflag = B9600;
      break;
    case 19200:
      rt_tio.c_cflag = B19200;
      break;
    case 38400:
      rt_tio.c_cflag = B38400;
      break;
    case 57600:
      rt_tio.c_cflag = B57600;
      break;
    case 115200:
      rt_tio.c_cflag = B115200;
      break;
    case 230400:
      rt_tio.c_cflag = B230400;
      break;
    default:
      rt_tio.c_cflag = B9600;
  }
  switch (tmp_data_bits)
  {
    case 7:
      rt_tio.c_cflag = rt_tio.c_cflag | CS7;
      break;
    case 8:
    default:
      rt_tio.c_cflag = rt_tio.c_cflag | CS8;
      break;
  }
  switch (tmp_parity)
  {
    case 1:
      rt_tio.c_cflag = rt_tio.c_cflag | PARENB;
      rt_tio.c_iflag = ICRNL;
      break;
   case -1:
      rt_tio.c_cflag = rt_tio.c_cflag | PARENB | PARODD;
      rt_tio.c_iflag = ICRNL;
      break;
   case 0:
   default:
      rt_tio.c_iflag = IGNPAR | ICRNL;
      break;
  }

  if (tmp_stop_bits == 2)
     rt_tio.c_cflag = rt_tio.c_cflag | CSTOPB;
    
  rt_tio.c_cflag = rt_tio.c_cflag | CLOCAL | CREAD;
  rt_tio.c_oflag = 0;
  rt_tio.c_lflag = 0; /*ICANON;*/
  if (timeout == 0)
  {
    rt_tio.c_cc[VMIN]=1;
    rt_tio.c_cc[VTIME]=0;
  }
  else
  {
    rt_tio.c_cc[VMIN]=0;
    rt_tio.c_cc[VTIME]= (int)(timeout * 10.0);
  }

  /* flush the serial port */
  tcflush(fd, TCIFLUSH);

  if (-1 == fcntl(fd, F_SETFL, FASYNC))
  {
    perror("fcntl");
    exit(-1) ;
    //return -1 ;
  }

  /* write the settings for the serial port */
  if (tcsetattr(fd,TCSANOW,&rt_tio) <0)
  {
    perror("Can't set terminal parameters ");
    exit(-1) ;
    //return -1 ;
  }
 
  /* flush the serial port */
  tcflush(fd,TCIOFLUSH);
 
   if (rt_verbose)
   {
      printf("serial port ok:\n");
      printf("device name   %s\n", port);
      printf("speed         %d\n", baud_rate);
      printf("data bits     %d\n", tmp_data_bits);
      printf("stop bits     %d\n", tmp_stop_bits);
      printf("parity        %d\n", tmp_parity);
      printf("timeout:      %f\n", timeout);
   }
   return fd ;
}



On 6/16/06, Tony Williams <> wrote:

I wish to use the com2 port on the TS-7250 to communicate with a
linear scale at 115200 buad 8-N-1.

I commented out the line in inittab for console port on ttyAM1.

The program listing below works fine for baud rates 9600,19200 and
38400. However it fails for baud rates 57600 and 115200.

I connected an led up to the transmit pin and at 38400 buad and 128
bytes of tramitted data the transmit time is less than 1 second.
(maybe around a quarter second). However when I change

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

to use the 57600 buad rate the transmit time goes to 3 seconds and at
115200 buad rate the transmit time goes to 6 seconds.

Any help will be greatly appreciated. Full program listed below.

Tony

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/ioctls.h>

int main(void)
{
// Open serial port ttyAM1 Serial Port
//
int fd,n,i;
struct termios options;
char buf[255],*bufptr;

fd = open ("/dev/ttyAM1", O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
if (fd == -1)
{
perror ("Open port /dev/ttyAM1 failure\n");
exit (-1);
}
tcgetattr(fd,&options);
/* Set Baud rate to 115200
*******************************************/
cfsetispeed(&options, B57600);
cfsetospeed(&options, B57600);
/* Set Parity to 8-N-1
***********************************************/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* Enable the receiver and set local mode
****************************/
options.c_cflag |= ( CLOCAL | CREAD);
/* Set Flow control to none
******************************************/
options.c_cflag &= ~(IXON | IXOFF | IXANY);
/* Set read to raw input
*********************************************/
//options.c_lflag &= (ICANON | ECHO | ECHOE | ISIG);
/* Set Raw Output
options.c_oflag &= ~OPOST;
/* Write options
*****************************************************/
tcsetattr(fd,TCSANOW,&options);

n = write(fd,"Hello World Hello World Hello World
XXXXXXXXXXXXXXXXXXX
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZ\r\n",128);
printf("Bytes written %i\n",n);

/* Read input until CR or NL or Buffer full
**************************/
i = 1;
buf[0] = 0;
while (i == 1)
{
n = read(fd,buf,255);
//printf("buf = %s n = %i\n",buf,n);
if (n > 0)
break;
}

printf("Result is n = %i char = %s\n",n,buf);
close(fd);

}




--
Dr. Don W. Carr
J. G. Montenegro 2258
Guadalajara, Mexico
+52-333-630-0704
+52-333-836-4500 ext 2930 __._,_.___


SPONSORED LINKS
Linux os Hardware Arms
Computer internet Computer security Computer hardware security


YAHOO! GROUPS LINKS

  •  Visit your group "ts-7000" on the web.
     
  •  To unsubscribe from this group, send an email to:
     =Unsubscribe
     
  •  Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.



__,_._,___
<Prev in Thread] Current Thread [Next in Thread>
Admin

Disclaimer: Neither Andrew Taylor nor the University of NSW School of Computer and Engineering take any responsibility for the contents of this archive. It is purely a compilation of material sent by many people to the birding-aus mailing list. It has not been checked for accuracy nor its content verified in any way. If you wish to get material removed from the archive or have other queries about the archive e-mail Andrew Taylor at this address: andrewt@cse.unsw.EDU.AU