Simple C++ class example using serial port, USB, wireless…

This entry is part 2 of 2 in the series Serial communications

This post is part of the Practical C++ programming tutorial for Bioloid

Here you can find a post serie about using serial port communications with C/C++ and C#, for Windows, Linux and microcontrollers.
This code is for Windows and Visual Studio and can be used for serial cable communications, USB2Dynamixel and indeed Zigbee:

Header:

class SerialPort {
private:
HANDLE serialPortHandle;

public:
SerialPort();
~SerialPort();

int connect ();
int connect (wchar_t *device);
//int connect (char *deviceName, int baudRate, SerialParity parity);
void disconnect(void);

int sendArray(unsigned char *buffer, int len);
int getArray (unsigned char *buffer, int len);

void clear();
};

Body:


SerialPort::SerialPort() {
serialPortHandle = INVALID_HANDLE_VALUE;
}

SerialPort::~SerialPort() {
if (serialPortHandle!=INVALID_HANDLE_VALUE)
CloseHandle(serialPortHandle);

serialPortHandle = INVALID_HANDLE_VALUE;
}

int SerialPort::connect() {
return connect(L"COM1");
}

int SerialPort::connect( wchar_t* device) {
int error=0;
DCB dcb;

memset(&dcb,0,sizeof(dcb));

dcb.DCBlength = sizeof(dcb);

dcb.BaudRate = 57600;
dcb.Parity = NOPARITY;
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;

serialPortHandle = CreateFile(device, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);

if (serialPortHandle != INVALID_HANDLE_VALUE) {
if(!SetCommState(serialPortHandle,&dcb))
error=2;
}
else {
error=1;
}

if (error!=0) {
disconnect();
}
else {
clear();
}

return error;
}

void SerialPort::disconnect(void) {
CloseHandle(serialPortHandle);
serialPortHandle = INVALID_HANDLE_VALUE;

//printf("Port 1 has been CLOSED and %d is the file descriptionn", fileDescriptor);
}

int SerialPort::sendArray(unsigned char *buffer, int len) {
unsigned long result;

if (serialPortHandle!=INVALID_HANDLE_VALUE)
WriteFile(serialPortHandle, buffer, len, &result, NULL);

return result;
}

int SerialPort::getArray (unsigned char *buffer, int len) {
unsigned long read_nbr;

read_nbr = 0;
if (serialPortHandle!=INVALID_HANDLE_VALUE)
{
ReadFile(serialPortHandle, buffer, len, &read_nbr, NULL);
}

return((int) read_nbr);
}

void SerialPort::clear() {
PurgeComm (serialPortHandle, PURGE_RXCLEAR | PURGE_TXCLEAR);
}
Series Navigation<< (I) Programming serial port communications

6 comments on “Simple C++ class example using serial port, USB, wireless…

  1. Aurelien says:

    Hi there, how would I use this class with the setup PC->USB2Dynamixel->Zig2Serial->Zig100 and Zig110->CM510? I am at ease with C++ but not with communication systems…Cheers and keep up the good work!

  2. It’s easy, you always use the communications (by wire or wireless) as if you were using a file, open, read, write, close commands. You should set the proper COM (Win) tty (Linux) port and the baud rate.


Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/softwa03/public_html/softwaresouls/wp-content/plugins/most-shared-posts/msp-fetch.php on line 179

Warning: file_get_contents(http://urls.api.twitter.com/1/urls/count.json?url=http%3A%2F%2Fsoftwaresouls.com%2Fsoftwaresouls%2F2012%2F02%2F07%2Fprogramming-bioloid-choose-hardware-firmware-and-languages%2F): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/softwa03/public_html/softwaresouls/wp-content/plugins/most-shared-posts/msp-fetch.php on line 179

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/softwa03/public_html/softwaresouls/wp-content/plugins/most-shared-posts/msp-fetch.php on line 179

Warning: file_get_contents(http://urls.api.twitter.com/1/urls/count.json?url=http%3A%2F%2Fsoftwaresouls.com%2Fsoftwaresouls%2F2011%2F12%2F20%2Fc-dynamixel-reading-and-writing-example%2F): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/softwa03/public_html/softwaresouls/wp-content/plugins/most-shared-posts/msp-fetch.php on line 179
%d bloggers like this: