使用C#通过modbus TCP协议实现对可编程马达控制器的控制

tuguojiao 2015-06-30 03:48:50
各位大神,我现在在用WPF/C#做一个客户端的界面,实现通过modbus tcp的协议对我的马达控制器的控制,但是好像并没有找到合适的modbus tcp的动态链接库。我利用的modbus_vc.dll可以连接上设备,但是发送十进制的指令并没有回应。十进制的指令应该是没有问题的,我用modbus的第三方软件测试过的。我不知道是我的dll动态链接库有问题,还是我的指令发送有错误。以下是我的C#程序。请问我错在哪个环节了,请问有大牛可以给我指点一下这个dll动态链接库里的函数的参数都是什么意思吗?有没有大神有合适的动态链接库可以发给我用一下。我的邮箱tuguojiao@126.com qq:675849603,如果可以通过qq给我指导一下就更好了,求求各位大神了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

//ShellExecute
using System.Runtime.InteropServices;

namespace ModbusControlledBrowser
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

// import external dll
[DllImport("modbus_vc.dll", EntryPoint = "Connect", CallingConvention = CallingConvention.Cdecl)]
public static extern int Connect(string ip, int port, char[] Msg);

[DllImport("modbus_vc.dll", EntryPoint = "Disconnect", CallingConvention = CallingConvention.Cdecl)]
public static extern int Disconnect(char[] Msg);

[DllImport("modbus_vc.dll", EntryPoint = "WriteCoil", CallingConvention = CallingConvention.Cdecl)]
public static extern int WriteCoil(int adress, int value, char[] Msg);

[DllImport("modbus_vc.dll", EntryPoint = "ReadCoil", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int ReadCoil(int adress, int* value, char[] Msg);

[DllImport("modbus_vc.dll", EntryPoint = "WriteRegisterWord", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int WriteRegisterWord(int adress, int value, char[] Msg);

[DllImport("modbus_vc.dll", EntryPoint = "ReadOutputRegisterWord", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int ReadOutputRegisterWord(int adress, int* value, char[] Msg);

//Update Information in CPX
char[] Msg = new char[256];
int error;

public unsafe MainWindow()
{
InitializeComponent();


error = Connect("192.168.0.104", 502, Msg);

}

Private void Button_Click_1(object sender, RoutedEventArgs e)
{
int sideIndex;
error = ReadOutputRegisterWord(0, &sideIndex, Msg);

error = WriteRegisterWord(3, 55, Msg);

int prgError;
error = ReadCoil(2, &prgError, Msg);

error = WriteCoil(1, 0, Msg);
}

}
}
...全文
526 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧