I3CPAC+-+Exercise+12

=Exercise 12 - Seriel Interface=

Fælles klasse Serial Communication
Vi har oprettet en enkelt klasse til at håndtere den serielle kommunikation:

HEADER FIL: code format="cpp"
 * 1) include   /* Standard input/output definitions */
 * 2) include  /* String function definitions */
 * 3) include  /* UNIX standard function definitions */
 * 4) include   /* File control definitions */
 * 5) include   /* Error number definitions */
 * 6) include  /* POSIX terminal control definitions */


 * 1) include

using namespace std;

class SerialCom { public: SerialCom; ~SerialCom;

int openSerialPort(string usbPort, int mode, speed_t baudRate, tcflag_t databits, bool blocking);

void writeSerialPort(string data, size_t dataSize); char readSerialPort(char* dataBuffer);

void closeSerialPort;

private: int fd_; string filePath_; struct termios options_; } code CPP FIL: code format="cpp"
 * 1) include "SerialCom.h"

SerialCom::SerialCom {	fd_ = 0; filePath_ = ""; }

SerialCom::~SerialCom {

}

int SerialCom::openSerialPort(string usbPort, int mode, speed_t baudRate, tcflag_t databits, bool blocking) {	filePath_ = "/dev/" + usbPort;

fd_ = open(filePath_.c_str, mode); if(fd_ == -1) perror("openSerialPort: Unable to open");

/*	Setting Baud Rate */

tcgetattr(fd_, &options_); cfsetispeed(&options_, baudRate); cfsetospeed(&options_, baudRate);

/*	Setting Character Size */	options_.c_cflag &= ~CSIZE; 	/* Mask the character size bits */ options_.c_flag |= databits;	/* Select 8 data bits */

/*	Setting Parity to None */	options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB;

/*	Setting the options_ */	tcsetattr(fd_, TCSANOW , &options_);

if(blocking) fcntl(fd, F_SETFL, FNDELAY); else fcntl(fd, F_SETFL, 0); }

void SerialCom::writeSerialPort(string data, size_t dataSize) {	char* buffer = (char*)data.c_str;

int bytesSent = (fd_, buffer, dataSize);

if(bytes < 0) cout << "writeSerialPort failed" << std::endl; }

char SerialCom::readSerialPort(char* dataBuffer) {	char bytesRecv; bytesRecv = read(fd_, &dataBuffer, 60);

if(bytesRecv < 0) cout << "readSerialPort failed" << std::endl;

return bytesRecv; }

void SerialCom::closeSerialPort {	close(fd_); } code Herefter oprettede vi et main for en writer/server og reader/client.

Writer
code format="cpp"
 * 1) include "SerialCom.h"

int main {	SerialCom SC;

int error = SC.openSerialPort("ttyS0",O_RDWR, B115200, CS8, true);

if(error == -1) cout << "Unable to open the port" << endl; else cout << "Port openened" << endl;

cout << "Writing to SerialPort" << endl;

SC.writeSerialPort("Sendt fra Writer",sizeof("Sendt fra Writer")-1);

} code

Reader
code format="cpp"
 * 1) include "SerialCom.h"

int main {	SerialCom SC;

int error = SC.openSerialPort("ttyS0",O_RDWR, B115200, CS8, true);

if(error == -1) cout << "Unable to open the port" << endl; else cout << "Port openened" << endl;

char buffer[50]; char* bufPtr = buffer;

while(1) {			SC.readSerialCom(bufPtr);

cout << "Message received: " << bufPtr << endl;

} } code