ScriptBasic

Support => ScriptBasic Examples w/source => Topic started by: Fouad_msb on October 15, 2012, 02:19:54 AM

Title: How to read/write variables in Byte data type?
Post by: Fouad_msb 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






























Title: Re: How to read/write variables in Byte data type?
Post by: Support 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.
Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on October 16, 2012, 12:18:13 AM

Thank you very much :)

now I can start with the next step in the program
Title: Re: How to read/write variables in Byte data type?
Post by: Support 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)
Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb 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
Title: Re: How to read/write variables in Byte data type?
Post by: Support 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.

Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on October 20, 2012, 12:01:27 AM
the value is 51.
I think it should be converted to decimal before XOR loop.
Title: Re: How to read/write variables in Byte data type?
Post by: Support 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.  :-\
Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb 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
Title: Re: How to read/write variables in Byte data type?
Post by: Support 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.

Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb 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
Title: Re: How to read/write variables in Byte data type?
Post by: Support 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$
Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on October 25, 2012, 04:03:59 AM
Thanks a lot, It works :)

What's the syntax for "Select Case"?
Title: Re: How to read/write variables in Byte data type?
Post by: Support 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.
Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb 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
Title: Re: How to read/write variables in Byte data type?
Post by: Support on October 26, 2012, 09:55:30 AM
It's been a long time since I played with serial port communications under Windows. I think you need to setup the port from the OS (bits, speed, parity, ...) before using it in your SB script. You may be able to do this from SB with a SYSTEM command prior to opening the COMM port. You are just going to have to play with it to find out what works. I see no reason to close the port between access runs but this is something you will need to experiment with. Please keep us in the loop with your progress.

You don't have to use LINE INPUT and can use INPUT that doesn't wait for an end of line sequence. You tell INPUT how many characters to get and it will return what you requested or whatever is in the buffer at the time.


Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on October 26, 2012, 08:42:29 PM
- ok, what's the command you use to set up the COMM port (bit, speed,parity,...) under Linux?

- I used input with 8 characters/bytes, but still if one device is offline, then it will stop at that
step and will not run the rest of lines in the program!So, I would like to use the command Input with
time like 3 sec maximam so if there is no response from the device, then it just wait for 3 seconds then
goes to the next step.

- What's the command to check the port stauts if it is opened or not?


Thanks a lot, and sorry for bothering you with my many questions :)
Title: Re: How to read/write variables in Byte data type?
Post by: Support on October 26, 2012, 11:02:30 PM
Try a MODE (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/mode.mspx?mfr=true) command to setup your serial port.

The CURL extension module allows connecting to a port with timeout support. (and a bunch of other features) Maybe you can give that a try. SB's socket/serial port support is pretty raw.

Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on October 29, 2012, 03:15:15 PM
I included the module and added the following line:
curl::option CurlHandle,"CONNECTTIMEOUT",5

but the output was as follows:

(0): error &H72: Built in function has too few arguments

========
By the way, I downloaded the attached file which is a sample showing how to do non-blocking serial i/o. But it gives an error
(0): error &H68:The user function "waitevents" is used but is not defined.
(0): error &H68:The user function "setevent" is used but is not defined.
(0): error &H68:The user function "setevent" is used but is not defined.
Title: Re: How to read/write variables in Byte data type?
Post by: Support on October 29, 2012, 08:58:00 PM
ScriptBasic OPEN of sockets and serial ports do NOT support timeouts. SB will wait forever on a port until it's closed.

I didn't have much hope for serial port support with cURL but it looks like you might have discovered a way. If cURL works for serial ports, that is the best way to go.



Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on October 29, 2012, 11:33:49 PM
Regarding the file I attached in prviouse post, to which module belongs the functions : waitevents() and setevents()?
Title: Re: How to read/write variables in Byte data type?
Post by: Support on October 30, 2012, 09:39:59 AM
Where did you find that code? It sort of looks like ScriptBasic. SB is telling you that you're calling functions that don't exist.

If you want to use cURL, you have to IMPORT/INCLUDE the curl.bas file first before calling any of the extension module functions.

Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on October 30, 2012, 07:17:37 PM
Thanks a lot. I really appreciate your support :)
Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on November 11, 2012, 12:27:06 PM
Hi
How are you?  Hope you doing well

Would you please send me the link of which scriptbasic
compiler that could be installed in android!

Thanks a lot
Title: Re: How to read/write variables in Byte data type?
Post by: Support on November 11, 2012, 01:03:31 PM
ScriptBasic is an interpretive scripting language written with ANSI/ISO compliant C compiler.

I have an experimental version of ScriptBasic for Android native Linux (http://www.scriptbasic.org/forum/index.php?action=dlattach;topic=249.0;attach=144) you are welcome to try. I was able to get AIR's SQLite3 extension module working on Android. (self contained with no dependencies)
Title: Re: How to read/write variables in Byte data type?
Post by: Fouad_msb on November 13, 2012, 12:46:51 PM
Hi,

Does it need something other than unzipping and copying it!
Whenever I try to run it,  it gives an error,  file not found
Title: Re: How to read/write variables in Byte data type?
Post by: Support on November 13, 2012, 12:58:56 PM
Check out THIS (http://www.scriptbasic.org/forum/index.php/topic,249.0.html) thread and if you're still having issues, let me know.