GX works 2 Mitsubishi- Swap Bytes

Ranjith

Member
Join Date
May 2007
Location
Melbourne
Posts
316
New to Mitsubishi. Please help me with the following.
What's the easiest way to swap bytes with GX Works2. QCPU03D.
Reading data from a load cell via Modbus TCP. Data is in byte format. 4 bytes per each Variable. Need to convert ABCD bytes to DCBA.
What's the correct syntax for the array elements?
Thanks in advance.






[
 
First of all WTOB does not swap bytes it takes each byte and stores it into a separate word you need to use the SWAP function in a for next loop.
You set a bit to do the swap and if not set jump round the for next loop, Note: you cannot have a Non True bit on a for next loop it will not compile that is why I have put in a conditional jump.
You load zero into the index register (Z) as a pointer
On a trigger it runs a for next loop
The array is then pointed to by the Z register so on first pass My_Array[Z] is My_Array[0] and next 1 etc.
This swaps the bytes in the array then at the end of the for/next reset the trigger bit to stop it continuing to swap bytes.
You could first pass the array to another so you do not destroy the original then swap bytes on the copy.

Swap Bytes in array.png
 
This is an updated version with a second array so as not to destroy the original.
It takes the data in array 1 and copies it to array 2 then swaps the bytes in array 2 leaving the original.

Swap Bytes in array.png
 
Thanks Parky for the detailed explanation.
I am adding some load cells to an existing project.
It's a simple ladder logic project.
So I cannot add Function blocks to the existing one.
Is there a way to convert a simple project to a structured one.
What are the pros and cons?
 
Thanks Parky for the detailed explanation.
I am adding some load cells to an existing project.
It's a simple ladder logic project.
So I cannot add Function blocks to the existing one.
Is there a way to convert a simple project to a structured one.
What are the pros and cons?


Parky,
Just seen your reply to my other post.
Thanks
 
Look at the other post you did on converting however, I got it slightly wrong I was thinking of a single file format doh... The only way would be to get a printout and manually re-code it. Fortunately GXWorks allows you to write it in ladder so should not be too taxing and you could code it in FBD but it will take a bit of time.
 
Yes & no, there are no arrays but Swap My_Array[Z] in Simple mode is
SWAP D10Z0 so the pointer Z0 (Zed Zero) if Z0 was 2 then it points to D12.
so you can indirectly address what is effectively an array, however, I just tried to do it in a simple project and for some reason the pointer to the conditional jump created an error in the simulator, it compiled ok so not sure what the problem is, it may be some bug in the simulator I will try to find out why.
Unfortunately you need to loop with a for next loop & jump round it but although it worked in a structured project it did not in a simple one. I will try it on a real PLC if I can route one out.
 
Here is the equivalent in ladder, I struggled with conditional jump but then realised that you have to put a FEND just before the END instruction it then treats the whole program as a called function without this it will not run the conditional jump although it compiles.
DR: because it's in the ladder format there is no function (AOI) as such, it has to be a structured project which means re-writing it .
You could do a jump to after the FEND and a RET instruction so it treats it like a gosub routine.
See ladder attached.
.
 
Last edited:
Here is the ladder version using a call to a sub routine, to use it you will have to copy & paste the code before the END instruction (do not include the END instruction in the file use the existing one.
You will need to change the bit that does the call (M0) to one you create for triggering the byte swap, note: this has to be reset in the call as shown so you set it somewhere on a oneshot. you will also have to change my D10 & D20 to the first word of your array (you cannot build an array in simple project so I suggest you name each Dataword something like Array_W1, Array_W2 etc) and do the same for the other array. change the length of the For instruction to how many registers you need for example 10.
I have included a zip file as a project and a pdf to look at.
If you want send me a Private message with your email address & I will send you mine so that if you wish you could send me your code & I will modify it for you.
 

Similar Topics

Hi all, anyone does have experience in Gx Works 2? Currently I having an issue which is when certain device added from MELSOFT Navigator and...
Replies
1
Views
107
Hi Folks I have made a function block that has the EN input enabled. (FBD language) In want to run some code at the top of the function block as...
Replies
9
Views
1,722
Hello Friends I am a rookie in mitsubishi. I have many conditions in my program, How can I add several lines to a rung? For example: How can I...
Replies
1
Views
1,507
I am having issues generating a CSV file to reference when I print a report on a project I commissioned last month. I understand the process of...
Replies
1
Views
1,513
Hi Folks, I am trying to export "Global Label" files to CSV, using the "Right Click" and select "Write To CSV File". However, when these files...
Replies
0
Views
2,660
Back
Top Bottom