A&B Micro820 ModbusRTU value to String

MauritsKoppe

Member
Join Date
Feb 2023
Location
netherlands
Posts
12
Hello,


I am using an A&B Micro820 togehter with ccw. Reading a device via Modbus RTU. This goes all OK.


Now I am reading a Modbus register which represent two ASCII characters.
The word value that I am reading is 12857, this is Hex 3239. This is correct.



Now I want to convert this number to his ascii value and store this in a string. So the string will be equal to 29.
Here I am struggling.


Using the COP instruction doesn't work or I am doing something wrong.

Using the ASCII instruction only allows one char (8 bits).


Does anyone know a solution?

Thank you.
 
Not tested, but something like this might work:
Start with creating an empty result string

AND_MASK your 3239 tag with FF (hex) or 256 (decimal) to get 39 by itself.
CHAR that result to a temporary string
INSERT the temporary string to position 1 of the result string

AND_MASK your 3239 tag with FF00 (hex) or 65280 (decimal) then divide that by 256 to get 32 by itself
CHAR that to the temporary string and INSERT to position 1 of the result string

There's probably a cleaner way to do it, especially of you will be working with longer strings than two characters. Then I would be looking at a ST loop.
 
Not tested, but something like this might work:
Start with creating an empty result string

AND_MASK your 3239 tag with FF (hex) or 256 (decimal) to get 39 by itself.
CHAR that result to a temporary string
INSERT the temporary string to position 1 of the result string

AND_MASK your 3239 tag with FF00 (hex) or 65280 (decimal) then divide that by 256 to get 32 by itself
CHAR that to the temporary string and INSERT to position 1 of the result string

There's probably a cleaner way to do it, especially of you will be working with longer strings than two characters. Then I would be looking at a ST loop.


+1. drBITboy approves ;).


TL;DR


Since this is presumably ASCII (i.e. 7-bt) data, bit 15 will never be 1, otherwise there would need to be another AND with 0x00FF to clear the possibility of sign extension from the DIV by 256, that is if the 3239 is in an INT and not in a UINT. You could also use the SHR (SHift Right) instruction as an alternative to the DIV by 256, but that involves casting the 3239 into a 32-bit integer and possibly ANDing with 0x0000FFFF.

So in the end, @5618's approach is like the best.
 

Similar Topics

I’m trying to read values from a device that can either send registers as 32 bit or a pair of 16 bit but if I understand right, the micro can only...
Replies
26
Views
880
Hello, I set up my Micro820 with a basic mapping. When I try to run a script to read the addresses I get errors for illegal addresses...
Replies
2
Views
184
Hi Guys, I am a new member and this is my first post! I have a little PLC experience but it is mostly with siemens logo and using ladder...
Replies
4
Views
929
Hello, I am using a AB Micro820 PLC, via the SD card I want to update the PLC program (not FW). First I created a text file on the SD card with...
Replies
2
Views
312
Hi New to PLCs, up until now I have played with smart relays and the siemens S7-1200 line with TIA. I am trying to get a AB Micro820 to talk to...
Replies
0
Views
314
Back
Top Bottom