Author Topic: How to Write Multiple Register Inputs  (Read 3558 times)

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
How to Write Multiple Register Inputs
« on: March 16, 2015, 11:37:08 AM »
Hi,

I am working on to control a device over modbus.  Reading is ok but i can not write multiple register inputs.

Device sends hex data as  short[]  array.   While writing, i did the same but i got exception  overflow.. 

I attached the spec page of the fuction.

Values should be :

Vega serial address  : 1
Sub Function code : 2 (0x02)
Meter addressed :  1
Product addressed : 1  ( 0x31)
Sub function code :  2 ( 0x02)
Preset :  95.5  ( any double value)
Max Weight :  100 ( any double value)
Reserved : 0 (0x00)

How can i populate this message?   I am trying for days. Already read all forums, blogs, everything..

Could anyone please help??



 
« Last Edit: March 16, 2015, 11:56:19 AM by mesutonus »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to Write Multiple Register Inputs
« Reply #1 on: March 16, 2015, 11:47:06 AM »
Function code 2 is a read function. It is not possible to write. Below is a list of typical Modbus function codes, if you notice there is no write or force function for "Inputs"

01 Read Coil Status
02 Read Input Status
03 Read Holding Registers
04 Read Input Registers
05 Force Single Coil
06 Preset Single Register
07 Read Exception Status
15 (0F Hex) Force Multiple Coils
16 (10 Hex) Preset Multiple Regs

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to Write Multiple Register Inputs
« Reply #2 on: March 16, 2015, 11:56:05 AM »
Sorry, written by mistake.

Function Code  : 16
Sub Function  Code : 2



modbusRTUCom1.Write("40001", datapack);  is what i do..
« Last Edit: March 16, 2015, 11:59:59 AM by mesutonus »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to Write Multiple Register Inputs
« Reply #3 on: March 16, 2015, 12:10:01 PM »
Is your datapack and array of string values?

Dim datapack(2) as string
datapack(0)=10
datapack(1)=20
datapack(0)=30
« Last Edit: March 16, 2015, 12:20:57 PM by Archie »

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to Write Multiple Register Inputs
« Reply #4 on: March 16, 2015, 12:15:32 PM »
When i read from device it returns  16 bit int array. I convert it to 32 bit, then to hex. After combining hex parts together i convert it to ulong number, then byte and then to double.   
So i think it expects   hex values in short format when i send.

I tried

data[0] = "hex val"
data[0]  = "Int16 val"
data[0] = "UInt16 val"
data[0] = "byte val"
...
..
all of possible combinations i know..

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to Write Multiple Register Inputs
« Reply #5 on: March 16, 2015, 12:21:35 PM »
Your string values have to be strictly numbers:

data[0]="123"

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to Write Multiple Register Inputs
« Reply #6 on: March 16, 2015, 12:25:37 PM »
Ok but in which format?  Like

double doubleval =  75.5;

data =    Convert.ToUInt16(doubleval).toString()  ??

or its bytes? What about crc values? Will they be calculated by short values and then converted to string before sending or calculated on string vals?

May i ask if you can give a example for me for attached mapping?


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to Write Multiple Register Inputs
« Reply #7 on: March 16, 2015, 12:28:29 PM »
Dim data(3) as string
data(0)="111"
data(1)="222"
data(2)="333"
modbusRTUCom1.Write("40001",data)

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to Write Multiple Register Inputs
« Reply #8 on: March 16, 2015, 12:35:55 PM »
Does component convert "111" to ushort when sending?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to Write Multiple Register Inputs
« Reply #9 on: March 16, 2015, 12:38:31 PM »
Does component convert "111" to ushort when sending?
Yes, it converts it to a Uint16

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to Write Multiple Register Inputs
« Reply #10 on: March 16, 2015, 12:41:12 PM »
But device returns signed int16 while reading registers?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to Write Multiple Register Inputs
« Reply #11 on: March 16, 2015, 01:12:06 PM »
I'm sorry... I meant that it does convert to an Int16

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to Write Multiple Register Inputs
« Reply #12 on: March 16, 2015, 01:14:14 PM »
Spec says ;

Coding System :  8 Binary bit hexadecimal 0...9,A....F.    Two hexadecimal char in each of 8 bit field of message.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to Write Multiple Register Inputs
« Reply #13 on: March 16, 2015, 01:17:52 PM »
What spec?

mesutonus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to Write Multiple Register Inputs
« Reply #14 on: March 16, 2015, 01:18:58 PM »
Spec document of the device.