Ewon TechForum
Providing technical solutions to technical requests


Modbus request via basic IDE
julpap Offline
Junior Member
#1
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?
Reply

simon Offline
eWON Support
#2
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)
Reply

julpap Offline
Junior Member
#3
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


Attached Files
.txt   program.txt (Size: 1,04 KB / Downloads: 39)
Reply

simon Offline
eWON Support
#4
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)


Attached Files
.png   IoSrvSettings.png (Size: 10,45 KB / Downloads: 22)
.png   PortSettings.png (Size: 11,33 KB / Downloads: 18)
.png   ScriptRunning.png (Size: 23,96 KB / Downloads: 25)
.png   ModbusSlaveSimul.png (Size: 2,59 KB / Downloads: 23)
Reply

julpap Offline
Junior Member
#5
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?
Reply

simon Offline
eWON Support
#6
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
Reply

julpap Offline
Junior Member
#7
(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!
Reply

simon Offline
eWON Support
#8
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%
Reply

julpap Offline
Junior Member
#9
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
Reply

simon Offline
eWON Support
#10
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 ;-)
Reply

julpap Offline
Junior Member
#11
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
Reply

simon Offline
eWON Support
#12
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") ?
Reply

julpap Offline
Junior Member
#13
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
Reply

julpap Offline
Junior Member
#14
(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
Reply

simon Offline
eWON Support
#15
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.
Reply



Possibly Related Threads…
12-11-2019, 02:52 PM
Last Post: AlexV
  Double Float via Modbus Started by alex barnaba
2 Replies - 2.856 Views
11-10-2019, 09:14 AM
Last Post: Diksha.Saxena
04-10-2019, 09:26 AM
Last Post: djonedd
  Modbus ASCII IOServer for eWON Started by simon
8 Replies - 6.083 Views
22-07-2019, 01:33 PM
Last Post: simon
15-04-2019, 12:00 PM
Last Post: Energenius
18-02-2019, 11:32 PM
Last Post: simon
04-02-2019, 11:31 PM
Last Post: simon
07-11-2018, 01:31 PM
Last Post: simon
  Basic script Ewon Flexy 102 Started by yroue
3 Replies - 2.297 Views
04-09-2018, 01:07 PM
Last Post: simon
  Modbus Server Max Tags Started by Quiles85
2 Replies - 1.865 Views
04-04-2018, 04:19 PM
Last Post: Quiles85



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy