[openbeosnetteam] Re: Serial port?
- From: "Nathan Whitehorn" <nathan.whitehorn@xxxxxxxxxxx>
- To: openbeosnetteam@xxxxxxxxxxxxx
- Date: Sat, 18 May 2002 15:11:56 EDT (-0400)
> Anyone know the ioctl's to call to setup a serial port under R5?
>
> Francois thought someone here may have them but seemed to think they
> wrere
> yet more undocumented ones :)
Define 'set up'. I have attached to this e-mail code for a class that
very much resembles BSerialPort, which may help.
-Nathan
#include <BuildMacros.h>
namespace IO {
class _EXPORT SerialPort;
}
#include <IO/SerialPort.h>
#include <Util/Exception.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
using IO::SerialPort;
SerialPort::SerialPort(Util::String device) throw (Util::Exception *) {
if (device[0] != '/') {
port_name = "/dev/ports";
port_name << device;
} else
port_name = device;
fd = open(port_name(),O_RDWR);
if (fd < 0) {
fd = errno;
throw new Util::Exception(errno);
}
}
SerialPort::SerialPort() throw () : port_name(""), fd(B_NO_INIT) {}
SerialPort::~SerialPort() throw () {
if (fd >= 0)
close(fd);
}
Util::String SerialPort::Port(bool full_path) throw () {
if (full_path)
return port_name;
return port_name.Substring(11
/*strlen("/dev/ports/")*/,port_name.Length());
}
int SerialPort::FD() throw () {
return fd;
}
ssize_t SerialPort::Read (void *buffer, size_t size) throw (Util::Exception *)
{
if (fd < 0)
throw new Util::Exception(fd);
ssize_t result = read(fd,buffer,size);
if (result < 0)
throw new Util::Exception(errno);
return result;
}
ssize_t SerialPort::Write (const void *buffer, size_t size) throw
(Util::Exception *) {
if (fd < 0)
throw new Util::Exception(fd);
ssize_t result = write(fd,buffer,size);
if (result < 0)
throw new Util::Exception(errno);
return result;
}
void SerialPort::FlushBuffer() throw (Util::Exception *) {
int result = tcflush(fd,TCIOFLUSH);
if (result < 0)
throw new Util::Exception(errno);
}
int SerialPort::WaitForInput(bigtime_t /*timeout*/) throw () {
//----timeout not supported! CBH!
int bytes = -1;
ioctl(fd,TCWAITEVENT,&bytes);
return bytes;
}
void SerialPort::SetBlocking(bool block) throw (Util::Exception *) {
int result;
if (block)
result = fcntl(fd,F_SETFL,O_RDWR);
else
result = fcntl(fd,F_SETFL,O_RDWR | O_NONBLOCK);
if (result < 0)
throw new Util::Exception(errno);
}
void SerialPort::ClearInputBuffer() throw (Util::Exception *) {
if (fd < 0)
throw new Util::Exception(fd);
if (tcdrain(fd) < 0)
throw new Util::Exception(errno);
}
void SerialPort::ClearOutputBuffer() throw (Util::Exception *) {
if (fd < 0)
throw (status_t)(fd);
if (tcdrain(fd) < 0)
throw new Util::Exception(errno);
}
void SerialPort::SetDataRate(data_rate bps) throw (Util::Exception
*) {
struct termios attr;
int result;
tcgetattr(fd,&attr);
cfsetispeed(&attr,bps);
cfsetospeed(&attr,bps);
result = tcsetattr(fd,TCSANOW,&attr);
if (result < 0)
throw new Util::Exception(errno);
}
IO::data_rate SerialPort::DataRate(void) throw () {
struct termios attr;
tcgetattr(fd,&attr);
return (data_rate)(cfgetispeed(&attr));
}
void SerialPort::SetDataBits(IO::data_bits numBits) throw
(Util::Exception *) {
struct termios attr;
int result;
tcgetattr(fd,&attr);
attr.c_cflag &= (~CSIZE);
if (numBits == B_DATA_BITS_8)
attr.c_cflag |= CS8;
result = tcsetattr(fd,TCSANOW,&attr);
if (result < 0)
throw new Util::Exception(errno);
}
IO::data_bits SerialPort::DataBits(void) throw () {
struct termios attr;
tcgetattr(fd,&attr);
return (attr.c_cflag & CS8) ? B_DATA_BITS_8 : B_DATA_BITS_7;
}
void SerialPort::SetStopBits(IO::stop_bits numBits) throw
(Util::Exception *) {
struct termios attr;
int result;
tcgetattr(fd,&attr);
attr.c_cflag &= (~CSTOPB);
if (numBits == B_STOP_BITS_2)
attr.c_cflag |= CSTOPB;
result = tcsetattr(fd,TCSANOW,&attr);
if (result < 0)
throw new Util::Exception(errno);
}
IO::stop_bits SerialPort::StopBits(void) throw () {
struct termios attr;
tcgetattr(fd,&attr);
return (attr.c_cflag & CSTOPB) ? B_STOP_BITS_2 : B_STOP_BITS_1;
}
void SerialPort::SetParityMode(parity_mode which) throw
(Util::Exception *) {
struct termios attr;
int result;
tcgetattr(fd,&attr);
attr.c_cflag &= (~(PARENB | PARODD));
switch(which) {
case B_ODD_PARITY:
attr.c_cflag &= PARODD;
//--Fall through
case B_EVEN_PARITY:
attr.c_cflag &= PARENB;
//--Do nothing for no parity
}
result = tcsetattr(fd,TCSANOW,&attr);
if (result < 0)
throw new Util::Exception(errno);
}
IO::parity_mode SerialPort::ParityMode(void) throw () {
struct termios attr;
tcgetattr(fd,&attr);
if (!(attr.c_cflag & PARENB))
return IO::B_NO_PARITY;
return (attr.c_cflag & PARODD) ? B_ODD_PARITY : B_EVEN_PARITY;
}
void SerialPort::SetDTR(bool asserted) throw (Util::Exception *) {
int result = ioctl(fd,TCSETDTR,(int)asserted);
if (result < 0)
throw new Util::Exception(errno);
}
void SerialPort::SetRTS(bool asserted) throw (Util::Exception *) {
int result = ioctl(fd,TCSETRTS,(int)asserted);
if (result < 0)
throw new Util::Exception(errno);
}
bool SerialPort::IsCTS(void) throw () {
return (ioctl(fd,TCGETBITS,TCGB_CTS) != 0);
}
bool SerialPort::IsDSR(void) throw () {
return (ioctl(fd,TCGETBITS,TCGB_DSR) != 0);
}
bool SerialPort::IsRI(void) throw () {
return (ioctl(fd,TCGETBITS,TCGB_RI) != 0);
}
bool SerialPort::IsDCD(void) throw () {
return (ioctl(fd,TCGETBITS,TCGB_DCD) != 0);
}
Other related posts: