Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi there,

I am writing a C++ application to send data down a serial port. My problem arises when I attempt to create a file with all my commands in and link that to my main file. I need to access the serialport1 object defined here in my main form "Communicate.h" (I changed the scope from private to public):

C++
public: System::IO::Ports::SerialPort^  serialPort1;


I need to access this in Commands.cpp I try it like this:

C++
#include "Commands.h"
#include "Communicate.h"

using namespace SerialCommunications;
using namespace System;
using namespace System::IO::Ports;

void elbow(int dir, int amt)
{
    array<Byte>^ cmd = { 0xff, 0xff, 0xff };
    Communicate::serialPort1->Write(cmd, 0, 3);
}


However I just get this error message trying to access it:

"Error: a nonstatic member reference must be relative to a specific object"

Thanks in advance!!
Max
Posted
Updated 14-Jun-15 21:34pm
v3
Comments
nv3 15-Jun-15 3:36am    
Looks like you have to instantiate the serialPort1 object first.

There is no serialport object defined yet. Somewhere you need

C#
// Create a new SerialPort object with default settings.
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

C++
using <System.dll>

using namespace System;
using namespace System::IO::Ports;

namespace SerialCommunications
{
	ref class communicate
	{
	public:
		communicate(void)
		{
			// Create a new SerialPort object with default settings.
			serialPort1 = gcnew SerialPort();
		};

	private:
		SerialPort^ serialPort1;
	public:

		void Write(array<unsigned char> ^ buffer, int offset, int count)
		{
			serialPort1->Write(buffer, offset, count);
		}
	};
}


then

C++
/ 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;
}
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 15-Jun-15 0:08am    
Sure, a 5.
—SA
[no name] 15-Jun-15 11:21am    
Sorry I forgot to add the code for the definition for the seiralport1 it is in the Communicate.h file:

this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));

I don't understand what you mean by move the declaration to the class because I need to access the serialPort1 object from within the form communicate.h.

Thanks again,
Max
[no name] 15-Jun-15 20:44pm    
Your questions raise some questions but I have added an edit.
Thank you very much for your response! I have managed to fix the issue by changing the:

public: System::IO::Ports::SerialPort^  serialPort1;

to:
public: static System::IO::Ports::SerialPort^  serialPort1;


Thank you very much,
Max
 
Share this answer
 
Comments
Philippe Mori 16-Jun-15 19:51pm    
Usually, you want to avoid static variable as it can make the code harder to reuse in another context.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900