I'll try to assemble some relevant snippets and get them to you.
Java serial port programming is a little bit of a pain - you have to download some extra libs.
If you have your heart set on Java - you can start prepping your environment to give Java access to the serial port.
Google away for that one.
C/C++ will be able to open the ports without hassle.
Jesse Peterson wrote some good Java code.
"heyu" is a C program that accesses the port - but finding what you're after in that code base can be a little daunting if you've not looked at it before.
Here, I pulled up my IDE.
This is the open() code I use:
// -----------------------------------------------------------------
int X10Controller::connect (const string portName, const int baudRate)
{
setPortName( portName );
//
// open up the serial port
struct termios adtio;
int portstatus;
if ((this->portNum = open( this->portName.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
std::cout << "Unable to open the X10Controller on serial device " << this->portName << "\n";
this->isOpen = false;
} else {
//
// We want full control of what is set and simply reset the entire adtio struct
memset( &adtio, 0, sizeof( adtio ) );
// Serial control options
adtio.c_cflag &= ~PARENB; // No parity
adtio.c_cflag &= ~CSTOPB; // One stop bit
adtio.c_cflag &= ~CSIZE; // Character size mask
adtio.c_cflag |= CS8; // Character size 8 bits
adtio.c_cflag |= CREAD; // Enable Receiver
adtio.c_cflag &= ~HUPCL; // No "hangup"
adtio.c_cflag &= ~CRTSCTS; // No flowcontrol
adtio.c_cflag |= CLOCAL; // Ignore modem control lines
// Baudrate, for newer systems
cfsetispeed( &adtio, baudRate );
cfsetospeed( &adtio, baudRate );
// Serial local options: adtio.c_lflag
// Raw input = clear ICANON, ECHO, ECHOE, and ISIG
// Disable misc other local features = clear FLUSHO, NOFLSH, TOSTOP, PENDIN, and IEXTEN
// So we actually clear all flags in adtio.c_lflag
adtio.c_lflag = 0;
// Serial input options: adtio.c_iflag
// Disable parity check = clear INPCK, PARMRK, and ISTRIP
// Disable software flow control = clear IXON, IXOFF, and IXANY
// Disable any translation of CR and LF = clear INLCR, IGNCR, and ICRNL
// Ignore break condition on input = set IGNBRK
// Ignore parity errors just in case = set IGNPAR;
// So we can clear all flags except IGNBRK and IGNPAR
adtio.c_iflag = IGNBRK|IGNPAR;
// Serial output options
// Raw output should disable all other output options
adtio.c_oflag &= ~OPOST;
adtio.c_cc[ VTIME ] = 10; // timer 1s
adtio.c_cc[ VMIN ] = 0; // blocking read until 1 char
if (tcsetattr( this->portNum, TCSANOW, &adtio ) < 0) {
// cout << "Unable to initialize serial device";
} else {
tcflush( this->portNum, TCIOFLUSH );
//
// Set DTR low and RTS high and leave other ctrl lines untouched
ioctl( this->portNum, TIOCMGET, &portstatus); // get current port status
portstatus &= ~TIOCM_DTR;
portstatus |= TIOCM_RTS;
ioctl( this->portNum, TIOCMSET, &portstatus ); // set current port status
this->isOpen = true;
}
}
return ( this->isOpen ? this->portNum : 0 );
} // open