Support > ScriptBasic Examples w/source
How to read/write variables in Byte data type?
Fouad_msb:
Hi,
I need to send and receive values in Bytes (8bits) data type.
But I'm not able to. I tried t::ArrayToString, it looks converted. but couldn't get it back in array.
The same thing for Pack/Unpack. I Packed a value but gives "Undef" when try to unpack it.
I tried the following:
=========
s=PACK("S1",05)
print s ,"\n",LEN(s),"\n"
UNPACK s BY S1 TO d1
print "\n",d1, "\n"
=========
output:
☺5
2
undef
====,=======,=======,
I tried the following also:
s = t::ArrayToString(a[])
t::StringToArray(s,l)
print l
++++++++++
Output:
undef
Support:
--- Code: ---' Packing a integer
np = PACK("I2",12345)
PRINT np,"\n"
UNPACK np BY "I2" TO n
PRINT n,"\n"
' Storing an array in a string
IMPORT t.bas
a[0] = "JRS"
a[1] = 2012
s = t::ArrayToString(a)
t::StringToArray(l,s)
PRINT l[0],"\n"
PRINT l[1],"\n"
--- End code ---
jrs@laptop:~/sb/test$ scriba mytest.sb
90
12345
JRS
2012
jrs@laptop:~/sb/test$
Works fine for me. You were trying to PACK a number with a string type.
Hope this helps.
Fouad_msb:
Thank you very much :)
now I can start with the next step in the program
Support:
Glad to hear you're able to move on with your project.
Someday I plan to add array sort to the T extension module. (the plate is a bit full at the moment)
Fouad_msb:
Actually I have a device which need to send/receive data in Bytes!
I just made the portion of turning it ON, but still no action.
I could do that in VB6, but don't know how to convert it to Scriptbasic.
Following the code written in VB6:
Private Sub Form_Load()
connect
End Sub
Private Sub Command1_Click()
'Sending Command to device
Dim Send_Buf(7) As Byte
Send_Buf(0) = &H2
Send_Buf(1) = &H6
Send_Buf(2) = &H4
Send_Buf(3) = &H1
Send_Buf(4) = &H22
Send_Buf(5) = &H10
Send_Buf(6) = &H0
Send_Buf(7) = &H0
For i = 0 To 6
Send_Buf(7) = Send_Buf(7) Xor Send_Buf(i)
Next
SendData Send_Buf
End Sub
Public Sub connect()
If Winsock1.State <> sckConnected Then
Winsock1.Close
DoEvents
Winsock1.RemoteHost = "192.168.100.183"
Winsock1.RemotePort = 5555
Winsock1.connect
DoEvents
End If
End Sub
Public Sub SendData(Send_Buf() As Byte)
On Error Resume Next
If Winsock1.State = sckConnected Then
Winsock1.SendData Send_Buf
DoEvents
Else
connect
Winsock1.SendData Send_Buf
DoEvents
End If
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
'On Error Resume Next
Dim bytData() As Byte
Winsock1.GetData bytData(), vbByte + vbArray, bytesTotal
If bytesTotal = 8 Then
If bytData(2) = m_address Then
SetStatus bytData
End If
End If
End Sub
Public Function IsBitSet(InByte As Byte, Bit As Byte) As Boolean
IsBitSet = ((InByte And (2 ^ Bit)) > 0)
End Function
Public Function GetWMode(InByte As Byte) As Byte
If IsBitSet(InByte, 4) Then
'cool or auto
End If
End Sub
Public Sub SetStatus(Rcv_Buf() As Byte)
On Error Resume Next
If IsBitSet(Rcv_Buf(4), 7) Then
'OFF status
ac_state = False
Else
'ON Status
ac_state = True
End Sub
=========
Thanks & Best Regards
Fouad
Navigation
[0] Message Index
[#] Next page
Go to full version