.DLL文件中的串行端口

时间:2012-10-25 05:17:29

标签: visual-studio-2010 visual-c++ dll serial-port

我在Visual C ++中创建了.dll文件 它包含一个函数,它接受两个参数并通过串口发送数据。接下来,我想在我的应用程序中包含此.dll并调用这些函数。但我无法调用这些功能。请帮助。

以下是我的.dll

的标题文件
 namespace positioncontrol

{  
using namespace std;
using namespace System;
using namespace System::IO::Ports;

  public ref class control

    {   
    static int rotate(char a, String^ b);

    };
}

我的.cpp

就是.dll
   #include "goniometer.h"    

    namespace positioncontrol
    { 

   void control::rotate(char a, String^ b)
    {        SerialPort^ serialPort = gcnew SerialPort(L"COM5",9600,Parity::None,1,StopBits::One);          
             int inp_rotation;
             array<unsigned char>^ inp_c = gcnew array<unsigned char>(2);   
             String^ inp_string;                                             
             inp_c[0] = a;
             inp_string= b;                                     
             inp_rotation=Int32::Parse(inp_string);                         
             inp_c[1] = (unsigned char)inp_rotation;
             serialPort->Write(inp_c,0,2);                                         
        }
     }

我在桌面应用程序中使用此.dll。我已经包含了头文件

#ifndef goniometer_h
#define goniometer_h
#include "goniometer.h"
#endif

添加了包含目录的路径,并添加了.dll作为参考。 现在我使用.dll中定义的函数进行点击事件

private: System::Void button9_Click(System::Object^  sender, System::EventArgs^  e) {

              char dir;       
              dir = 0x42;
              String^ inp_string;                                                
              inp_string=enter_degree->Text; 
              positioncontrol::control::rotate(dir,inp_string);                                                      

         }

现在,当我构建我的桌面应用程序时,我收到了以下错误

1>C:\Users\DELL\Desktop\Final\Motor_Dual_API\Debug\goniometer.h(10): error C2011: 'positioncontrol::control' : 'class' type redefinition
1>          c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C2027: use of undefined type 'positioncontrol::control'
1>          c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C3861: 'rotate': identifier not found
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C2027: use of undefined type 'positioncontrol::control'
1>          c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C3861: 'rotate': identifier not found

请帮助我搞清楚错误。 谢谢和问候

2 个答案:

答案 0 :(得分:0)

在C ++中,命名空间分隔符为::而不是.。查看您的using声明。

答案 1 :(得分:0)

您的代码未定义serialPort。将定义添加到全局命名空间,命名空间positionControl或作为函数中的自动变量

从语义的角度来看,serialPort是一个指针。因此,还必须创建一个对象的实例,其中serialPort指向。