Switch to desktop version  
Modbus request via basic IDE - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: Ewon Embedded Technology (https://techforum.ewon.biz/forum-51.html)
+---- Forum: Field Data Acquisition (https://techforum.ewon.biz/forum-2.html)
+---- Thread: Modbus request via basic IDE (/thread-287.html)

Pages: 1 2


Modbus request via basic IDE - julpap - 24-04-2017

I would like to pick some value from a modbus but I don't want to create a tag for each value but just to write on a txt file the values that I need. 

I tried this code which comes from the programming guide. 

Code:
TestIO:
A$ = chr$(4)+chr$(0)+chr$(0)+chr$(0)+chr$(1) : rem create modbus command
rem initiate the modbus request on slave 21
a% = IOSEND "MODBUS","10",A$
Wait_IO_End:
b% = IORCV a%,1 : rem read the status
IF b%=-1 THEN
GOTO Wait_IO_End : rem if idle then loop
ENDIF
B$ = IORCV a% : rem read the result and free the slot
PRINT LEN(B$)
PRINT B$
END


But something looks wrong with the IORCV command.

The error is :  invalid parameter (5) : B$ = IORCV a% : rem read the result and free the slot

Please could you help me on this?


RE: Modbus request via basic IDE - simon - 24-04-2017

I have just tried that and it is working good.

Code:
TestIO:
debug=1
A$ = Chr$(4)+Chr$(0)+Chr$(0)+Chr$(0)+Chr$(1) : Rem create modbus command
Rem initiate the modbus request on slave 21
a% = IOSEND "MODBUS","1,192.168.120.98",A$
Wait_IO_End:
b% = IORCV a%,1 : Rem read the status
IF b%=-1 THEN
GOTO Wait_IO_End : Rem if idle then loop
ENDIF
B$ = IORCV a% : Rem read the result and free the slot

IF (debug) THEN
 Print Len(B$);" bytes received (hex)"
 FOR i%=1 to LEN(B$)
   j%=ASCII(B$(i%))
   X$ = SFMT j%,30,0,"%X"
   print X$;" ";
 NEXT i%
 PRINT ""
ENDIF

END

Can you send the whole "program.bas" file ?  (From the IDE, Click File -> Export)


RE: Modbus request via basic IDE - julpap - 24-04-2017

Your code is not working as well. 

I am using a serial modbus connection RS485 define in IOconfig (it works because I am using some data to set some tags)

Find herewith my small program.

Thanks a lot for your support


RE: Modbus request via basic IDE - simon - 25-04-2017

I just tried the same code (only this line has changed =>   a% = IOSEND "MODBUS","1",A$) over RS485 and it works on my side.


Code:
TestIO:
debug=1
A$ = Chr$(4)+Chr$(0)+Chr$(0)+Chr$(0)+Chr$(1) : Rem create modbus command
Rem initiate the modbus request on slave 21
a% = IOSEND "MODBUS","1",A$
Wait_IO_End:
b% = IORCV a%,1 : Rem read the status
IF b%=-1 THEN
GOTO Wait_IO_End : Rem if idle then loop
ENDIF
B$ = IORCV a% : Rem read the result and free the slot

IF (debug) THEN
Print Len(B$);" bytes received (hex)"
FOR i%=1 To LEN(B$)
  j%=ASCII(B$(i%))
  X$ = SFMT j%,30,0,"%X"
  Print X$;" ";
NEXT i%
PRINT ""
ENDIF

END


See some snapshot of the config.
I used a RS485/USB converter to connect my PC on which I ran a Modbus Slave Simulator.
I am running the firmware 11.2s2 (good idea to run the same firmware)


RE: Modbus request via basic IDE - julpap - 26-04-2017

Thanks a lot it's working I don't know what was my error. 

the value is an integer. How can I translate it?

What is the purpose of the end of your code with the For loop? I think it will answer to my first question. What is SFMT?


RE: Modbus request via basic IDE - simon - 26-04-2017

The last loop is for displaying the result into an Hexadecimal string.
To convert an integer to a string, use STR$ or SFMT
To convert a string to an integer/float, use VAL

You will find more details in the Programming Reference Guide : https://developer.ewon.biz/content/basic-1


RE: Modbus request via basic IDE - julpap - 28-04-2017

(26-04-2017, 12:50 PM)Simon Wrote: The last loop is for displaying the result into an Hexadecimal string.
To convert an integer to a string, use STR$ or SFMT
To convert a string to an integer/float, use VAL

You will find more details in the Programming Reference Guide : https://developer.ewon.biz/content/basic-1

Thanks, I am trying to collect the value (what is the type of value that I recieve? it is bin, hexa, string,...)

Can you help me to convert the value into an integer (signed)

thanks a lot!


RE: Modbus request via basic IDE - simon - 28-04-2017

From the IORCV function (B$ = IORCV a%) you mean ?
If yes, this function returns a string (Here B$).
To get each byte of this string, you extract each char of the string [b$(index)] and then convert the char into its ASCII code.

FOR i%=1 To LEN(B$)
j%=ASCII(B$(i%))
NEXT i%


RE: Modbus request via basic IDE - julpap - 04-05-2017

I still have a problem,
I am able to read a modbus value in define by a tag with the adresse 40420I (holding register) (integer signed) and I define a topic with the slave 10
but I have an error when I do it by this way

A$ = Chr$(4)+Chr$(0)+Chr$(4)+Chr$(2)+Chr$(0)
a% = IOSEND "MODBUS","10",A$ // it is the slave number 10
Wait_IO_End:
b% = IORCV a%,1 : Rem read the status
IF b%=-1 THEN
GOTO Wait_IO_End : Rem if idle then loop
ENDIF
B$ = IORCV a% : Rem read the result and free the slot
Print B$

The result of this last print is #ERR

Do you have a clue to resolve that?

thanks


RE: Modbus request via basic IDE - simon - 09-05-2017

You misunderstood the command (A$) which is not the Tag Address but a real Modbus frame like described here for example : http://www.modbustools.com/modbus.html

So to read the register 40420, the frame in Hexa must be "0A 04 01 A4 00 01 + CRC" where 0Ah = 10d = the address, 04h is the function code, 01 A4 = 420d is the Tag address and 00 01 the quantity to read = 1x16 bits register.

So A$ (address and CRC must be excluded) must be equal to Chr$(4)+Chr$(1)+Chr$(164)+Chr$(0)+Chr$(1)

Hope it is more clear ;-)


RE: Modbus request via basic IDE - julpap - 18-05-2017

thank you for your help!

I put this code

Code:
TestIO:
debug=1
A$ = Chr$(4)+Chr$(1)+Chr$(211)+Chr$(0)+Chr$(1) : Rem create modbus command
Rem initiate the modbus request on slave 21
a% = IOSEND "MODBUS","1",A$
Wait_IO_End:
b% = IORCV a%,1 : Rem read the status
IF b%=-1 THEN
GOTO Wait_IO_End : Rem if idle then loop
ENDIF
B$ = IORCV a% : Rem read the result and free the slot
IF (debug) THEN
Print Len(B$);" bytes received (hex)"
FOR i%=1 To LEN(B$)
 j%=ASCII(B$(i%))
 X$ = SFMT j%,30,0,"%X"
 Print X$;" ";
NEXT i%
PRINT ""
ENDIF

END

I wanted to have the value of the address 467 In Tags I put 40467I and it works. But still an error(#ERR) with the code.

Sorry for this long and laborious topic!

regards,
Julien


RE: Modbus request via basic IDE - simon - 23-05-2017

I tried on my eWON and it is running fine.
Can you give me your backup (Do it with ebuddy and check "Include support files") ?


RE: Modbus request via basic IDE - julpap - 24-05-2017

Thanks for the support,

The solution is to write the request like that

A$ = Chr$(3)+Chr$(1)+Chr$(187)+Chr$(0)+Chr$(1)

with in this case I read the register 444.

Thanks again


RE: Modbus request via basic IDE - julpap - 22-06-2017

(24-04-2017, 03:45 PM)Simon Wrote: I have just tried that and it is working good.

Code:
TestIO:
debug=1
A$ = Chr$(4)+Chr$(0)+Chr$(0)+Chr$(0)+Chr$(1) : Rem create modbus command
Rem initiate the modbus request on slave 21
a% = IOSEND "MODBUS","1,192.168.120.98",A$
Wait_IO_End:
b% = IORCV a%,1 : Rem read the status
IF b%=-1 THEN
GOTO Wait_IO_End : Rem if idle then loop
ENDIF
B$ = IORCV a% : Rem read the result and free the slot

IF (debug) THEN
 Print Len(B$);" bytes received (hex)"
 FOR i%=1 to LEN(B$)
   j%=ASCII(B$(i%))
   X$ = SFMT j%,30,0,"%X"
   print X$;" ";
 NEXT i%
 PRINT ""
ENDIF

END

Can you send the whole "program.bas" file ?  (From the IDE, Click File -> Export)
Hi Simon,

I am trying to make a modbus request by tcp IP but when I put your code I have an error (invalid parameter (5) 30: b%=IORCV a%,1)

This is my code. I try to read the register 2133 of the slave 1 of the Ip 192.168.1.1


Code:
TestIO:
debug=1
A$ = Chr$(3)+Chr$(8)+Chr$(85)+Chr$(0)+Chr$(1) : Rem create modbus command
Rem initiate the modbus request on slave 21
a% = IOSEND "MODBUS","1,192.168.1.1",A$
Wait_IO_End:
b% = IORCV a%,1 : Rem read the status
IF b%=-1 THEN
GOTO Wait_IO_End : Rem if idle then loop
ENDIF
B$ = IORCV a% : Rem read the result and free the slot

IF (debug) THEN
 Print Len(B$);" bytes received (hex)"
 FOR i%=1 to LEN(B$)
   j%=ASCII(B$(i%))
   X$ = SFMT j%,30,0,"%X"
   print X$;" ";
 NEXT i%
 PRINT ""
ENDIF

END
 

Do you know what is wrong?

Thanks,

Julien


RE: Modbus request via basic IDE - simon - 22-06-2017

Julien,

I tried you code (just copy/paste and change the IP address) and it is working fine in my Flexy.

Are you sure the IP address is correct ? Is it well in the same range as your LAN IP ?
There has been perhaps a problem while copy/pasting the code ? Try to clear it and take it from here like me.