Page 1 of 1

write float dro to modbus vars(2x16bit)??

PostPosted: Mon Mar 02, 2020 2:49 pm
by cahit
hi i made a macro to write the dro's to 2 modbus vars. i wonder if there is a shorter and a better and faster way to do it. this is my macro is:

byte[] yarray = BitConverter.GetBytes((float)exec.GetYpos());
byte[] zarray = BitConverter.GetBytes((float)exec.GetZpos());
byte[] aarray = BitConverter.GetBytes((float)exec.GetApos());
byte[] barray = BitConverter.GetBytes((float)exec.GetBpos());
byte[] xmarray = BitConverter.GetBytes((float)exec.GetXmachpos());
byte[] ymarray = BitConverter.GetBytes((float)exec.GetYmachpos());
byte[] zmarray = BitConverter.GetBytes((float)exec.GetZmachpos());
byte[] amarray = BitConverter.GetBytes((float)exec.GetAmachpos());
byte[] bmarray = BitConverter.GetBytes((float)exec.GetBmachpos());


ushort Writevalue0 = Convert.ToUInt16((xarray[1] * 256)+xarray[0]); exec.SetModbusregister(0, Writevalue0);
ushort Writevalue1 = Convert.ToUInt16((xarray[3] * 256)+xarray[2]); exec.SetModbusregister(1, Writevalue1);
ushort Writevalue2 = Convert.ToUInt16((yarray[1] * 256)+yarray[0]); exec.SetModbusregister(2, Writevalue2);
ushort Writevalue3 = Convert.ToUInt16((yarray[3] * 256)+yarray[2]); exec.SetModbusregister(3, Writevalue3);
ushort Writevalue4 = Convert.ToUInt16((zarray[1] * 256)+zarray[0]); exec.SetModbusregister(4, Writevalue4);
ushort Writevalue5 = Convert.ToUInt16((zarray[3] * 256)+zarray[2]); exec.SetModbusregister(5, Writevalue5);
ushort Writevalue6 = Convert.ToUInt16((aarray[1] * 256)+aarray[0]); exec.SetModbusregister(6, Writevalue6);
ushort Writevalue7 = Convert.ToUInt16((aarray[3] * 256)+aarray[2]); exec.SetModbusregister(7, Writevalue7);
ushort Writevalue8 = Convert.ToUInt16((barray[1] * 256)+barray[0]); exec.SetModbusregister(8, Writevalue8);
ushort Writevalue9 = Convert.ToUInt16((barray[3] * 256)+barray[2]); exec.SetModbusregister(9, Writevalue9);
ushort Writevalue10 = Convert.ToUInt16((xmarray[1] * 256)+xmarray[0]); exec.SetModbusregister(10, Writevalue10);
ushort Writevalue11 = Convert.ToUInt16((xmarray[3] * 256)+xmarray[2]); exec.SetModbusregister(11, Writevalue11);
ushort Writevalue12 = Convert.ToUInt16((ymarray[1] * 256)+ymarray[0]); exec.SetModbusregister(12, Writevalue12);
ushort Writevalue13 = Convert.ToUInt16((ymarray[3] * 256)+ymarray[2]); exec.SetModbusregister(13, Writevalue13);
ushort Writevalue14 = Convert.ToUInt16((zmarray[1] * 256)+zmarray[0]); exec.SetModbusregister(14, Writevalue14);
ushort Writevalue15 = Convert.ToUInt16((zmarray[3] * 256)+zmarray[2]); exec.SetModbusregister(15, Writevalue15);
ushort Writevalue16 = Convert.ToUInt16((amarray[1] * 256)+amarray[0]); exec.SetModbusregister(16, Writevalue16);
ushort Writevalue17 = Convert.ToUInt16((amarray[3] * 256)+amarray[2]); exec.SetModbusregister(17, Writevalue17);
ushort Writevalue18 = Convert.ToUInt16((bmarray[1] * 256)+bmarray[0]); exec.SetModbusregister(18, Writevalue18);
ushort Writevalue19 = Convert.ToUInt16((bmarray[3] * 256)+bmarray[2]); exec.SetModbusregister(19, Writevalue19);


thanks

Cahit