Author Topic: How to read/write variables in Byte data type?  (Read 97450 times)

Fouad_msb

  • Guest
How to read/write variables in Byte data type?
« on: October 15, 2012, 02:19:54 AM »
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






























« Last Edit: October 15, 2012, 02:24:11 AM by Fouad_msb »

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: How to read/write variables in Byte data type?
« Reply #1 on: October 15, 2012, 09:04:40 AM »
Code: [Select]
' 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"

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.
Script BASIC Project Manager

Fouad_msb

  • Guest
Re: How to read/write variables in Byte data type?
« Reply #2 on: October 16, 2012, 12:18:13 AM »

Thank you very much :)

now I can start with the next step in the program

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: How to read/write variables in Byte data type?
« Reply #3 on: October 16, 2012, 09:51:33 AM »
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)
Script BASIC Project Manager

Fouad_msb

  • Guest
Re: How to read/write variables in Byte data type?
« Reply #4 on: October 18, 2012, 03:05:16 PM »
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

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: How to read/write variables in Byte data type?
« Reply #5 on: October 18, 2012, 03:26:35 PM »
Code: [Select]
Send_Buf[0] = CHR(&H2)
Send_Buf[1] = CHR(&H6)
Send_Buf[2] = CHR(&H4)
Send_Buf[3] = CHR(&H1)
Send_Buf[4] = CHR(&H22)
Send_Buf[5] = CHR(&H10)
Send_Buf[6] = CHR(&H0)
Send_Buf[7] = CHR(&H0)
FOR i = 0 TO 6
  Send_Buf[7] = Send_Buf[7] XOR Send_Buf[i]
NEXT

PRINT ASC(Send_Buf[7]),"\n"

jrs@laptop:~/sb/test$ scriba mytest2.sb
48
jrs@laptop:~/sb/test$

Does your VB code return the same value as above?

The rest is simply opening up a socket and using PRINT and INPUT to communicate with the device.

Script BASIC Project Manager

Fouad_msb

  • Guest
Re: How to read/write variables in Byte data type?
« Reply #6 on: October 20, 2012, 12:01:27 AM »
the value is 51.
I think it should be converted to decimal before XOR loop.

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: How to read/write variables in Byte data type?
« Reply #7 on: October 20, 2012, 09:25:58 AM »
Code: [Select]
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

PRINT Send_Buf[7],"\n"

jrs@laptop:~/sb/test$ scriba mytest3.sb
51
jrs@laptop:~/sb/test$

My error, I shouldn't have converted it to a character value first.  :-\
Script BASIC Project Manager

Fouad_msb

  • Guest
Re: How to read/write variables in Byte data type?
« Reply #8 on: October 21, 2012, 11:02:38 PM »
it works fine. thanks a lot :)

I have another question, how to make a timer! without stopping the program sequence?
I tried to make a loop, but it will not go to then next step.

I need to pull the data every 3 sec, in the same time that will not affect the other commands like which need to send it to the device.

for example, I have a thermostat which need to read the temperature every 3 sec, and in the same time I can send or change the setpoint by sending a command to the device.

thanks a lot. I reall appreciate your support.
Fouad

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: How to read/write variables in Byte data type?
« Reply #9 on: October 22, 2012, 10:01:56 AM »
ScriptBasic doesn't support timers like VB so you may have to be a little creative with this. You have a few ways to approach this.

1. You could create a MAIN routine that checks NOW and runs a set of SUBs/FUNCTIONs and SLEEPs when there is nothing to do.

2. Create two scripts. One that polls devices as gets info and write it to a file. The other script would process the file on a first in first out basis.

3. Create an extension module with a true timer and access your device from the extension module.

Script BASIC Project Manager

Fouad_msb

  • Guest
Re: How to read/write variables in Byte data type?
« Reply #10 on: October 24, 2012, 05:32:34 AM »
How Can I use the serial port instead of the ethernet?
does the following command is correct? I tried it but still gives an error "(0): error &H16:The file can not be opened."
============
open "COM1:4800" for comm as 1

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: How to read/write variables in Byte data type?
« Reply #11 on: October 24, 2012, 07:33:56 AM »
Quote
open "COM1:4800" for comm as 1

Try this.

Code: [Select]
OPEN "COM1:4800" FOR BINARY AS #1

I tried the following with my old laptop running Ubuntu that has an internal modem.

Code: [Select]
OPEN "/dev/ttySL0" FOR BINARY AS #1
PRINT #1,"ATZ\r"
LINE INPUT #1, mr
PRINT mr
LINE INPUT #1, mr
PRINT mr

jrs@laptop:~/sb/test$ scriba testmodem.sb
ATZ
OK
jrs@laptop:~/sb/test$
« Last Edit: October 24, 2012, 11:40:14 AM by support »
Script BASIC Project Manager

Fouad_msb

  • Guest
Re: How to read/write variables in Byte data type?
« Reply #12 on: October 25, 2012, 04:03:59 AM »
Thanks a lot, It works :)

What's the syntax for "Select Case"?

support

  • Administrator
  • *****
  • Posts: 859
    • Script BASIC Open Source Project
Re: How to read/write variables in Byte data type?
« Reply #13 on: October 25, 2012, 12:03:31 PM »
Quote
What's the syntax for "Select Case"?

Code: [Select]
IF case = 1 THEN
  ' Do case 1 stuff
ELSE IF case = 2 THEN
  ' Do case 2 stuff
ELSE
  ' Do default case stuff
END IF

Sorry, no SELECT/CASE but IF/THEN/ELSE has unlimited nesting.
Script BASIC Project Manager

Fouad_msb

  • Guest
Re: How to read/write variables in Byte data type?
« Reply #14 on: October 25, 2012, 09:39:43 PM »
ok, thanks.

1-The command you sent me "OPEN "COM1:4800" FOR BINARY AS #1" for opening the seiral port is working fine, but sometimes it doesn't read till I use a serial port monitor software, then after closing it , it can work in script basic! I guess need to specify the Data Bits, Parity & Stop Bits. So, would you please help me how to specify these data for the com port!

2- I need to read the temperature every 3 Seconds, and in that I open the port & close it again. So, how can I check if the port (Ethernet + Com port) is already opened to avoid opening it again and avoid errors?.

3- What's your suggestion when need to read the temperature or any data in such small period, shall I keep the port open or just open it then close it at the end of the function?

4- How to make the line input (waiting to receive the reply from the device or port" specified with a time? so, that step will not stop the process for long and doesn't affect the next step of the sequence. For example if I'm pulling data from 3 devices and if the first one is offline, than it will stop reading data from other devices.

Thanks a lot
« Last Edit: October 26, 2012, 01:25:50 AM by Fouad_msb »