From word to 2 bytes (codesys)

zylar

Member
Join Date
Jan 2012
Location
hell
Posts
3
hello guys, i am looking for a way to split up a word (analog input)into 2 bytes to send over CAN. anyone have an idea?

thanks in advance
 
Ok never mind, i solved it, for anyone who is interested: word -> word_to_byte -> highbyte

word -> [shr] 8 bits -> lowbyte

decode: lowbyte -> [mul] 256 -> [add] highbyte -> word again
 
I am sorry we could not help you in time.
I don't program Codesys - obvioulsy some of the guys on this forum had not seen your post it time

Please don't let that sway you away - you may be able to help people later.
welcome to the forum anyway
 
There is another way I have done this using marker space.

You define the Word and the two bytes in overlapping memory locations. This is like a union in C if you know what that is. For instance:

Code:
HI_BYTE AT %MB0: BYTE;
LO_BYTE AT %MB1: BYTE;


MY_WORD AT %MW0: WORD;

Now when you put your word into the variable MY_WORD it is instantly and automatically available as BYTE values at HI_BYTE and LO_BYTE. And vice versa.

No code required.

Note, I may have the lo and hi bytes in the wrong order above. I'm not anywhere that I can test this right now.
 
Last edited:
There is another way I have done this using marker space.

Code:
HI_BYTE AT %MB0: BYTE;
LO_BYTE AT %MB1: BYTE;


No code required.
[/quote]

You should note that if you use global store in this way, you can get trouble in acyclic programs; the M-contents are overwritten from the interrupt-call.  It is so with Simatic, probalby also with CoDeSys.

With a grain of salt
Kalle
 
zylar do you know the site of 3s-software.com
and yes kalle is correct it works too
however your solution is better to understand and can be used everywhere.
(at www.oscat.de you will find a nice collection for communication)
 
Ok never mind, i solved it, for anyone who is interested: word -> word_to_byte -> highbyte

word -> [shr] 8 bits -> lowbyte

decode: lowbyte -> [mul] 256 -> [add] highbyte -> word again

Firstly, really sorry to revive an old thread, but how would you go about preserving decimal places using this method?

Thanks
 
There are no decimals in a word, only in real. But if you want to send 5,78 for example you can multiply by 100 so you get 578, send that in an word and divide it on the other end
 
There are no decimals in a word, only in real. But if you want to send 5,78 for example you can multiply by 100 so you get 578, send that in an word and divide it on the other end

I thought that would be the case, the only downside I suppose is that you restrict yourself to a maximum of 655.35 else your High-Byte value will top 256 and you'll get an incorrect value

How would you approach getting a figure higher than that? Are we getting in to the realms of dividing it by two and storing it in two separate locations and adding them together after the COMS section? I cant believe its so difficult to send large numbers between a PLC and HMI in CoDeSys!
 
It also depends on what your processor can handle.
Consider fractions if needed (5/8).
 
65535 is the maximum i assume you are using analogue.
if you reach the max it will fail.
good practice is to keep well clear of both 0 and 65535 with integers
 

Similar Topics

Hello, I am new to Codesys 3.5 and I am trying to figure out how to convert a word into 2 bytes to send to another plc via ethernet/ip. Could...
Replies
5
Views
5,102
Hi, I try to combine 2 Word to a Double Word, but I don't know how to make that in ST. I'm a guy from C/ASM,so in C, it's would look like this...
Replies
6
Views
9,993
I am working on an RFID project that will write Micrologic integer value to an RFID tag. The RFID system is the Turck BLCDN RFID SLICE. It...
Replies
3
Views
4,134
Hi All, Just a question that is PLC and SCADA related. I am using Wonderwares Intouch 9.5 and a Siemens S7-300 PLC, i have a word that stores...
Replies
4
Views
3,018
Maybe a stupid question: Is there a simple way (instruction) to swap the bytes in a word. (S7-300) So more clever then: L MB 0 T DB1.DBB0 L...
Replies
2
Views
6,263
Back
Top Bottom