2014-08-25 update 1

This commit is contained in:
parallelbgls@outlook.com
2014-08-25 16:21:57 +08:00
parent ad05e3f770
commit aa1cbf85ba
6 changed files with 421 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using ModBus.Net;
using System.Reflection;
using ModBus.Net.Prodave;
namespace NA200H.UI.ConsoleApp
{
@@ -12,8 +13,64 @@ namespace NA200H.UI.ConsoleApp
{
static void Main(string[] args)
{
/*
string ip = "192.168.4.247";
try
{
BaseProtocal wrapper = new ModbusTcpProtocal(ip);
Console.WriteLine("link ip is {0}",ip);
//第一步先生成一个输入信息的object数组
object[] inputObjects = new object[] {(byte) 0x11, (byte) 0x01, (short) 0x13, (short) 0x25};
//第二步:向仪器发送这个信息,并接收信息
byte[] outputBytes = wrapper.SendReceive(inputObjects);
//第三步:输出信息
for (int i = 0; i < outputBytes.Length; i++)
{
Console.WriteLine(outputBytes[i]);
}
Console.WriteLine("modbus link success");
}
catch (Exception)
{
Console.WriteLine("modbus link failed");
}
Console.Read();
*/
string ip = "192.168.3.246";
try
{
Console.WriteLine("link ip is {0}",ip);
short ConNr = 63; // First connection(0 63)(max. 64 connections).
string AccessPoint = "S7ONLINE"; // Default access point——S7ONLINE
Prodave6.CON_TABLE_TYPE ConTable; // Connection table
int ConTableLen = System.Runtime.InteropServices.Marshal.SizeOf(typeof (Prodave6.CON_TABLE_TYPE));
// Length of the connection table
int RetValue;
string[] splitip = ip.Split('.');
ConTable.Adr = new byte[] {byte.Parse(splitip[0]), byte.Parse(splitip[1]), byte.Parse(splitip[2]), byte.Parse(splitip[3]), 0, 0};
ConTable.AdrType = 2; // Type of address: MPI/PB (1), IP (2), MAC (3)
ConTable.SlotNr = 2; // 插槽号
ConTable.RackNr = 0; // 机架号
RetValue = Prodave6.LoadConnection_ex6(ConNr, AccessPoint, ConTableLen, ref ConTable);
if (RetValue > 0) throw new Exception();
//以下测试SetActiveConnection_ex6
UInt16 UConNr = (UInt16) ConNr;
RetValue = Prodave6.SetActiveConnection_ex6(UConNr);
if (RetValue > 0) throw new Exception();
Console.WriteLine("prodave link success");
}
catch (Exception)
{
Console.WriteLine("prodave link failed");
}
Console.Read();
//先初始化一个协议转换器这里构造Modbus/Tcp协议。
BaseProtocal wrapper = new ModbusTcpProtocal();
/*BaseProtocal wrapper = new ModbusTcpProtocal();
//调用方法一:手动构造
//第一步先生成一个输入信息的object数组
@@ -53,7 +110,7 @@ namespace NA200H.UI.ConsoleApp
Console.Read();
Console.Read();
ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct readHoldRegisterInputStruct = new ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct(0x11, "MW1", 8);
ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct readHoldRegisterInputStruct = new ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct(0x11, "0", 8);
ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct readHoldRegisterOutputStruct = (ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadHoldRegisterModbusProtocal"], readHoldRegisterInputStruct);
for (int i = 0; i < readHoldRegisterOutputStruct.HoldRegisterStatus.Length; i++)
{
@@ -124,7 +181,60 @@ namespace NA200H.UI.ConsoleApp
Console.WriteLine(setSystemTimeOutputStruct.StartAddress);
Console.WriteLine(setSystemTimeOutputStruct.WriteCount);
Console.Read();
Console.Read();
Console.Read();*/
/*short ConNr = 63; // First connection(0 63)(max. 64 connections).
string AccessPoint = "S7ONLINE"; // Default access point——S7ONLINE
Prodave6.CON_TABLE_TYPE ConTable;// Connection table
int ConTableLen = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Prodave6.CON_TABLE_TYPE));// Length of the connection table
int RetValue;
//ConTable.Adr = new byte[] { 115, 238, 36, 68, 0, 0 };
ConTable.Adr = new byte[] { 192, 168, 3, 246, 0, 0 };
ConTable.AdrType = 2; // Type of address: MPI/PB (1), IP (2), MAC (3)
ConTable.SlotNr = 2; // 插槽号
ConTable.RackNr = 0; // 机架号
RetValue = Prodave6.LoadConnection_ex6(ConNr, AccessPoint, ConTableLen, ref ConTable);
//以下测试SetActiveConnection_ex6
UInt16 UConNr = (UInt16)ConNr;
RetValue = Prodave6.SetActiveConnection_ex6(UConNr);
//以下测试db_write_ex6
UInt16 BlkNr = 1;//data block号
Prodave6.DatType DType = Prodave6.DatType.BYTE;//要读取的数据类型
UInt16 StartNr = 0;//起始地址号
UInt32 pAmount = 20;//需要读取类型的数量
UInt32 BufLen = 200;//缓冲区长度(字节为单位)
//参数data block号、要写入的数据类型、起始地址号、需要写入类型的数量、缓冲区长度字节为单位、缓冲区
byte[] pWriteBuffer = new byte[5];
for (int i = 0; i < pWriteBuffer.Length; i++)
pWriteBuffer[i] = (byte)(1);
//RetValue = Prodave6.db_write_ex6(BlkNr, DType, StartNr, ref pAmount, BufLen, pWriteBuffer);
//以下测试db_read_ex6
//参数data block号、要读取的数据类型、起始地址号、需要读取类型的数量、缓冲区长度字节为单位
//缓冲区、缓冲区数据交互的长度
byte[] pReadBuffer = new byte[200];
UInt32 pDatLen = 0;
//RetValue = Prodave6.as200_field_read_ex6(Prodave6.FieldType.V, BlkNr, StartNr, pAmount, BufLen, pReadBuffer,
// ref pDatLen);
RetValue = Prodave6.db_read_ex6(BlkNr, DType, StartNr, ref pAmount, BufLen, pReadBuffer, ref pDatLen);
//pBuffer = new byte[300];
//RetValue = Prodave6.GetErrorMessage_ex6(RetValue, 300, pBuffer);
//s = Encoding.ASCII.GetString(pBuffer);
//以下测试field_read_ex6测试DB区
//参数data block号、要读取的数据类型、起始地址号、需要读取类型的数量、缓冲区长度字节为单位
//缓冲区、缓冲区数据交互的长度
Prodave6.FieldType FType = Prodave6.FieldType.E;
for (int i = 0; i < pWriteBuffer.Length; i++)
pWriteBuffer[i] = (byte)(1);
RetValue = Prodave6.field_read_ex6(FType, BlkNr, StartNr, pAmount, BufLen, pReadBuffer, ref pDatLen);
RetValue = Prodave6.field_write_ex6(FType, BlkNr, StartNr, pAmount, BufLen, pWriteBuffer);
RetValue = Prodave6.field_read_ex6(FType, BlkNr, StartNr, pAmount, BufLen, pReadBuffer, ref pDatLen);*/
}
}
}