Here is an example of how to use the protocol RS232 I have tested the board ts7800 I hope you can use it. Bye
#include<unistd.h> #include<sys/types.h> #include<sys/mman.h> #include<sys/time.h> #include<stdio.h> #include<fcntl.h> #include<string.h> #include <stdlib.h> #include "PRUEBA_PUERTO_DIO_LCD.h" #include "MAPEO_MEMORIA.h" #include "spi.c" #include <time.h> #include <termios.h>
#define DEFBAUDRATE B9600 #define DEFFLAGS CS8 | CREAD | CLOCAL #define ASYNC_LOW_LATENCY 0x2000 /* Request low latency behaviour */ #define false 0 #define true 1 #define CMODE 0 // Canonical mode #define NCMODE 1 // Non-Canonical mode #define MDBMODE 2 // Multi drop mode #define ASYNC_LOW_LATENCY 0x2000 /* Request low latency behaviour */ #define PARITY_OD (PARENB | PARODD) #define CMSPAR 010000000000 #define PARITY_NO (0) #define PARITY_EV (PARENB) #define PARITY_SP (PARENB | CMSPAR)
#define PARITY_MK (PARENB | PARODD | CMSPAR)
int Baudrate; // Serial operation mode int Flags; // Serial operation mode struct termios newtio; /* New port configuration */ struct termios oldtio; /* Original port configuration */
struct timeval timeout_1; struct timeval timeout_i;
int Opened; int handler; char end_char; // End of line int Mode; // Serial operation mode #define flushSerial(fd) do { if (!isatty (fd)) tcflush(fd,TCIOFLUSH); } while (0)
void setSerialBaud(int new_baud, int flags) { int handler; Baudrate = new_baud; Flags = flags; memset((void *)(&newtio),0,sizeof(newtio)); //if(handlerTc == 0) newtio.c_cflag=new_baud | flags | ASYNC_LOW_LATENCY; newtio.c_iflag=IGNPAR; newtio.c_oflag=0;
/* Non-Canonical input */ newtio.c_lflag=0; newtio.c_cc[VTIME]=0; newtio.c_cc[VMIN] = 0; tcflush(handler,TCIFLUSH); tcsetattr(handler,TCSANOW,&newtio); }
int openSerial(const char *Port) { //int handler; /* Open and init serial */ handler=open(Port,O_RDWR|O_NOCTTY); // Open serial for R/W if ( handler < 0 ) { /* Could serial be opened? */ perror(Port); /* No -> error */ return false; } else { }; /* end if *handler<0 */ tcgetattr(handler,&oldtio); /* Save original port configuration */ Opened = true; return true; }
int Write(int *write_len,unsigned char *write_buffer) { int len = *write_len; //int handler;
*write_len = 0; flushSerial(handler); Mode = NCMODE; switch (Mode) { case MDBMODE: /* Multi drop mode */ if (len > 1) { // Set parity mark Flags = Flags & ~(PARITY_MK|PARITY_SP); Flags = Flags | (PARITY_MK); setSerialBaud(Baudrate, Flags); // Send first character write(handler, (void *)write_buffer, 1); } else { // Send first character write(handler, (void *)write_buffer, 1); *write_len = 1; return true; } // Set parity space Flags = Flags & ~(PARITY_MK|PARITY_SP); Flags = Flags | (PARITY_SP); setSerialBaud(Baudrate, Flags); len--; *write_len++; write_buffer++; break; case CMODE: /* Cannonical mode */ write_buffer[(*write_len)++]=end_char; break; } *write_len += write(handler,(void *)write_buffer,len); if (*write_len <= 0) { // Opened = false; return false; } return true; }
int Read(int *read_len,unsigned char *read_buffer) { fd_set _tty_set; /* Serial selector */ int retval,len,stay=true; struct timeval tv; fd_set read_set; timeout_1.tv_sec=1 / 1000; timeout_1.tv_usec=(1 % 1000) * 1000; timeout_i.tv_sec=1 / 1000; timeout_i.tv_usec=(1 % 1000) * 1000; //flushSerial(handler); len = 0; read_buffer--; FD_ZERO (&_tty_set); FD_SET(handler,&_tty_set); tv.tv_sec = timeout_1.tv_sec; tv.tv_usec = timeout_1.tv_usec;
do { read_set=_tty_set; retval = select(handler+1, &read_set, NULL, NULL, &tv); if (retval && FD_ISSET(handler,&read_set)) { read_buffer++; if ((retval = read(handler,(void *)read_buffer,1)) <= 0) { // Opened = false; /* Handler active, but failed */ return false; } len += retval; } else { /* No more bytes */ *read_len = len; return len; }; if (Mode == CMODE) { stay = (*read_buffer != end_char); } else { stay = (len < *read_len); } /* end if */ tv.tv_sec = timeout_i.tv_sec; tv.tv_usec = timeout_i.tv_usec; } while ( stay ); *read_len = len; return len; }
int main(void) { int ret; int tamano; int tamano_lectura; int tamano_lecturas; unsigned char buffer[124]; unsigned char buffer_rx[256]; int i=0; tamano=124; memset(buffer,' ',sizeof(buffer)); sprintf(buffer,"HOLA SOY JUAN FELIPE OBANDO \n"); ret = openSerial("/dev/ttts4"); setSerialBaud(B57600,DEFFLAGS); Write(&tamano,&buffer[0]); tamano_lectura= Read(&tamano_lecturas,&buffer_rx[0]); if (tamano_lectura==true) { printf("%s",buffer_rx); } return 0; }
Juan Felipe Obando Obando Valencia.
To: From: Date: Wed, 13 Oct 2010 04:44:35 +0000 Subject: [ts-7000] Re: Failure to read serial comm on TS-7300
Yes, this is C++. But nevermind the language and syntax, it`s not important at all. The code here is not production code. It`s just a testing program to make sure I`m able to communicate over the serial ports before I do anything for the production code. Ok, actually I`ve tried several variations of the code I posted while trying to get it to work. I do get EAGAIN. And yes, I`ve tried issuing read again after doing sleep, and of course I get nothing. I`ve also tried to use blocking mode, to which I also get nothing. The last variation I tried (and optimistic to say, with partial success) is where I perform a write and read repeatly, and then closing and opening the port again after each write and read. Doing this, I managed get the correct and expected data with every other read. Wonder why...
int main ( int argc, char** argv ) { for ( ; ; ) { int fd = open ( argv[1], O_RDWR | O_NOCTTY | O_NDELAY ) ; termios port_settings ;
cout << "fd = " << fd << endl ;
memset ( &port_settings, 0, sizeof port_settings ) ; cfsetispeed ( &port_settings, B9600 ) ; cfsetospeed ( &port_settings, B9600 ) ;
port_settings.c_cc[VMIN] = 0 ; port_settings.c_cc[VTIME] = 10 ;
port_settings.c_iflag &= ~ICRNL ; port_settings.c_cflag &= ~PARENB ; port_settings.c_cflag &= ~CSTOPB ; port_settings.c_cflag &= ~CSIZE ; port_settings.c_cflag |= CRTSCTS ; port_settings.c_cflag |= CS8 ; port_settings.c_cflag |= CREAD ; port_settings.c_cflag |= CLOCAL ;
tcsetattr ( fd, TCSANOW, &port_settings ) ; tcflush ( fd, TCIOFLUSH ) ;
char output[ ] = { 0x06, 0x01, 0x00, 0x00, 0x00, 0x10, 0x3c, 0x71 } ; int wResult ; char cA[8] ; int r ;
memset ( cA, 0, 8 ) ;
cA[7] = '\0' ; wResult = write ( fd, output, 8 ) ;
cout << "Bytes written = " << wResult << endl ;
if ( wResult == -1 ) { switch ( errno ) { case EAGAIN : cout << "eagain" << endl ; sleep ( 1 ) ; break ; case EBADF : cout << "ebadf" << endl ; break ; case EFAULT : cout << "efault" << endl ; break ; case EINTR : cout << "eintr" << endl ; break ; case EINVAL : cout << "einval" << endl ; break ; case EIO : cout << "eio" << endl ; break ; case ENOSPC : cout << "enospc" << endl ; break ; case EISDIR : cout << "eisdir" << endl ; break ; case EPIPE : cout << "epipe" << endl ; break ; default : cout << "errno = " << errno ; } }
cout << "Zzzzzzzz.........." << endl ;
usleep ( 500000 ) ;
cout << "Awaken..." << endl ; cout << "Bytes read = " << ( r = read ( fd, cA, 7 ) ) << endl ;
if ( r == -1 ) { switch ( errno ) { case EAGAIN : cout << "eagain" << endl ; sleep ( 1 ) ; break ; case EBADF : cout << "ebadf" << endl ; break ; case EFAULT : cout << "efault" << endl ; break ; case EINTR : cout << "eintr" << endl ; break ; case EINVAL : cout << "einval" << endl ; break ; case EIO : cout << "eio" << endl ; break ; case EISDIR : cout << "eisdir" << endl ; break ; default : cout << "errno = " << errno ; } }
for ( int x = 0 ; x < 8 ; ++x ) { cout << dec << static_cast<short>( cA[x] ) << "\t" << hex << static_cast<short>( cA[x] ) << "\t" << cA[x] << endl ; }
cout << "\n---END---" << endl ;
sleep ( 1 ) ; } }
Not sure if it`s worth noting, but this is the first time I`m using anything from Technologic Systems. So I`m not farmiliar with the stuff yet. Is there anything specific to TS-7300 that must be done to use the serial ports?
__._,_.___
__,_._,___
|