# Serial port management in a class, constructor undefined error

**URL:** <https://discourse.processing.org/t/serial-port-management-in-a-class-constructor-undefined-error/20465>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [May 4, 2020, 11:42am UTC](https://discourse.processing.org/t/serial-port-management-in-a-class-constructor-undefined-error/20465 "2020-05-04T11:42:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderDyas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/alexanderdyas/32/9080_2.png) [@AlexanderDyas](https://discourse.processing.org/u/AlexanderDyas)\
**Post date:** [May 4, 2020, 11:42am UTC](https://discourse.processing.org/t/serial-port-management-in-a-class-constructor-undefined-error/20465/1 "2020-05-04T11:42:36Z")

</div>

Hello,

I am having trouble moving some code into a class. The code works fine outside the class, in the setup() function for instance. Within the class the following line raises an error on build :

```auto
myPort = new Serial(this, Serial.list()[serialport], baudrate);

```

Error:

```auto
The constructor Serial(class_test.mytest, String, int) is undefined

```

I’m pretty sure I’m calling it ok, as I said, the same line compiles and works fine outside the class.

What am I missing?

Thanks,

Alexander

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 4, 2020, 12:48pm UTC](https://discourse.processing.org/t/serial-port-management-in-a-class-constructor-undefined-error/20465/2 "2020-05-04T12:48:39Z")

</div>

> [@AlexanderDyas](#):
>
> I’m pretty sure I’m calling it ok, as I said, the same line compiles and works fine outside the class.

I guess the problem is that

- outside the class when you use this, it refers to the sketch
- inside the class it refers to the class.

Work-around would be to pass this to the class constructor and use there.

Chrisir

---

<div class="post-metadata">

**Author:** ![AlexanderDyas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/alexanderdyas/32/9080_2.png) [@AlexanderDyas](https://discourse.processing.org/u/AlexanderDyas)\
**Post date:** [May 4, 2020, 3:03pm UTC](https://discourse.processing.org/t/serial-port-management-in-a-class-constructor-undefined-error/20465/3 "2020-05-04T15:03:32Z")

</div>

Hi @Chrisir,

Perfect, this makes sense. I now take a PApplet type value in the constructor of my class, which I pass to the Serial method. From the main sketch I just pass `this`.

Thanks!

Alexander

---

<div class="post-metadata">

**Author:** ![AlexanderDyas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/alexanderdyas/32/9080_2.png) [@AlexanderDyas](https://discourse.processing.org/u/AlexanderDyas)\
**Post date:** [May 5, 2020, 6:33am UTC](https://discourse.processing.org/t/serial-port-management-in-a-class-constructor-undefined-error/20465/4 "2020-05-05T06:33:35Z")

</div>

Summary of working solution. We pass the parent (this, type PApplet) to the MyClass constructor, so we can then pass it to the Serial constructor.

myclass\_test.pde:

```auto
// Test of MyClass
MyClass themyclass;
int serialport=1;

void setup () {

  themyclass = new MyClass(this,serialport);

}

```

myclass.pde:

```auto
// Include Serial libraries
import processing.serial.*;

class MyClass {

  // Serial connection
  Serial myPort;
  int serialport;
  int baudrate=57600;
  char parity='N';
  int dataBits=8;
  float stopBits=1.0;

  // Constructor
  MyClass(PApplet Parent,int c_serialport) {

    // Open Serial line
    serialport=c_serialport;
    myPort=new Serial(Parent,Serial.list()[serialport],baudrate,parity,dataBits,stopBits);

  }

}

```

Cheers,

Alexander
