// fSio.cpp
//
// C++ class wrapper for USB<-> Serial Driver.
//
//
#define USBDEVICE "/dev/usb/ttyUSB0"
#include <modules/fSio.h>
#include <string.h>
#include <errno.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
// constructor
fSio::fSio(const char *dev_name)
{
error = 0;
fd = open(dev_name, O_RDWR | O_NOCTTY );
if (fd < 0)
{
perror(dev_name);
exit(-1);
}
bzero(&tio, sizeof(tio)); /* clear struct for new port settings */
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
//tio.c_cflag |= (CLOCAL | CREAD);
//tio.c_cflag &= ~PARENB;
//tio.c_cflag &= ~CSTOPB;
//tio.c_cflag &= ~CSIZE;
//tio.c_cflag |= CS8;
//tio.c_cflag &= ~CRTSCTS;
//cfsetispeed(&tio, B9600);
//cfsetospeed(&tio, B9600);
//tio.c_lflag |= ICANON;
/*
initialize all control characters
default values can be found in /usr/include/termios.h
*/
tio.c_cc[VTIME] = 1; /* delay timer unused (time = 0.1s * VTIME) */
tio.c_cc[VMIN] = 0; /* blocking read until 1 character arrives or time expires */
/*
now clean the modem line and activate the settings for the port
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
printf("USB Device open\n");
};
// destructor
fSio::~fSio(void)
{};
// Serial I/O (SIO) functions
int fSio::fSioReset(int Port, int InQueue, int OutQueue)
{
if (fd > 0)
{
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
return 1;
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioDone(int Port)
{
if (fd > 0)
{
close(fd);
return 1;
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioBaud(int Port, unsigned int BaudRate)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioPutc(int Port, char Byte)
{
char buf[2];
if (fd > 0)
{
buf[0] = Byte;
return write(fd, buf, 1);
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioPuts(int Port, char * Buffer, unsigned int Size)
{
if (fd > 0)
{
return write(fd, Buffer, Size);
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
unsigned int fSio::fSioGetc(int Port)
{
unsigned char value;
if (fd > 0)
{
if (read(fd, &value, 1) <= 0)
{
error = F_READERROR;
return -1;
}
else
{
return (unsigned int) value;
}
}
else
{
error = F_DEVICENOTOPEN;
return -1;
}
}
int fSio::fSioGets(int Port, char *Buffer, unsigned int Size)
{
unsigned int i = 0;
if (fd <= 0)
{
error = F_DEVICENOTOPEN;
return -1;
}
while (i < Size)
{
if (read(fd, (void *) &Buffer[i], 1) <= 0)
{
error = F_READERROR;
return -1;
}
i++;
}
return 0;
}
int fSio::fSioDTR(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRTS(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioTxClear(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRxClear(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioTxQue(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRxQue(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioStatus(int Port, unsigned int Mask)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioFlow(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioParms(int Port, int Parity, int StopBits, int DataBits)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioCTS(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioDSR(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRI(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioDebug(int Parm)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioInfo(char Parm)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioDCD(int Port)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioBrkSig(int Port, char Cmd)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioUnGetc(int Port, char Chr)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioWinError(char * Buffer, int Size)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioRead(int Port, int Reg)
{
error = F_NOTIMPLEMENTED;
return -1;
}
int fSio::fSioEvent(int Port, long Mask)
{
/*
Let's implement this as a call to poll...
In any case I interpret the Mask as the time out
for now...
*/
error = 0;
struct pollfd ufds =
{
fd, POLLIN | POLLPRI, 0
};
int rc = poll(&ufds, 1, Mask);
if ( ufds.revents & POLLNVAL)
error = F_DEVICENOTOPEN;
if ( ufds.revents & (POLLERR | POLLHUP))
error = F_READERROR;
if (!rc)
return 0;
else
{
return ufds.revents & (POLLIN | POLLPRI) ? 1 : 0;
}
error = F_NOTIMPLEMENTED;
return 1;
}
int fSio::fSioNleft(int Port)
{
int rc, nleft;
rc = ioctl(fd, FIONREAD, &nleft);
return ( rc < 0 ? rc : nleft);
}