记录
c#创建类库工程
using System;
using System.Collections.Generic;
using System.Text;
using Advantech.Adam;
using System.Runtime.InteropServices;
using System.Net.Sockets;
namespace Adam6060Com
{
[ComVisible(true)]
[Guid("88DE7087-8B80-4978-B2F4-C0937984A078")]
public interface DoAdam6060
{
[DispId(1)]
void Adam6060Init(string m_IP, int m_Port);
[DispId(2)]
void Adam6060SingleSetOut(int m_out, bool status);
[DispId(3)]
bool Adam6060ReadStatus(int m_in);
}
[ComVisible(true)]
[Guid("1AA8315B-C1A3-45C1-A9E0-9D2CC43763A1")]
public class Adam6060Operate : DoAdam6060
{
private AdamSocket adamModbus;
public void Adam6060Init(string m_IP, int m_Port)
{
adamModbus = new AdamSocket();
adamModbus.SetTimeout(1000, 1000, 1000);
if (adamModbus.Connect(m_IP, ProtocolType.Tcp, m_Port))
{
Console.WriteLine("Adam6060 Init Sucess");
}
else
{
Console.WriteLine("Adam6060 Init Fail");
}
}
public void Adam6060SingleSetOut(int m_out, bool status)
{
adamModbus.Modbus().ForceSingleCoil(m_out + 17, status);
}
public bool Adam6060ReadStatus(int m_in)
{
bool res = false;
bool[] bWDT;
adamModbus.Modbus().ReadCoilStatus(1, 6, out bWDT);
res = bWDT[m_in];
return res;
}
}
}
C++测试
#include <iostream>
#include <Windows.h>
#import "Adam6060Com.tlb"
int main() {
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(OLESTR("Adam6060Com.Adam6060Operate"), &clsid);
Adam6060Com::DoAdam6060 *ptr;
hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, __uuidof(Adam6060Com::DoAdam6060), (LPVOID*)&ptr);
ptr->Adam6060Init("192.168.1.5", 502);
CoUninitialize();
system("pause");
return 0;
}