2

I'm trying to make the following example code work. I've read thru several tutorials and Q&A's, but I can't get it to work. In all likelyhood my understanding of classes are lacking, but I learn by doing. Hope I don't offend anybody :-)

I'm working on serial port communication, and I'm trying to use the callback version of this library: http://www.webalice.it/fede.tft/serial_port/serial_port.html

The specific question is in the commented code.

UPDATED - I figured it out, the code below is working :-)

Here's the SerialPort.h file:

#include "AsyncSerial.h"

class SerialPort
public:
    void portOpen();
    void portWrite();
private:
    CallbackAsyncSerial serial;
};

And SerialPort.cpp:

#include "SerialPort.h"

void SerialPort::portOpen() {
// serial = CallbackAsyncSerial("COM1", 115200);  Doesn't work
serial.open("COM1", 115200);  //This works :-)
}

void SerialPort::portWrite() {
    serial.writeString("Hello\n");
}

void main() {
    SerialPort objt;
    objt.portOpen();
    objt.portWrite();
}

Thanks for your help!

soje
  • 25
  • 4

2 Answers2

2

"//How do I make the object "serial" accessible in the other members?"

Make it a member variable itself

class SerialPort
public:
    void portSet();
    void portOpen();
    void portWrite();

private:
    CallbackAsyncSerial serial;
};

void SerialPort::portOpen() {
    serial = CallbackAsyncSerial("COM1", 115200);
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • @soje Did you try what I'm proposing actually? As from the [linked docs](http://www.webalice.it/fede.tft/serial_port/serial_port.html), `CallbackAsyncSerial` provides an appropriate default constructor, but I can't spot copy constructor, or assignment operator. I don't have a clue, how this should work. Maybe you've been hitting a crappy tutorial. – πάντα ῥεῖ Dec 19 '14 at 21:42
  • Sorry! I missed the change in the second part. I've updated the code above to what I'm trying now, and i get this error: ..\asyncserial.h(181): error C2248: 'boost::noncopyable_::noncopyable::operator =' : cannot access private member declared in class 'boost::noncopyable_::noncopyable' – soje Dec 19 '14 at 22:05
  • I needed a variation of the last part. I've updated the code in my question to the working version. Thanks for help, and Merry Christmas :-) – soje Dec 20 '14 at 19:05
  • @soje _"I needed a variation of the last part."_ I don't know exactly, regarding the availablilty to initialze that member using a default constructor. Anyway `CallbackAsyncSerial` claims to provide one in it's reference documentation. Glad, you could solve your problems. Merry X-Mas and have a good holydays as well ... – πάντα ῥεῖ Dec 20 '14 at 19:18
1

To make it accessible to other members it should be a member variable. That means to declare it within the class SerialPort definition.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15