Flexy to Omron UDINT_BCD Data type
I found another post on how to read the data type of UDINT_BCD from an Omron controller, using a script, no problem.
however I need to work in reverse, write from a flexy to an Omron PLC With 32 bit BCD (UDINT_BCD).
I wanted to see if anyone had any guidance or experience on this
-
Hi DavidGSydney! I checked out the Script example on how to Convert from BCD into a normal INT i think:
OMRON UINT_BCD tag value incorrect - Ewon Support / Ewon Flexy - hms.how
If you want to write a Value to your OMRON you would need to reverse that.
From a Digital Value like 127 (BINARY): 0111 1111 / 0x7F (hex)/ 127 (Dec)
127 in BCD is: 0001 0010 0111 (1=0001 2= 0010 7=0111). (if interpreted as INT would be 295)
I have created an example Script you can try.
It is not tested though with Omron... just the conversion reversed:
Rem --- eWON start section: Init Section
eWON_init_section:
Rem --- eWON user (start)
TSET 1, 2 // Set a 2-second timer
ONTIMER 1, "goto ConvertToBCD" // Every 2 seconds, go to ConvertToBCD section
Rem --- eWON user (end)
End
Rem --- eWON end section: Init SectionRem --- eWON end section: Init SectionRem --- eWON start section: ConvertToBCDSection
Rem --- eWON user (start)
Rem This section performs the decimal-to-BCD: Binary Coded Decimal conversion
ConvertToBCD:
Rem Let's assume Tag1@ contains a regular decimal value lets use 127 as an example in the comments
REM EXAMPLE Digital/Decimal 127 normal (BINARY): 0111 1111 / 0x7F (hex)
REM EXAMPLE BCD: 127 in BCD is: 0001 0010 0111 (1=0001 2= 0010 7=0111)
REM The check if it worked is to know what the 127 coded in BCD would be interpreted normaly /as a Decimal: 295
Rem Extract each individual decimal digit of the number
Dig0% = DecimalValue% MOD 10 // Extract the units digit --- 127 MOD 10 means Modulo 10 means 127/10 what would be left after the ',' the first diget will be extractet (the Rest) REST 7 > here the 7 (0111) would be extracted
Dig1% = (DecimalValue% / 10) MOD 10 // Extract the tens digit --- 127/10 = 12 /10> REST 2 > here the 2 (0010) would be extracted
Dig2% = (DecimalValue% / 100) MOD 10 // Extract the hundreds digit --- 127/100 = 1/10 > REST 1 (0001)
Dig3% = (DecimalValue% / 1000) MOD 10 // Extract the thousands digit --- 127/1000 = 0 REST 0
Rem Combine these digits into the BCD format
BCDValue% = Dig0% OR (Dig1% * 16) OR (Dig2% * 256) OR (Dig3% * 4096)
REM 0111 OR (0010 * 0001 0000) lets simplify and explain it only on this operation: A Binary Multiplication would have this endresult a shift in places:
REM 0111 OR (0010 0000) an OR combines them together:
REM 0010 0111
REM 0001 0000 0000 = (Dig2% * 256) and OR combines them together again
REM 0001 0010 0111 = BCD 127 > interpreted as 295 if printed
Rem Store the BCD value into Tag2@
Tag2@ = BCDValue%
Rem Print the result for verification
PRINT BCDValue%I hope this can help a bit!
br
i.A. Franziska Gültig
0
Please sign in to leave a comment.
Comments
1 comment