用CB改写VC串口程序时,连接错误!!
用VC写好的串口程序,现在要改写为C++ Builder5的。
编译出错:
[Linker Error] Unresolved external '__stdcall _com_issue_error(long)' referenced from C:\HP34401\HP.OBJ
[Linker Error] Unresolved external '__stdcall _com_util::ConvertStringToBSTR(const char *)' referenced from C:\HP34401\HP.OBJ
请各位大哥帮我看看,应该怎么做才能通过编译!
源代码:
#include <vcl.h>
#pragma hdrstop
#include "hp.h"
#include "stdafx.h"
//need for COM
#include "windows.h"
#include "comdef.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
const CLSID CLSID_AGTIOManager = {0xAFF8D8E1,0xCE0D,0x11D3,{0x98,0xBB,0x00,0x10,0x83,0x01,0xCB,0x39}};
const IID IID_IIOManager = {0xAFF8D8F8,0xCE0D,0x11D3,{0x98,0xBB,0x00,0x10,0x83,0x01,0xCB,0x39}};
const IID IID_IIO = {0xAFF8D8EE,0xCE0D,0x11D3,{0x98,0xBB,0x00,0x10,0x83,0x01,0xCB,0x39}};
extern "C" {
interface IComponent : IDispatch {
STDMETHOD(ComponentManufacturer)( BSTR* pVal);
STDMETHOD(ComponentDescription)( BSTR* Desc);
STDMETHOD(ComponentVersion)( BSTR* pVal);
STDMETHOD(ComponentProgID)(BSTR* pVal);
STDMETHOD(LogInterface)( VARIANT_BOOL pVal);
STDMETHOD(LogInterface)( VARIANT_BOOL* pVal);
STDMETHOD(InstanceName)( BSTR* pVal);
STDMETHOD(InstanceName)( BSTR pVal);
};
interface IIO : IComponent {
STDMETHOD(ComponentManufacturer)( BSTR* pVal);
STDMETHOD(ComponentDescription)( BSTR* Desc);
STDMETHOD(ComponentVersion)( BSTR* pVal);
STDMETHOD(ComponentProgID)(BSTR* pVal);
STDMETHOD(LogInterface)( VARIANT_BOOL pVal);
STDMETHOD(LogInterface)( VARIANT_BOOL* pVal);
STDMETHOD(InstanceName)( BSTR* pVal);
STDMETHOD(InstanceName)( BSTR pVal);
STDMETHOD(CanHandleConnectionName)(BSTR ConnectionName,VARIANT_BOOL* HandleIt);
STDMETHOD(Clear)();
STDMETHOD(Connect)(VARIANT ConnectionName);
STDMETHOD( ConnectionName)(BSTR* pVal);
STDMETHOD(DeviceLock)();
STDMETHOD(DeviceUnlock)();
STDMETHOD(Enter)( VARIANT* Result,BSTR Format);
STDMETHOD(Find)(BSTR Expression,long* Count,SAFEARRAY* Addresses);
STDMETHOD( Initialize)(/*IInitialize*/void** pVal);//modified avoid IInitalize definition
STDMETHOD(IOType)(BSTR* pVal);
STDMETHOD(Output)(VARIANT OutputString);
STDMETHOD(Read)(/*IRead*/void ** pVal);//avoid defining IRead
STDMETHOD(BufferSize)(long* pVal);
STDMETHOD(BufferSize)(long pVal);
STDMETHOD(ReadBytes)(long* Length,SAFEARRAY* Bytes);
STDMETHOD(ReadTerminator)(short* pVal);
STDMETHOD(ReadTerminator)( short pVal);
STDMETHOD(Query)(VARIANT OutputString,VARIANT* ReturnVal);
STDMETHOD(Timeout)(long* pVal);
STDMETHOD(Timeout)(long pVal);
STDMETHOD(Write)(/*IWrite*/void** pVal);//avoid IWrite definition
STDMETHOD(WriteBytes)(long Length, SAFEARRAY* Data);
STDMETHOD(WriteTerminator)(short* pVal);
STDMETHOD(WriteTerminator)(short pVal);
};
interface IEnumIO : IUnknown {
STDMETHOD(Next)( long celt, IIO** rgelt,long* pceltFetched);
STDMETHOD(Skip)( long celt);
STDMETHOD( Reset)();
STDMETHOD(Clone)(IEnumIO** rgelt);
};
interface IIOManager : IDispatch {
STDMETHOD(ConnectToInstrument)(
BSTR IOAddress,
IIO** ppIIO
);
STDMETHOD(FindSpecifiedInstruments)(
BSTR Expression,
IUnknown** ppAgtIOServers
);
STDMETHOD(FindSpecifiedInstrumentsIEnum)(
BSTR Expression,
IEnumIO** ppIEnumIO
);
};
}
void __fastcall TForm1::measureClick(TObject *Sender)
{
IIOManager *IMngr;
IIO *IDevice;
HRESULT hr;
_bstr_t myInstrument,strTmp;
_variant_t myCmd;
_variant_t myResult;
//printf("Start CExample program. We assume a 34401 voltmeter on COM1 set to 9600 baud\n\n");
//Initialize COM
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_AGTIOManager,NULL,CLSCTX_INPROC_SERVER,IID_IIOManager,(LPVOID *)&IMngr);
if FAILED(hr)
{
return ;
}
IDevice=NULL;
//this string asks for an instrument on RSR232 port 1, 9600 baud, dtr/dsr handshake
myInstrument="COM2::BAUD=9600,PARITY=EVEN,SIZE=7,HANDSHAKE=DTR_DSR";
//a GPIB address would look like "GPIB::22" etc.
hr = IMngr->ConnectToInstrument(myInstrument,&IDevice);
if (FAILED(hr) || !IDevice)
{
IMngr->Release();
return ;
}
IMngr->Release();
myCmd="*IDN?";
hr=IDevice->Query(myCmd,&myResult);//shows use of Query method
if (FAILED(hr) )
{
IDevice->Release();
return;
}
strTmp=myResult;
//now see if it contains a 34401, if so we assume we got the right instrument
/*if (strstr(strTmp,"34401") == NULL )
{
IDevice->Release ();
printf("Didn't find a 34401 voltmeter!\n");
//return 0;
}
*/
hr = IDevice->Output(_variant_t("SYST:REMOTE"));
if (FAILED(hr) )
{
IDevice->Release();
return;
}
//set the instrument to dc volts and take a measurment
hr = IDevice->Output(_variant_t("MEAS:VOLT:DC?"));
if (FAILED(hr) )
{
IDevice->Release();
return ;
}
hr=IDevice->Enter(&myResult,_bstr_t("K")); //'K' is the standard format
if (FAILED(hr) )
{
IDevice->Release();
return 0;
}
//note the myResult string will have <cr><lf> at the end.
// Edit1->Text=(char *)_bstr_t(myResult);
IDevice->Release();
//Uninit so COM is exited clean.
CoUninitialize();
//return 0;
}
[解决办法]
没有看见你的错误代码呀
[解决办法]
[Linker Error] Unresolved external '__stdcall _com_issue_error(long)' referenced from C:\HP34401\HP.OBJ
[Linker Error] Unresolved external '__stdcall _com_util::ConvertStringToBSTR(const char *)' referenced from C:\HP34401\HP.OBJ
你的程序是不是需要调用其它的dll,如果是缺少dll相关的lib文件.
如果你的dll是vc编的并且原来vc是静态调用的,建议你改为动态调用dll,因为vc和cb的lib格式不一样,即使用cb自带的工具转换后也容易出现问题.
[解决办法]
把HP.cpp和hp.h加入到当前工程中,
或者把HP34401目录加入到工程的include目录(个人建议).
[解决办法]
串口设置在Windows中直接当文件来读写就可以了。至于发送指令,只不过你要了解设备的通讯协议,发送相应的字符串而已。如AT指令集。