Hi guys, I'm trying to link a Micro-controller board to my C++ program to control the relays on it. The vendor provided a test software in VB that involved entering the COM port number (4 in my case) and clicking a button to sync up with it, the full software and info on the board is here: [http://www.oceancontrols.com.au/KTA-223.html] under the link 'VB6 test software' link (I've also included .txt attachments of the VB code). I've tried converting the code to C++ along with using namespaces, etc, but I seem to have hit some problems. The code is pretty long so I'll post the relevant things and the errors.

Namespaces being used

using namespace System;
using namespace System::ComponentModel::Container;
using namespace System::ComponentModel;
using namespace System::Windows;
using namespace System::Windows::Forms;

Component Initialisation from VB changed to C++

public ref class zebra : public System::Windows::Forms::Form
	{
	public:
		zebra(void)
		{
			InitializeComponent();
			{
		this->components = gcnew System::ComponentModel::Container();
		System::ComponentModel::ComponentResourceManager ^resources = gcnew System::ComponentModel::ComponentResourceManager(zebra::typeid);
		this->SerialPort1 = gcnew System::IO::Ports::SerialPort(this->components);
			}
		}
}
internal:
array<System::IO::Ports::SerialPort^> ^SerialPort1 = gcnew array<System::IO::Ports::SerialPort^>(this->components + 1);

For the above code, I got the following errors:
error C3083: 'Windows': the symbol to the left of a '::' must be a type
error C3083: 'Forms': the symbol to the left of a '::' must be a type
error C2039: 'Form' : is not a member of 'System'
error C2504: 'Form' : base class undefined
error C2470: 'internal' : looks like a function definition, but there is no parameter list; skipping apparent body
error C3861: 'InitializeComponent': identifier not found
error C2039: 'components' : is not a member of 'zebra'.\zebraDlg.cpp(44) : see declaration of 'zebra'
'ComponentModel': the symbol to the left of a '::' must be a type

and many others. I suspect I may gave failed to include something or that my syntax symbols and function calls are in error.

Lastly, the button that reads the COM port number from a text box to sync with the device (code below).

void CzebraDlg::OnBnClickedButton5()
{
	 try
 {
			if (SerialPort1::IsOpen)
			{
				SerialPort1->Close();
			}
			SerialPort1->PortName = "COM" + IDC_EDIT2->Text;
			SerialPort1->Open();
		}
		catch (Exception ^ex)
		{
			CerrorDlg  cid;  //*** Create an instance of our dialog
			cid.DoModal(); //Display Dialog box
		}
	
}

For the button control, I get errors like
error C2227: left of '->Close' must point to class/struct/union/generic type
error C2227: left of '->PortName' must point to class/struct/union/generic type
and others all dealing with the '->' symbols.

I'm not really sure what the compiler is telling me but I suspect it is just a few lines I may not have. Can anyone help? The VB code I derived it from is from the link above. Thanks a lot in advance!! I'm using MFC to build the GUI.

Recommended Answers

All 5 Replies

Hi
Since moving from VB6 to C++ .Net I've ported a few of my VB apps to C++ .Net.
The approach I usually take is to start from scatch!
In your case if you haven't done so already is to download the VB.Net version from the site you linked and use that as a base to work from. You'll find this a bit easier to work with.
Create a new Windows Forms Application in C++ and duplicate all the controls on the VB.Net form onto your C++.net form. Setting all the control properties as you go.
Tip: use meaningful name for the controls (not textBox6, button13) it will make the code more readable and easier to follow especially if you have to start tracking errors.
Once you have added all the controls and before you add ANY code build it and run it. If all is well you will end up with an application with a form identical to the VB version. But of course will do abosultely nothing!
Now the hard bit!
Create the necessary events for the controls as per the VB.Net version and hand code the conversion into C++ syntax. VB.Net and C++ have little in common other than the .Net Framework so you might have the odd work-around to get things to work as they should.
This should get you started.
If you need any help let me know and I will help where I can.

Cheers
Milton

Hello Milton thanks so much for the reply, my VB is actually extremely limited, but I've done what I could above, as well as using converters for some lines I could not really figure out. I also noticed, that VB does not require me to "include" or "use namespace"s as C++ does. I think I might just have missed including something or the syntax of the pointers and symbols I used. Can you see any errors at first glance? Thanks!!

Hi
Yes quite a bit is wrong.
That's why I suggest that you create a new project from scratch. Add the controls you need and let the designer generate the code for you.
See attached Form1.h file.
Maybe this will help you more.

Hi Milton, sorry for the late reply, but in the time since, I decided to junk the whole extra class and make the button sync to COM4 straight without the class but I seem to have hit some problems. Here is the button code now

void CzebraDlg::OnBnClickedButton5()
{
	System::IO::Ports::SerialPort ^mySerialPort = gcnew System::IO::Ports::SerialPort("COM4");
	 try
			{
				
			
			if (mySerialPort->IsOpen)
				{
					mySerialPort->Close();
				}
			//mySerialPort->PortName = "COM4"// + IDC_EDIT2->Text;
			mySerialPort->Open();
			}
		catch (Exception ^ex)
			{
			CerrorDlg  cid;  //*** Create an instance of our dialog
			cid.DoModal(); //Display Dialog box
			}	
}

But when I compile it, I seem to have the following errors

1>.\zebraDlg.cpp(19) : error C2039: 'Ports' : is not a member of 'System::IO'
1>.\zebraDlg.cpp(19) : error C2871: 'Ports' : a namespace with this name does not exist
1>.\zebraDlg.cpp(1696) : error C2882: 'System' : illegal use of namespace identifier in expression
1>.\zebraDlg.cpp(1696) : error C2228: left of '.IO' must have class/struct/union
1>.\zebraDlg.cpp(1696) : error C2228: left of '.Ports' must have class/struct/union
1>.\zebraDlg.cpp(1696) : error C2228: left of '.SerialPort' must have class/struct/union
1>.\zebraDlg.cpp(1696) : error C2065: 'mySerialPort' : undeclared identifier
1>.\zebraDlg.cpp(1696) : error C2061: syntax error : identifier 'System'
1>.\zebraDlg.cpp(1701) : error C2227: left of '->IsOpen' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1703) : error C2227: left of '->Close' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1706) : error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''

The words in red are the main problem, I think the ones with the '->'s are because of them. Anyway, below are all the includes, maybe I did something wrong there?

#using <system.dll>
#include "System.dll"
#include "stdafx.h"
#include "zebra.h"
#include "zebraDlg.h"
#include <string>
#include <fstream>
#include "windows.h"
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include "usb.h"
#include <setupapi.h>
#include <hidsdi.h>
using namespace System;
using namespace System::IO;
using namespace System::IO::Ports;

Hi Milton, sorry for the late reply, but in the time since, I decided to junk the whole extra class and make the button sync to COM4 straight without the class but I seem to have hit some problems. Here is the button code now

void CzebraDlg::OnBnClickedButton5()
{
	System::IO::Ports::SerialPort ^mySerialPort = gcnew System::IO::Ports::SerialPort("COM4");
	 try
			{
				
			
			if (mySerialPort->IsOpen)
				{
					mySerialPort->Close();
				}
			//mySerialPort->PortName = "COM4"// + IDC_EDIT2->Text;
			mySerialPort->Open();
			}
		catch (Exception ^ex)
			{
			CerrorDlg  cid;  //*** Create an instance of our dialog
			cid.DoModal(); //Display Dialog box
			}	
}

But when I compile it, I seem to have the following errors

1>.\zebraDlg.cpp(19) : error C2039: 'Ports' : is not a member of 'System::IO'
1>.\zebraDlg.cpp(19) : error C2871: 'Ports' : a namespace with this name does not exist
1>.\zebraDlg.cpp(1696) : error C2882: 'System' : illegal use of namespace identifier in expression
1>.\zebraDlg.cpp(1696) : error C2228: left of '.IO' must have class/struct/union
1>.\zebraDlg.cpp(1696) : error C2228: left of '.Ports' must have class/struct/union
1>.\zebraDlg.cpp(1696) : error C2228: left of '.SerialPort' must have class/struct/union
1>.\zebraDlg.cpp(1696) : error C2065: 'mySerialPort' : undeclared identifier
1>.\zebraDlg.cpp(1696) : error C2061: syntax error : identifier 'System'
1>.\zebraDlg.cpp(1701) : error C2227: left of '->IsOpen' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1703) : error C2227: left of '->Close' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>.\zebraDlg.cpp(1706) : error C2227: left of '->Open' must point to class/struct/union/generic type
1> type is ''unknown-type''

The words in red are the main problem, I think the ones with the '->'s are because of them. Anyway, below are all the includes, maybe I did something wrong there?

#using <system.dll>
#include "System.dll"
#include "stdafx.h"
#include "zebra.h"
#include "zebraDlg.h"
#include <string>
#include <fstream>
#include "windows.h"
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include "usb.h"
#include <setupapi.h>
#include <hidsdi.h>
using namespace System;
using namespace System::IO;
using namespace System::IO::Ports;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.