Hi,
i'm working on a driver for the ORGA ECO 5000 cardreader.
But the "serial.c" - file (provided with the CT-skeleton sources) doesn't
seem to work.
I get a handle to "/dev/ttyS0" for example, but other functions (like
tcgetattr(handle, ...)) shutdown with an error (-1).
Where is the problem ??
I changed some part of the sources (eliminated MAC support), but this is (I
hope) not the reason.
I also have to set the RTS-Line off to communicate with the cardreader.
Otherwise i don't get a response. (ACK or NACK) Which parameters do i have to
change or set in the termios-structure.
I attached the modified "serial.c"-file. Please take a look at it.
If there are any questions, please contact me!!
Thank you so much for your support. ;->
Bye, Frank
/*
* NAME:
* serial.c -- Copyright (C) 1998 David Corcoran
*
* DESCRIPTION:
* This provides Unix/Mac serial driver support
*
* AUTHOR:
* David Corcoran, 7/22/98
*
* Modified by Mark Hartman for Macintosh support, 7/15/98
*
* LICENSE: See file LICENSE.
*
*/
#include <stdio.h> /* Standard Includes */
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include "serial.h"
#include <termios.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/time.h>
struct IO_Specs {
HANDLE handle;
BYTE baud;
BYTE bits;
char parity;
long blocktime;
} ioport;
static char _rcsid[] UNUSED = "$Id$";
/*
* InititalizePort -- initialize a serial port
* This functions opens the serial port specified and sets the
* line parameters baudrate, bits per byte and parity according to the
* parameters. If no serial port is specified, the Communications
* Manager's dialog is invoked to permit the user to select not only
* the port, but all the other (baud/bits/parity) details as well.
* Selections made by the user in this case will override the
* parameters passed.
*/
bool
IO_InitializePort(int baud, int bits, char parity, char* port)
{
/* UNIX SERIAL SUPPORT */
HANDLE handle;
struct termios newtio;
handle = open(port, O_RDWR); /* Try user input depending on port */
if (handle < 0) { /* Problems with /dev/smartcard */
return -1;
}
if(tcgetattr(handle, &newtio)<0) {
close(handle);
return -100;
};
/*
* Set the baudrate
*/
switch (baud) {
case 9600: /* Baudrate 9600 */
newtio.c_cflag = B9600;
break;
case 19200: /* Baudrate 19200 */
newtio.c_cflag = B19200;
break;
default:
close(handle);
return -2;
}
/*
* Set the bits.
*/
switch (bits) {
case 5: /* Five bits */
newtio.c_cflag |= CS5;
break;
case 6: /* Six bits */
newtio.c_cflag |= CS6;
break;
case 7: /* Seven bits */
newtio.c_cflag |= CS7;
break;
case 8: /* Eight bits */
newtio.c_cflag |= CS8;
break;
default:
close(handle);
return -3;
}
/*
* Set the parity (Odd Even None)
*/
switch (parity) {
case 'O': /* Odd Parity */
newtio.c_cflag |= PARODD | PARENB | INPCK;
break;
case 'E': /* Even Parity */
newtio.c_cflag &= (~PARODD);
newtio.c_cflag |= PARENB | INPCK;
break;
case 'N': /* No Parity */
break;
default:
close(handle);
return -4;
}
/*
* Setting Raw Input and Defaults
*/
newtio.c_cflag |= CREAD|HUPCL|CLOCAL;
newtio.c_iflag &= ~(IGNPAR|PARMRK|INLCR|IGNCR|ICRNL);
newtio.c_iflag |= BRKINT;
newtio.c_lflag &= ~(ICANON|ECHO|ISTRIP);
newtio.c_cflag |=CRTSCTS;
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
if (tcflush(handle, TCIFLUSH) < 0) { /* Flush the serial port */
close(handle);
return -5;
};
if(tcsetattr(handle, TCSANOW, &newtio) < 0) {
close(handle);
return -6;
};
ioport.handle = handle; /* Record the handle */
ioport.baud = baud; /* Record the baudrate */
ioport.bits = bits; /* Record the bits */
ioport.parity = parity; /* Record the parity */
ioport.blocktime = 1; /* Default Beginning Blocktime */
return TRUE;
}
HANDLE
IO_ReturnHandle() {
return ioport.handle; /* Return the current used handle */
}
int
IO_UpdateReturnBlock(int blocktime) { /* Sets the blocking timeout value */
ioport.blocktime = (long)blocktime; /* For Unix seconds by default */
return ioport.blocktime; /* Return the current blocktime */
}
int
IO_ReturnBaudRate() {
return ioport.baud; /* Return the current baudrate */
}
bool
IO_FlushBuffer() {
HANDLE handle = ioport.handle;
if (tcflush(handle, TCIFLUSH) == 0)
return TRUE;
return FALSE;
}
bool
IO_Read( int readsize, BYTE *response ) {
fd_set rfds;
struct timeval tv;
int rval;
BYTE c;
HANDLE handle;
int count = 0;
handle = ioport.handle;
tv.tv_sec = ioport.blocktime;
tv.tv_usec = 0;
FD_ZERO(&rfds);
for (count=0; count < readsize; count++) {
FD_SET(handle, &rfds);
rval = select (handle+1, &rfds, NULL, NULL, &tv);
if (FD_ISSET(handle, &rfds)) {
read(handle, &c, 1);
response[count] = c;
}
else {
tcflush(handle, TCIFLUSH);
return FALSE;
}
}
return TRUE;
}
bool
IO_Write(BYTE c) {
HANDLE handle = ioport.handle;
tcflush(handle, TCIFLUSH); /* Flush the port */
if (write(handle, &c, 1) == 1) /* Write one byte */
return TRUE;
return FALSE;
}
bool
IO_Close() {
HANDLE handle = ioport.handle;
if ( close (handle) == 0 ) { /* Close the handle */
return TRUE;
} else {
return FALSE; /* Done ! */
}
}