ts-7000
[Top] [All Lists]

Re: [ts-7000] TS-7260

To:
Subject: Re: [ts-7000] TS-7260
From: "Don W. Carr" <>
Date: Thu, 17 Aug 2006 17:26:04 -0500
The following is my routine for opening a serial port which works very well on the TS-7300. Set the timeout to 0 if you want to wait forever to recieve characters. Don.


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

struct termios saved_tty_parameters; /* saved serial port setting */
int rt_verbose;


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 8/17/06, weide72 <> wrote:

I am trying to communicate with a device that measures voltage,
amps, etc. via COM2 on the TS-7260. The device needs a 4-byte array
sent to it and responds with a 3-byte array containing the requested
information. I am pretty sure that I have intialized all the
settings for COM2 correctly. I guess I'm not sure what the correct
way to send the 4-byte array is or if I need to do anything more to
set up COM2 besides in code. Can anyone help? My code is as
follows:

#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)
{
int fd,n;

char send[] = {129, 3, 2, 121}; //array to request average battery 1
volts

printf("Bytes sent %s\n", send);
struct termios options;
char buf[2];

fd = open("/dev/ttyAM1", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
if (fd == -1)
{
perror ("Open port /dev/ttyAM1 failure\n");
return -1;
}

tcgetattr(fd,&options);

/* Set Baud rate to 2400bps
*******************************************/
cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);

/* Enable the receiver and set local mode
****************************/
options.c_cflag |= ( CLOCAL | CREAD);

/* Set Parity to 8-N-1
***********************************************/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

/* Set Software and Hardare Flow control to none
******************************************/
options.c_iflag &= ~(IXOFF | IXOFF | IXANY);
options.c_cflag &= ~CRTSCTS;

/* Set read to raw input
*********************************************/
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

/* Set Raw Output
*********************************************/
options.c_oflag &= ~OPOST;

/* Set timeout to 1 second
*****************************************************/
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;

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

/* Save options
*****************************************************/
tcsetattr(fd,TCSANOW,&options);

n = write(fd,send,4);

n = read(fd,buf,3);
printf("Bytes read %i\n",n);
printf("buf = %s\n" ,buf);
close(fd);
return 0;
}

The program returns value for the write but nothing is ever read.
Also, for connecting the COM2 header to a DB-9 I have the following:
COM2 Pin2(RXD) = DB9 Pin2(RXD)
COM2 Pin3(TXD) = DB9 Pin3(TXD)
COM2 Pin5(GND) = DB9 Pin5(GND)
Thanks for the help.

Ryan Weidemann




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


SPONSORED LINKS
Single board computer Hardware Computer running slow
Linux os Single board


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