#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

#include "serial.h"

cSerialPort SerialPort;

void cSerialPort::openPort(char *serialPort){
	
        fd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
        fcntl(fd, F_SETFL, 0);

        tcgetattr(fd, &options);

        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;
        cfsetispeed(&options, B9600);
        cfsetospeed(&options, B9600);

        tcsetattr(fd, TCSANOW, &options);
}

void cSerialPort::writePort(char *cmd){
	int laenge;
	char *cr = "\n";
    strcat(cmd,cr);
	laenge = strlen(cmd);
	write(fd, cmd, laenge);
}

void cSerialPort::closePort(){
	close(fd);
}
