PDA

View Full Version : SERIN2, SEROUT2 with long variables



pedja089
- 18th November 2010, 23:22
First time using long variables, so I need little clarification...
When I used word variables, I used this code to receive it:

CNT1 VAR WORD
PC_in:
SERIN2 Rx,84,3000,PC_in,[WAIT("DATA:"), CNT.byte1,CNT1.byte0]
'And to display it:
SEROUT2 Tx,84,["CNT1:",DEC5 CNT1,10,13]
In VB6 code for extracting bytes from word and to send:

Private Sub Command1_Click()
Dim Str As String, B1 As Byte, B2 As Byte
Dim CNT As Long, C1 As long

CNT = CLng(TxtCNT.Text)
C1 = 256
B2 = Br \ C1
B1 = Br - C1 * B2
Str = "DATA:" & Chr(B2) & Chr(B1)
MsComm1.Output=Str
End Sub

Now I tried same with word variables:
PBP:

CNT VAR LONG
PC_in:
SERIN2 Rx,84,3000,PC_in,[WAIT("DATA:"), CNT.BYTE3, CNT1.BYTE2, CNT1.BYTE1, CNT1.BYTE0]
SEROUT2 Tx,84,["CNT1:",DEC9 CNT1,10]
VB:

Dim Str As String, B1 As Byte, B2 As Byte, B3 As Byte, B4 As Byte,
Dim CNT As Long, C1 As Long, C2 As Long, C3 As Long, C4 As Long

CNT = CLng(TxtCNT.Text)
C1 = 16777216
C2 = 65536
C3 = 256
B4 = CNT \ C1
CNT = CNT - B4 * C1
B3 = CNT \ C2
CNT = CNT - B3 * C2
B2 = CNT \ C3
B1 = CNT - C3 * B2
Str = "DATA:" & B4 & B3 & B2 & B1
MsComm1.Output=Str
End Sub
But doesn't work.
VB extracted and sent bytes correctly.
But for example when I sent number 400401402, I receive 842215986.
Does anyone know why this happens and how to correct that?
EDIT:
Now I see mistake:eek:
Maybe it's time to rest a little:D
VB obviously not sending correctly, It should be

Str = "DATA:" & Chr(B4) & Chr(B3) & Chr(B2) & Chr(B1)
Excuse me for harassment.