2014-08-27 update 3
This commit is contained in:
@@ -122,6 +122,7 @@ namespace ModBus.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
byte[] data = new byte[200];
|
byte[] data = new byte[200];
|
||||||
|
Thread.Sleep(100);
|
||||||
int i = serialPort1.Read(data, 0, serialPort1.BytesToRead);
|
int i = serialPort1.Read(data, 0, serialPort1.BytesToRead);
|
||||||
byte[] returndata = new byte[i];
|
byte[] returndata = new byte[i];
|
||||||
Array.Copy(data, 0, returndata, 0, i);
|
Array.Copy(data, 0, returndata, 0, i);
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ namespace ModBus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ModbusComProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend
|
public class ModbusRtuProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend
|
||||||
{
|
{
|
||||||
public override byte[] BytesExtend(byte[] content)
|
public override byte[] BytesExtend(byte[] content)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ namespace NA200H.UI.ConsoleApp
|
|||||||
//先初始化一个协议转换器,这里构造Modbus/Rtu协议。
|
//先初始化一个协议转换器,这里构造Modbus/Rtu协议。
|
||||||
BaseProtocal wrapper = new ModbusRtuProtocal();
|
BaseProtocal wrapper = new ModbusRtuProtocal();
|
||||||
|
|
||||||
|
/*
|
||||||
//调用方法一:手动构造
|
//调用方法一:手动构造
|
||||||
//第一步:先生成一个输入信息的object数组
|
//第一步:先生成一个输入信息的object数组
|
||||||
object[] inputObjects = new object[]{(byte)0x02,(byte)0x01,(short)0x00,(short)0x03};
|
object[] inputObjects = new object[]{(byte)0x02,(byte)0x01,(short)0x00,(short)0x03};
|
||||||
@@ -102,8 +103,15 @@ namespace NA200H.UI.ConsoleApp
|
|||||||
}
|
}
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.Read();
|
Console.Read();
|
||||||
Console.Read();
|
Console.Read();*/
|
||||||
|
|
||||||
|
ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct readHoldRegisterInputStruct = new ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct(2, "0", 4);
|
||||||
|
ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct readHoldRegisterOutputStruct = (ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadHoldRegisterModbusProtocal"], readHoldRegisterInputStruct);
|
||||||
|
for (int i = 0; i < readHoldRegisterOutputStruct.HoldRegisterStatus.Length; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(readHoldRegisterOutputStruct.HoldRegisterStatus[i]);
|
||||||
|
}
|
||||||
|
Console.Read();
|
||||||
/*
|
/*
|
||||||
//调用方法二:自动构造
|
//调用方法二:自动构造
|
||||||
//第一步:先生成一个输入结构体,然后向这个结构体中填写数据
|
//第一步:先生成一个输入结构体,然后向这个结构体中填写数据
|
||||||
|
|||||||
@@ -1,18 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
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;
|
|
||||||
using ModBus.Net;
|
using ModBus.Net;
|
||||||
|
using System.Windows;
|
||||||
|
|
||||||
|
|
||||||
namespace NA200H.UI.WPF
|
namespace NA200H.UI.WPF
|
||||||
{
|
{
|
||||||
@@ -24,14 +13,14 @@ namespace NA200H.UI.WPF
|
|||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
ModbusTCPProtocal wrapper = new ModbusTCPProtocal();
|
BaseProtocal wrapper = new ModbusRtuProtocal();
|
||||||
ReadCoilStatusTCPProtocal.ReadCoilStatusInputStruct readCoilStatusInputStruct = new ReadCoilStatusTCPProtocal.ReadCoilStatusInputStruct(11, "Q1", 3);
|
ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct readHoldRegisterInputStruct = new ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct(2, "0", 4);
|
||||||
ReadCoilStatusTCPProtocal.ReadCoilStatusOutputStruct readCoilStatusOutputStruct = wrapper.Protocals[ReadCoilStatusTCPProtocal.GetType()].SendReceive(readCoilStatusInputStruct);
|
ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct readHoldRegisterOutputStruct = (ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadHoldRegisterModbusProtocal"], readHoldRegisterInputStruct);
|
||||||
for (int i = 0; i < readCoilStatusOutputStruct.CoilStatus.Length; i++)
|
for (int i = 0; i < readHoldRegisterOutputStruct.HoldRegisterStatus.Length; i++)
|
||||||
{
|
{
|
||||||
System.Console.WriteLine(readCoilStatusOutputStruct.CoilStatus[i]);
|
Console.WriteLine(readHoldRegisterOutputStruct.HoldRegisterStatus[i]);
|
||||||
}
|
}
|
||||||
System.Console.Read();
|
Console.Read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user