CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2010
    Posts
    10

    Question Using C++ for USB interfacing

    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 include .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
    Code:
    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++
    Code:
    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).
    Code:
    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.
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Using C++ for USB interfacing

    Component Initialisation from VB changed to C++
    The code you posted is managed C++, not native C++. Try asking you question here.

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Using C++ for USB interfacing

    [ Moved thread ]

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Using C++ for USB interfacing

    First create Windows Forms C++/CLI application using VC++ Application Wizard. Then add COM port stuff to it.
    Your code is not full, it is impossible to understand why it is not compiled. Possibly because of missing project reference to Windows Forms Dll.
    Side note: why do you need to use C++/CLI for this? Is there anything in C++/CLI that you need to do, that is not available in VB or C#?

  5. #5
    Join Date
    Jun 2010
    Posts
    10

    Re: Using C++ for USB interfacing

    Hi Alex, I currently have a C++ software application that I need the board to respond to. How do I add COM port stuff? I'm not sure of the COM port syntax or what to include, I've never used hardware with my C++ programs before . Btw, I realised that I can use "." instead of "::" for the statements, and changed the code somewhat, it seems to give me less errors but now, I have
    'error C2882: 'System' : illegal use of namespace identifier in expression'
    and
    'error C2228: left of '.ComponentModel' must have class/struct/union along with others. The button problems with the '->' symbols are still there too though.'

    Is there a fix for any of this? Thanks a lot for the help!!

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

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Using C++ for USB interfacing

    You cannot use . instead of "::". Post the code with full error messages, there are no code lines in error messages and code, it is difficult to understand what happens here.
    Look at C++/CLI code sampe on this page: http://msdn.microsoft.com/en-us/libr...erialport.aspx

  7. #7
    Join Date
    Jun 2010
    Posts
    10

    Re: Using C++ for USB interfacing

    Hi Alex, the full code is about 30k characters long, even after I removed unnecessary bits, I've attached it as a .txt file though. Anyway, the code below is from the start to where the error is. I've changed it a little with reference to the link you gave along with the errors that were listed.

    Code:
    // zebraDlg.cpp : implementation file
    //
    #using <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::Object;
    using namespace System::MarshalByRefObject;
    using namespace System::ComponentModel;
    using namespace System::ComponentModel::Component;
    using namespace System::ComponentModel::Container;
    using namespace System::Collections;
    using namespace System::Windows;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::IO::Ports;
    using namespace System::Threading;
    using namespace System::ComponentModel::ComponentResourceManager
    extern "C" {
    #include "hidsdi.h"
    }
    
    #pragma comment(lib, "setupapi.lib")
    #pragma comment(lib, "hid.lib")
    
    #include "bliondi.h" //self defined header
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    //class for error dialog
    class CerrorDlg : public CDialog
    {
    public:
    	CerrorDlg() :CDialog(IDD) {} //*******Updated constructor
    
    // Dialog Data
    	enum { IDD = IDD_error };
    };
    //public ref class zebra : public System::Windows::Forms::Form
    
    public ref class SerialPort : public Component;
    {
    	public:
    		void InitializeComponent()
    			{
    		this->components = gcnew System.ComponentModel.Container();
    		System.ComponentModel.ComponentResourceManager ^resources = gcnew System.ComponentModel.ComponentResourceManager(CzebraDlg.typeid);
    		this->SerialPort = gcnew System.IO.Ports.SerialPort(this->components);
    			}
    }
    
    internal:
    	{
    //VB TO C++ CONVERTER TODO TASK: In C++, an object cannot reference itself in its class-level declarations:
    array<System.IO.Ports.SerialPort^> ^SerialPort = gcnew array<System.IO.Ports.SerialPort^>(this->components + 1);
    	};
    The error messages are below

    1>.\zebraDlg.cpp(54) : error C2504: 'Component' : base class undefined
    1>.\zebraDlg.cpp(54) : error C2143: syntax error : missing ',' before ';'
    1>.\zebraDlg.cpp(65) : error C2470: 'internal' : looks like a function definition, but there is no parameter list; skipping apparent body
    1>.\zebraDlg.cpp(59) : error C2039: 'components' : is not a member of 'SerialPort'
    1> .\zebraDlg.cpp(54) : see declaration of 'SerialPort'
    1>.\zebraDlg.cpp(59) : error C2061: syntax error : identifier 'System'
    1>.\zebraDlg.cpp(60) : error C2882: 'System' : illegal use of namespace identifier in expression
    1>.\zebraDlg.cpp(60) : error C2228: left of '.ComponentModel' must have class/struct/union
    1>.\zebraDlg.cpp(60) : error C2228: left of '.ComponentResourceManager' must have class/struct/union
    1>.\zebraDlg.cpp(60) : error C2065: 'resources' : undeclared identifier
    1>.\zebraDlg.cpp(60) : error C2061: syntax error : identifier 'System'
    1>.\zebraDlg.cpp(61) : error C2273: 'function-style cast' : illegal as right side of '->' operator
    1>.\zebraDlg.cpp(61) : error C2061: syntax error : identifier 'System'

    As for the button, since an error above stated that "SerialPort1" was not part of "SerialPort", I replaced "SerialPort1" with "SerialPort" alone. It seemed to have fixed that one error but now I have a load of 'syntax errors' the code and errors are below.

    Code:
    void CzebraDlg::OnBnClickedButton5()
    {
    	 try
     {
    			if (SerialPort.IsOpen)
    			{
    				SerialPort.Close();
    			}
    			SerialPort.PortName = "COM" + IDC_EDIT2.Text;
    			SerialPort.Open();
    		}
    		catch (Exception ^ex)
    		{
    			CerrorDlg  cid;  //*** Create an instance of our dialog
    			cid.DoModal(); //Display Dialog box
    		}
    
    			
    }
    And the errors for the button now are

    1>.\zebraDlg.cpp(1691) : error C2059: syntax error : '.'
    1>.\zebraDlg.cpp(1692) : error C2143: syntax error : missing ';' before '{'
    1>.\zebraDlg.cpp(1693) : error C2143: syntax error : missing ';' before '.'
    1>.\zebraDlg.cpp(1693) : error C2143: syntax error : missing ';' before '.'
    1>.\zebraDlg.cpp(1695) : error C2143: syntax error : missing ';' before '.'
    1>.\zebraDlg.cpp(1695) : error C2143: syntax error : missing ';' before '.'
    1>.\zebraDlg.cpp(1696) : error C2143: syntax error : missing ';' before '.'
    1>.\zebraDlg.cpp(1696) : error C2143: syntax error : missing ';' before '.'


    Perhaps I failed to include something? Thanks a lot in advance!

  8. #8
    Join Date
    Jun 2010
    Posts
    10

    Re: Using C++ for USB interfacing

    Hi Alex, an update of my program. Since the last post, I've decided to junk the whole extra class and make the button immediately sync to the COM4 port. The button code is below

    Code:
    void CzebraDlg::OnBnClickedButton5()
    {
    	System.IO.Ports.SerialPort ^mySerialPort = gcnew System.IO.Ports.SerialPort("COM4");
    	try
    	{
    
    			if (mySerialPort->IsOpen)
    				{
    					mySerialPort->Close();
    				}
    			mySerialPort->Open();
    			}
    
    		catch (Exception ^ex)
    			{
    			CerrorDlg  cid;  //*** Create an instance of our dialog
    			cid.DoModal(); //Display Dialog box
    			}	
    }
    But I seem to keep getting the following errors
    1>.\zebraDlg.cpp(23) : error C2039: 'Ports' : is not a member of 'System::IO'
    1>.\zebraDlg.cpp(23) : error C2871: 'Ports' : a namespace with this name does not exist
    1>.\zebraDlg.cpp(1702) : error C2882: 'System' : illegal use of namespace identifier in expression
    1>.\zebraDlg.cpp(1702) : error C2228: left of '.IO' must have class/struct/union
    1>.\zebraDlg.cpp(1702) : error C2228: left of '.Ports' must have class/struct/union
    1>.\zebraDlg.cpp(1702) : error C2228: left of '.SerialPort' must have class/struct/union
    1>.\zebraDlg.cpp(1702) : error C2065: 'mySerialPort' : undeclared identifier
    1>.\zebraDlg.cpp(1702) : error C2061: syntax error : identifier 'System'
    1>.\zebraDlg.cpp(1710) : error C2227: left of '->IsOpen' must point to class/struct/union/generic type
    1> type is ''unknown-type''
    1>.\zebraDlg.cpp(1712) : error C2227: left of '->Close' must point to class/struct/union/generic type
    1> type is ''unknown-type''
    1>.\zebraDlg.cpp(1715) : error C2227: left of '->Open' must point to class/struct/union/generic type
    1> type is ''unknown-type''

    I'm not really sure why I'm getting the first 2 errors, the link you provided called the namespaces in the same way.

    All the namespaces and includes are below, all of them are at the start of the program. The first 2 errors I talked about about are from line "using namespace System::IO::Ports;"
    Code:
    #using <System.dll>
    #include <system>
    #include "system.dll"
    #include "serial.h"
    #include "usb-serial.h"
    #include "windows.h"
    #include "stdafx.h"
    #include "zebra.h"
    #include "zebraDlg.h"
    #include <string>
    #include <fstream>
    #include "windows.h"
    #include <iomanip>
    #include <cstdio>
    #include <cstdlib>
    #include <setupapi.h>
    #include <hidsdi.h>
    using namespace System;
    using namespace System::IO;
    using namespace System::IO::Ports;

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Using C++ for USB interfacing

    Unfortunately you are mixing elements from various languages/frameworks.

    Quote Originally Posted by meepokman View Post
    Code:
    void CzebraDlg::OnBnClickedButton5()
    This pattern of naming event handlers looks like VB .NET to me, maybe it's also used in C#. In my C++/CLI programs the header of a button click handler looks like this:

    Code:
    System::Void Progress::bnCancel_Click(System::Object^  sender, System::EventArgs^  e)
    Ok, the function name as such isn't mandatory, but that's the way the IDE generates it, and yours might not match. Also note the parameters. I'm not sure whether the function would be accepted by the framework without them, even if you don't make use of them inside the handler.

    Code:
    	System.IO.Ports.SerialPort ^mySerialPort = gcnew System.IO.Ports.SerialPort("COM4");
    This way of specifying namespaces is C#, and it's also the way it is written thoughout the MSDN docs. But in C++/CLI you'll have to change all the dots into ::.

    Code:
    			cid.DoModal(); //Display Dialog box
    DoModal() is MFC. The equivalent method of the Form class is ShowDialog().

    1>.\zebraDlg.cpp(23) : error C2039: 'Ports' : is not a member of 'System::IO'
    1>.\zebraDlg.cpp(23) : error C2871: 'Ports' : a namespace with this name does not exist

    [...]

    I'm not really sure why I'm getting the first 2 errors, the link you provided called the namespaces in the same way.
    I don't understand that either. According to http://msdn.microsoft.com/en-us/libr....io.ports.aspx System::IO::Ports::SerialPort should exist.

    All the namespaces and includes are below, all of them are at the start of the program. [...]
    The vast majority of them is not needed, at least not for the code you posted.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Jun 2010
    Posts
    10

    Re: Using C++ for USB interfacing

    Hi Eri, I'm using MFC to create the GUI. When I double click on the button in the MFC creator it gives me

    Code:
    void CzebraDlg::OnBnClickedButton5()
    How should I call the serialports function, etc? When I replace '.' with '::' (code below), I get the following errors.

    Code:
    System::IO::Ports::SerialPort ^mySerialPort = gcnew System::IO::Ports::SerialPort("COM4");
    1>.\zebraDlg.cpp(1701) : error C3083: 'Ports': the symbol to the left of a '::' must be a type
    1>.\zebraDlg.cpp(1701) : error C2039: 'SerialPort' : is not a member of 'System::IO'
    1>.\zebraDlg.cpp(1701) : error C2065: 'SerialPort' : undeclared identifier
    1>.\zebraDlg.cpp(1701) : error C2065: 'mySerialPort' : undeclared identifier
    1>.\zebraDlg.cpp(1701) : error C3083: 'Ports': the symbol to the left of a '::' must be a type
    1>.\zebraDlg.cpp(1701) : error C2039: 'SerialPort' : is not a member of 'System::IO'
    1>.\zebraDlg.cpp(1701) : error C2061: syntax error : identifier 'SerialPort'
    1>.\zebraDlg.cpp(1710) : error C2227: left of '->IsOpen' must point to class/struct/union/generic type
    1> type is ''unknown-type''
    1>.\zebraDlg.cpp(1712) : error C2227: left of '->Close' must point to class/struct/union/generic type
    1> type is ''unknown-type''
    1>.\zebraDlg.cpp(1715) : error C2227: left of '->Open' must point to class/struct/union/generic type
    1> type is ''unknown-type''

    Are my namespaces correct or do I even need to include them?

    Code:
    using namespace System;
    using namespace std;
    using namespace System;
    using namespace System::IO;
    //using namespace System::IO::Ports;
    I had to comment out the last line because it kept telling me that "Ports" was not part of "IO". Thanks in advance!

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Using C++ for USB interfacing

    Quote Originally Posted by meepokman View Post
    Hi Eri, I'm using MFC to create the GUI.
    Ok, in that case the click handler name likely is ok and the DoModal() method definitely is. I'm not sure however about how to call native functions from C++/CLI, haven't done that yet.

    How should I call the serialports function, etc? When I replace '.' with '::' (code below), I get the following errors.

    Code:
    System::IO::Ports::SerialPort ^mySerialPort = gcnew System::IO::Ports::SerialPort("COM4");
    [...]
    That leaves me puzzled. As far as I can tell, that class should exist where you referred to and be defined in System.dll, so there should be no need to refer to extra assemblies. (I haven't yet used that one myself either so far, however.)

    Are my namespaces correct or do I even need to include them?
    using namespace directives IMO are primarily for convenience (and are particularly useful in conjunction with the deeply nested namespaces of the CLR), so there is no real need for them.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  12. #12
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Using C++ for USB interfacing

    Five days after asking the question you write that you are using MFC! Good way to get help.
    If you are using MFC, forget about C++/CLI and use plain Windows serial port API. You can use also some native serial port wrappers, available at CodeGuru and CodeProject. Serial port access is not a reason to use C++/CLI.
    I wonder why the board vendor doesn't provide C++ code example. Using VB for serial port access is strange decision. Well, you are lucky that this is not JavaScript.

  13. #13
    Join Date
    Jun 2010
    Posts
    10

    Re: Using C++ for USB interfacing

    Quote Originally Posted by Alex F View Post
    Five days after asking the question you write that you are using MFC! Good way to get help.
    If you are using MFC, forget about C++/CLI and use plain Windows serial port API. You can use also some native serial port wrappers, available at CodeGuru and CodeProject. Serial port access is not a reason to use C++/CLI.
    I wonder why the board vendor doesn't provide C++ code example. Using VB for serial port access is strange decision. Well, you are lucky that this is not JavaScript.
    Hi Alex, thanks for the advice, sorry I did not mention the MFC earlier, still a bit of a noob at that.

    Anyway, I tried this: http://www.codeproject.com/KB/system...ort.aspx#Usage and have followed the class calling in the demo program

    Code:
    class CSerialPortApp : public CWinApp
    {
    public:
      virtual BOOL InitInstance();
    };
    BOOL CSerialPortApp::InitInstance()
    {
      BYTE* pBuf = new BYTE[10000];
    
      try
      {
        COMMCONFIG config;
        CSerialPort::GetDefaultConfig(1, config);
    
        CSerialPort port;
        port.Open(4, 1200, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl);
    
        HANDLE hPort = port.Detach();
        port.Attach(hPort);
    
        DWORD dwModemStatus;
        port.GetModemStatus(dwModemStatus);
    
        DCB dcb;
        port.GetState(dcb);
    
        dcb.BaudRate = 9600;
        port.SetState(dcb);    
    
        DWORD dwErrors;                      
        port.ClearError(dwErrors);
    
        port.SetBreak();
        port.ClearBreak();
    
        COMSTAT stat;
        port.GetStatus(stat);
    
        COMMTIMEOUTS timeouts;
        port.GetTimeouts(timeouts);
    
        port.Setup(10000, 10000);
    
        port.GetConfig(config);
    
        config.dcb.BaudRate = 9600;
        port.SetConfig(config);
    
        port.Set0WriteTimeout();
        port.Set0ReadTimeout();
    
        char sBuf[] = "This should appear on the serial port";
        port.Write(sBuf, strlen(sBuf));
    
        DWORD dwMask;
        port.GetMask(dwMask);
    
        port.SetMask(EV_TXEMPTY); 
    
        //port.WaitEvent(dwMask);
    
        port.TerminateOutstandingWrites();
    
        port.TransmitChar('p');
    
        port.Set0Timeout();
    
        char sRxBuf[10];
        DWORD dwRead = port.Read(sRxBuf, 10);
    
        port.TerminateOutstandingReads();
    
        port.ClearDTR();
    
        port.ClearRTS();
    
        port.SetDTR();
    
        port.SetRTS();
    
        port.SetXOFF();
    
        port.SetXON();
    
        COMMPROP properties;
        port.GetProperties(properties);
    
        port.ClearWriteBuffer();
    
        port.ClearReadBuffer();
    
        port.Flush();
    
        port.Close();
      }
       catch (CSerialException* pEx)
      {
        TRACE(_T("Handle Exception, Message:%s\n"), pEx->GetErrorMessage());
        pEx->Delete();
      }
    
      delete [] pBuf;
    };
    After doing this, I put the following code into the button

    Code:
    void CzebraDlg::OnBnClickedButton5()
    {
    	char mychar;
    	CSerialPortApp theApp;
    	 try {
        CSerialPort port;
        port.Open(4, 9600, CSerialPort::NoParity, 8, 
          CSerialPort::OneStopBit, 
          CSerialPort::XonXoffFlowControl);
    
        char sBuf[] = "@00 ON 0"; //THIS WILL BE SENT TO TURN ON RELAYS (TAKEN FROM BOARD MANUAL) 
        port.Write(sBuf, strlen(sBuf));
    	Sleep(1000);			//RELAYS WITH REMAIN ON FOR 1 SECOND
    	char sBuf2[] = "@00 OF 1";
    	port.Write(sBuf2, strlen(sBuf));
        
    }
    catch (CSerialException* pEx)
    {
       	 CerrorDlg  cid;  //*** Create an instance of our dialog
          cid.DoModal(); //Display Dialog box
    }
    	
    }
    The code compiles fine but when I click the button, nothing happens. It is meant to connect to COM4. Did I make a mistake somewhere? Thanks a lot in advance!!

  14. #14
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Using C++ for USB interfacing

    Well, according to the article rating, this is good serial port wrapper. Now, about your code. What result do you expect? You write something to the port, should device reply to this packet? You need to read this reply.
    Download Portmon program (http://technet.microsoft.com/en-us/s.../bb896644.aspx) and see whether you send something to the port, and whether device replies. Set COM port parameters exactly according to device specification.
    Run working program sample from the board vendor and see how it works in Portmon. Compare this with your program results - this can help to understand what happens.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured