2014-10-08 update 2
This commit is contained in:
@@ -42,7 +42,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
public Dictionary<string, short> TransDictionary;
|
public Dictionary<string, short> TransDictionary;
|
||||||
|
|
||||||
private AddressTranslatorNA200H()
|
public AddressTranslatorNA200H()
|
||||||
{
|
{
|
||||||
TransDictionary = new Dictionary<string, short>();
|
TransDictionary = new Dictionary<string, short>();
|
||||||
TransDictionary.Add("Q", 0);
|
TransDictionary.Add("Q", 0);
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ namespace ModBus.Net
|
|||||||
byte temp = data[pos];
|
byte temp = data[pos];
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
{
|
{
|
||||||
t[i] = temp%2 > 0;
|
t[7 - i] = temp%2 > 0;
|
||||||
temp /= 2;
|
temp /= 2;
|
||||||
}
|
}
|
||||||
pos += 1;
|
pos += 1;
|
||||||
@@ -386,11 +386,11 @@ namespace ModBus.Net
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool GetBit(ushort number, ref int pos)
|
public bool GetBit(byte number, ref int pos)
|
||||||
{
|
{
|
||||||
if (pos < 0 && pos > 15) throw new IndexOutOfRangeException();
|
if (pos < 0 && pos > 8) throw new IndexOutOfRangeException();
|
||||||
int ans = number % 2;
|
int ans = number % 2;
|
||||||
int i = 15;
|
int i = 7;
|
||||||
while (i >= pos)
|
while (i >= pos)
|
||||||
{
|
{
|
||||||
ans = number%2;
|
ans = number%2;
|
||||||
@@ -537,7 +537,7 @@ namespace ModBus.Net
|
|||||||
byte temp = data[pos];
|
byte temp = data[pos];
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
{
|
{
|
||||||
t[8 - i] = temp%2 > 0;
|
t[i] = temp%2 > 0;
|
||||||
temp /= 2;
|
temp /= 2;
|
||||||
}
|
}
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|||||||
@@ -12,14 +12,16 @@ namespace NA200H.UI.ConsoleApp
|
|||||||
{
|
{
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
string ip = "192.168.3.247";
|
||||||
|
//先初始化一个协议转换器,这里构造Modbus/Tcp协议。
|
||||||
|
BaseProtocal wrapper = new ModbusTcpProtocal(ip);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
string ip = "192.168.4.247";
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
BaseProtocal wrapper = new ModbusTcpProtocal(ip);
|
|
||||||
Console.WriteLine("link ip is {0}",ip);
|
Console.WriteLine("link ip is {0}",ip);
|
||||||
//第一步:先生成一个输入信息的object数组
|
//第一步:先生成一个输入信息的object数组
|
||||||
object[] inputObjects = new object[] {(byte) 0x02, (byte) 0x01, (short) 0x13, (short) 0x25};
|
object[] inputObjects = new object[] {(byte) 0x02, (byte) 0x01, (short) 0x4e20, (short) 0x0004};
|
||||||
//第二步:向仪器发送这个信息,并接收信息
|
//第二步:向仪器发送这个信息,并接收信息
|
||||||
byte[] outputBytes = wrapper.SendReceive(inputObjects);
|
byte[] outputBytes = wrapper.SendReceive(inputObjects);
|
||||||
//第三步:输出信息
|
//第三步:输出信息
|
||||||
@@ -36,64 +38,10 @@ namespace NA200H.UI.ConsoleApp
|
|||||||
Console.Read();
|
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();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
//调用方法一:手动构造
|
//调用方法一:手动构造
|
||||||
//第一步:先生成一个输入信息的object数组
|
//第一步:先生成一个输入信息的object数组
|
||||||
object[] inputObjects = new object[]{(byte)0x02,(byte)0x01,(short)0x13,(short)0x25};
|
object[] inputObjects = new object[]{(byte)0x02,(byte)0x01,(short)0x4e20,(short)0x0004};
|
||||||
//第二步:向仪器发送这个信息,并接收信息
|
|
||||||
byte[] outputBytes = wrapper.SendReceive(inputObjects);
|
|
||||||
//第三步:输出信息
|
|
||||||
for (int i = 0; i < outputBytes.Length; i++)
|
|
||||||
{
|
|
||||||
Console.WriteLine(outputBytes[i]);
|
|
||||||
}
|
|
||||||
Console.WriteLine();
|
|
||||||
Console.Read();
|
|
||||||
Console.Read();
|
|
||||||
*/
|
|
||||||
|
|
||||||
//先初始化一个协议转换器,这里构造Modbus/Rtu协议。
|
|
||||||
BaseProtocal wrapper = new ModbusRtuProtocal();
|
|
||||||
|
|
||||||
/*
|
|
||||||
//调用方法一:手动构造
|
|
||||||
//第一步:先生成一个输入信息的object数组
|
|
||||||
object[] inputObjects = new object[]{(byte)0x02,(byte)0x01,(short)0x00,(short)0x03};
|
|
||||||
//第二步:向仪器发送这个信息,并接收信息
|
//第二步:向仪器发送这个信息,并接收信息
|
||||||
byte[] outputBytes = wrapper.SendReceive(inputObjects);
|
byte[] outputBytes = wrapper.SendReceive(inputObjects);
|
||||||
//第三步:输出信息
|
//第三步:输出信息
|
||||||
@@ -105,22 +53,28 @@ namespace NA200H.UI.ConsoleApp
|
|||||||
Console.Read();
|
Console.Read();
|
||||||
Console.Read();*/
|
Console.Read();*/
|
||||||
|
|
||||||
/*
|
|
||||||
//调用方法二:自动构造
|
//调用方法二:自动构造
|
||||||
//第一步:先生成一个输入结构体,然后向这个结构体中填写数据
|
//第一步:先生成一个输入结构体,然后向这个结构体中填写数据
|
||||||
ReadCoilStatusModbusProtocal.ReadCoilStatusInputStruct readCoilStatusInputStruct = new ReadCoilStatusModbusProtocal.ReadCoilStatusInputStruct(0x02, "Q20", 0x25);
|
AddressTranslator.CreateTranslator(new AddressTranslatorNA200H());
|
||||||
|
|
||||||
|
ReadDataInputStruct readCoilStatusInputStruct = new ReadDataInputStruct(0x02, ModbusProtocalReadDataFunctionCode.ReadCoilStatus, "N1", 0x0a);
|
||||||
//第二步:再生成一个输出结构体,执行相应协议的发送指令,并将输出信息自动转换到输出结构体中
|
//第二步:再生成一个输出结构体,执行相应协议的发送指令,并将输出信息自动转换到输出结构体中
|
||||||
ReadCoilStatusModbusProtocal.ReadCoilStatusOutputStruct readCoilStatusOutputStruct = (ReadCoilStatusModbusProtocal.ReadCoilStatusOutputStruct)wrapper.SendReceive(wrapper["ReadCoilStatusModbusProtocal"], readCoilStatusInputStruct);
|
ReadDataOutputStruct readCoilStatusOutputStruct = (ReadDataOutputStruct)wrapper.SendReceive(wrapper["ReadDataModbusProtocal"], readCoilStatusInputStruct);
|
||||||
//第三步:读取这个输出结构体的信息。
|
//第三步:读取这个输出结构体的信息。
|
||||||
for (int i = 0; i < readCoilStatusOutputStruct.CoilStatus.Length; i++)
|
bool[] array =
|
||||||
|
ValueHelper.Instance.ObjectArrayToDestinationArray<bool>(
|
||||||
|
ValueHelper.Instance.ByteArrayToObjectArray(readCoilStatusOutputStruct.DataValue,
|
||||||
|
new List<KeyValuePair<Type, int>>() {new KeyValuePair<Type, int>(typeof (bool), 0x0a)}));
|
||||||
|
for (int i = 0; i < array.Length; i++)
|
||||||
{
|
{
|
||||||
Console.WriteLine(readCoilStatusOutputStruct.CoilStatus[i]);
|
Console.WriteLine(array[i]);
|
||||||
}
|
}
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.Read();
|
Console.Read();
|
||||||
Console.Read();
|
Console.Read();
|
||||||
|
|
||||||
ReadInputStatusModbusProtocal.ReadInputStatusInputStruct readInputStatusInputStruct = new ReadInputStatusModbusProtocal.ReadInputStatusInputStruct(0x02, "I20", 0x25);
|
/*ReadInputStatusModbusProtocal.ReadInputStatusInputStruct readInputStatusInputStruct = new ReadInputStatusModbusProtocal.ReadInputStatusInputStruct(0x02, "I20", 0x25);
|
||||||
ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct readInputStatusOutputStruct = (ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct)wrapper.SendReceive(wrapper["ReadInputStatusModbusProtocal"], readInputStatusInputStruct);
|
ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct readInputStatusOutputStruct = (ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct)wrapper.SendReceive(wrapper["ReadInputStatusModbusProtocal"], readInputStatusInputStruct);
|
||||||
for (int i = 0; i < readInputStatusOutputStruct.InputStatus.Length; i++)
|
for (int i = 0; i < readInputStatusOutputStruct.InputStatus.Length; i++)
|
||||||
{
|
{
|
||||||
@@ -201,60 +155,8 @@ namespace NA200H.UI.ConsoleApp
|
|||||||
Console.WriteLine(setSystemTimeOutputStruct.StartAddress);
|
Console.WriteLine(setSystemTimeOutputStruct.StartAddress);
|
||||||
Console.WriteLine(setSystemTimeOutputStruct.WriteCount);
|
Console.WriteLine(setSystemTimeOutputStruct.WriteCount);
|
||||||
Console.Read();
|
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);*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user