There is no serialport object defined yet. Somewhere you need
serialPort1 = gcnew System::IO::Ports::SerialPort();
Refer to the example here:
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1[
^]
Maybe move the declaration to the class and instantiate it in the constructor.
[Edit]
Without your code I have no idea. First and most importantly I would ask why you are using C++/clr and not C#.
Here is a simple example JUST to show you the mechanism.
communicate.h
using <System.dll>
using namespace System;
using namespace System::IO::Ports;
namespace SerialCommunications
{
ref class communicate
{
public:
communicate(void)
{
serialPort1 = gcnew SerialPort();
};
private:
SerialPort^ serialPort1;
public:
void Write(array<unsigned char> ^ buffer, int offset, int count)
{
serialPort1->Write(buffer, offset, count);
}
};
}
then
/ testclr.cpp : main project file.
#include "stdafx.h"
#include "communicate.h"
using namespace System;
using namespace SerialCommunications;
int main(array<System::String ^> ^args)
{
communicate^ myCommunicate = gcnew communicate();
array<Byte>^ cmd = gcnew array<Byte>{ 0xff, 0xff, 0xff };
myCommunicate->Write(cmd, 0, 3);
return 0;
}