Skip to content

Commit c450e40

Browse files
authored
Update 594-file-open.markdown
1 parent d4f5911 commit c450e40

1 file changed

Lines changed: 87 additions & 22 deletions

File tree

_build/reference/594-file-open.markdown

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
55
Makes a file, device or network connection available for sequential input, sequential output.
66

7-
* file - A string expression that follows OS file naming conventions.
8-
* fileN - A file-handle (integer 1 to 256).
7+
* `file` - A string expression that follows OS file naming conventions.
8+
* `fileN` - A file-handle (integer 1 to 256).
99

10-
### File-mode
10+
### Open a file
11+
12+
`open "test.txt" for INPUT as #1`
1113

1214
| Expression | Description |
1315
|------------|---------------------------------------------|
@@ -19,24 +21,34 @@ If an existing file is opened for output, the file will be deleted and an empty
1921
The files are always opened as shared. You can use FREEFILE to get the next unused file-handle. Use PRINT,
2022
INPUT, BGETC and BPUTC to read from or write to a file or device.
2123

22-
### Open RS232, socket, URL or image
24+
#### Open a COM port (RS232)
25+
26+
You can open a connection to a device using the serial port with `open "COMn:speed" AS #1`, where `n` is the number of the port
27+
and `speed` is the baud rate. To open the first serial port with a baud rate of 19200 use: `open "COM1:19200" as #1`. In Windows
28+
COM1 and in Linux /dev/ttys1 would be opened.
2329

24-
With OPEN you can also open a network connection. Depending on the kind of connection the following file names can be used:
30+
#### Open a TCP/IP socket
2531

26-
| Connection Type | Example |
27-
|---------------------|---------|
28-
| URL | `open "http://api.duckduckgo.com/?q=Cars&format=json" as #1`
29-
| Image | `open "http://img2.wikia.nocookie.net/__cb20150113215904/farmville/images/9/92/Lumberjack_Gnome-icon.png" as #1` |
30-
| Socket | `open "SOCL:192.168.178.76:8080" as #1` |
31-
| Serial Port (RS232) | `open "COM1:19200" as #1` |
32+
You can open a connection to a device using a TCP/IP socket with `open "SOCL:IP:PORT" AS #1`, where `IP` is a valid host name or IP address
33+
and `port` is an open port.
3234

33-
More information with examples can be found in the article "Network programming".
35+
`open "SOCL:192.168.178.76:8080" as #1`
3436

35-
#### Open COM port (RS232)
37+
If you omit the host name or IP address in the `SOCL:` string passed to the OPEN command, SmallBASIC will listen for connections from another host/process.
3638

37-
You can open a connection to a device using the serial port with `open "COMn:speed" AS #1`, where n is the number of the port
38-
and speed is the baud rate. To open the first serial port with a baud rate of 19200 use: `open "COM1:19200" as #1`. In Windows
39-
COM1 and in Linux /dev/ttys1 would be opened.
39+
`open "SOCL:8080" as #1`
40+
41+
### Open an image
42+
43+
Use the prefix `HTTP:` with the `OPEN` command to open an image file over the network. You can then pass the file number to the `IMAGE` command. This returns an system object which can then be used to manipulate images in the graphical version of SmallBASIC.
44+
45+
`open "http://img2.wikia.nocookie.net/__cb20150113215904/farmville/images/9/92/Lumberjack_Gnome-icon.png" as #1`
46+
47+
### Open a HTTP connection
48+
49+
Use the prefix `HTTP:` with the `OPEN` command to open a network HTTP connection. You can then use the file number with the `TLOAD` command to read the data.
50+
51+
`open "http://api.duckduckgo.com/?q=Cars&format=json" as #1`
4052

4153
### Example 1: Open a file:
4254

@@ -61,17 +73,17 @@ wend
6173
close #1
6274
```
6375

64-
### Example 2: Open a socket
76+
### Example 2: Open a socket for listening (server)
6577

6678
```
6779
open "SOCL:10000" as #1 ' Open socket at port 10000
6880
69-
while (eof(1)) ' Loop until connection is closed
81+
while (!eof(1)) ' Loop until connection is closed
7082
7183
l = lof(1) ' Querry how much data is in the queue
7284
7385
if(l) then ' if data is available
74-
s = INPUT(l, #1) ' get all data
86+
s = INPUT(l, 1) ' get all data
7587
print s
7688
endif
7789
@@ -80,17 +92,27 @@ wend
8092
close #1
8193
```
8294

83-
### Example 3: Open a COM port (RS232)
95+
### Example 3: Open a socket as client
96+
97+
```
98+
open "SOCL:192.168.1.10:10000" as #1 ' Connect to server
99+
100+
print #1, "Hello world" ' Send string to server
101+
102+
close #1
103+
```
104+
105+
### Example 4: Open a COM port (RS232)
84106

85107
```
86108
open "COM1:19200" as #1 ' Open COM1 with 19200 bauds
87109
88-
while (eof(1)) ' Loop until connection is closed
110+
while (!eof(1)) ' Loop until connection is closed
89111
90112
l = lof(1) ' Querry how much data is in the queue
91113
92114
if(l) then ' if data is available
93-
s = INPUT(l, #1) ' get all data
115+
s = INPUT(l, 1) ' get all data
94116
print s
95117
endif
96118
@@ -99,3 +121,46 @@ wend
99121
close #1
100122
```
101123

124+
### Example 5: Open an image over network
125+
126+
```
127+
' open some random image I found on the net
128+
open "http://img2.wikia.nocookie.net/__cb20150113215904/farmville/images/9/92/Lumberjack_Gnome-icon.png" as #1
129+
130+
i = image(#1)
131+
i.show(100,50)
132+
```
133+
134+
### Example 6: Connect to an HTTP server
135+
136+
```
137+
print "DuckDuckGo Search"
138+
while 1
139+
print '<=== when cycle around need to isolate input prompt
140+
input "(Just enter quits) Term? ", queryTerm
141+
if trim(queryTerm)="" then ? "Cheers!":end '<=== need a way out
142+
url = "http://api.duckduckgo.com/?q=" + trim(queryTerm) + "&format=json"
143+
open url as #1
144+
if (eof(1)) then
145+
throw "Connection failed: " + url
146+
fi
147+
148+
dim results
149+
tload #1, results
150+
json = array(results)
151+
num_results = len(json.RelatedTopics)
152+
for i = 0 to num_results - 1
153+
if (isarray(json.RelatedTopics(i).topics)) then
154+
num_topics = len(json.RelatedTopics(i).Topics)
155+
for t = 0 to num_topics - 1
156+
print cat(1); " "; json.RelatedTopics(i).Topics(t).FirstURL; cat(0)
157+
print " "; json.RelatedTopics(i).Topics(t).text
158+
next t
159+
else
160+
print cat(1); " "; json.RelatedTopics(i).FirstURL; cat(0)
161+
print " "; json.RelatedTopics(i).Text
162+
endif
163+
next i
164+
Close #1 '<===== oh this helps!
165+
wend
166+
```

0 commit comments

Comments
 (0)