/** Initialize the serial port for data transfer with ATmega88.*/
int com2_initialize(void){
int fd;
// Initialize the serial device
fd = open(COM2_DEVICE, O_RDWR | O_NOCTTY);
// Returns error if failed to initialize the device.
if(fd < 0){
writelog("rs232laser", "failed to initialize serial device, fd was %i", fd);
return 0;
}
// Get and save the current serial port settings.
tcgetattr(fd, &presentTermIO);
// Initialize structure for new port settings.
memset(&newTermIO, 0, sizeof(newTermIO));
/*
Set necessary attributes for port.
Attributes:
BAUDRATE = bps rate
CS8 = 8bit, no parity, 1 stopbit
CLOCAL = local connection, no modem
CREAD = enable port for receiving characters
*/
newTermIO.c_cflag = COM2_BAUDRATE | CS8 | CLOCAL | CREAD;
// Enable raw output.
newTermIO.c_oflag = 0;
// Set input mode to non-canonical.
newTermIO.c_lflag = 0;
// Non-canonical input processing parameters.
newTermIO.c_cc[VMIN] = COM2_MAX_INPUTSIZE;
newTermIO.c_cc[VTIME] = COM2_TIMEOUT;
// Clean the modem line and activate the settings for the port.
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newTermIO);
return fd;
}