Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

[CR1000] modbusmaster UINT16 convert AB to BA


CEBTP Mar 7, 2016 02:26 PM

Greetings,

I have a little problem to convert my register from a modbus UINT16 need to change the order of the byte :

Modbus Receipt 02 03 10 E5 B0 03 03 AD 0F 03 05...

First Sensor 2 registers 16UINT Format AB CD {E5 B0 03 03} but I need (DC BA) to have {03 03 B0 E5} 

Second Sensor {AD 0F 03 05}  need {05 03 0F AD}

Don't need the 32 bytes coz I had a formula with the two each 16 bytes

PreserveVariables

ConstTable
Const tempEnreg=30
Const unitEnreg=3
Const tempScrut=2
Const unitScrut=2
Const nbrCptF=2 
EndConstTable

Const nbrRegF=nbrCptF*2
Const mbPort=Com1

Public etaBatt : Units etaBatt=V
Public tempInt : Units tempInt=Deg C
Public etatCom
Public modBusReg(nbrRegF) As Long
Public ConvReg(nbrRegF) As Long
Public Result(nbrCptF)

Dim i

DataTable (Enreg,1,-1)
DataInterval (0,tempEnreg,unitEnreg,10)
Sample (1,etaBatt,FP2)
Sample (1,tempInt,FP2)
Sample (nbrCptF,Result(),IEEE4)
EndTable

BeginProg

Scan (tempScrut,unitScrut,0,0)

Battery (etaBatt)
PanelTemp (tempInt,250)

'Data in UINT16 mode 3 OS>=28

ModbusMaster (etatCom,mbPort,9600,2,3,modBusReg(),801,nbrRegF,1,2,3)

'TRY to Convert register AB to BA and CD to DC

For i=1 To nbrRegF Step 1
MoveBytes(ConvReg(i),0,modBusReg(i),8,1)
MoveBytes(ConvReg(i),1,modBusReg(i),9,1)
MoveBytes(ConvReg(i),2,modBusReg(i),10,1)
MoveBytes(ConvReg(i),3,modBusReg(i),11,1)
MoveBytes(ConvReg(i),4,modBusReg(i),12,1)
MoveBytes(ConvReg(i),5,modBusReg(i),13,1)
MoveBytes(ConvReg(i),6,modBusReg(i),14,1)
MoveBytes(ConvReg(i),7,modBusReg(i),15,1)
MoveBytes(ConvReg(i),8,modBusReg(i),0,1)
MoveBytes(ConvReg(i),9,modBusReg(i),1,1)
MoveBytes(ConvReg(i),10,modBusReg(i),2,1)
MoveBytes(ConvReg(i),11,modBusReg(i),3,1)
MoveBytes(ConvReg(i),12,modBusReg(i),4,1)
MoveBytes(ConvReg(i),13,modBusReg(i),5,1)
MoveBytes(ConvReg(i),14,modBusReg(i),6,1)
MoveBytes(ConvReg(i),15,modBusReg(i),7,1)
Next i

'Out of bound ??? Doesn't work and the help file isn't very clear...Why can't work directly in hexa ?

'Result = (DC * 65535 + BA)/(2^15)
Result(1)=((ConvReg(2)*65535+ConvReg(1))/(2^15))
Result(2)=((ConvReg(4)*65535+ConvReg(3))/(2^15))

CallTable Enreg
NextScan
EndProg

Any help is welcome.

Best regards.


JDavis Mar 7, 2016 05:02 PM

Here is a simplified example of using Movebytes to flip byte orders. You can directly move bytes into a Long when extracting integers from binary data. 

Public Source As Long, Flipped As Long
Public Unsigned16 As Long
Public HexString As String

'Main Program
BeginProg
Source = &hAD0F0305
Scan (1,Sec,0,0)

MoveBytes (Flipped,0,Source,3,1)
MoveBytes (Flipped,1,Source,2,1)
MoveBytes (Flipped,2,Source,1,1)
MoveBytes (Flipped,3,Source,0,1)

HexString = Hex(Flipped)

'Extract a 16 bit unsigned and place it in a Long. A 32 bit signed integer can contain a 16 bit unsigned.
Unsigned16 = 0
'The CR1000 is big endian. So the bytes go in bytes 2 and 3 (the third and fourth bytes).
MoveBytes (Unsigned16,2,Source,1,1)
MoveBytes (Unsigned16,3,Source,0,1)

NextScan
EndProg


CEBTP Mar 8, 2016 09:33 AM

Thank you,

but I found a mathematical way to resolve it, I will try yours (someday).

MSB%256*256+(MSB-MSB%256)/256       UINT16(AB)-> UINT16(BA)

same with the LSB


kokihualpa Sep 25, 2016 02:47 PM

excuseme miss davis

in your example:

Source = &hAD0F0305

you use a hexadecimal string of 4 bytes. but what happen if you need send more bytes to dispositive for a serial port?

example

FFAA53030001000001

I see that movebytes cut to 4 bytes always.

Log in or register to post/reply in the forum.