OPEN-CLOSE

Top  Previous  Next

Action

Opens and closes a device.

 

 

Syntax

OPEN "device"  for MODE As #channel

CLOSE #channel

 

 

Remarks

Device

There are 2 hardware devices supported: COM1 and COM2.

With the software UART, you must specify the port pin and the baud rate.

COM3.0:9600   will use PORT 3.0  at 9600 baud.

Optional is ,INVERTED this will use inverted logic so you don't need MAX232 inverters.

MODE

You can use BINARY, INPUT or OUTPUT for COM1 and COM2, but for the software UART pins, you must specify INPUT or OUTPUT.

Channel

The number of the channel to open. Must be a positive constant.

 

Since there are uP's such as the 80537 with 2 serial channels on board, the compiler must know which serial port you want to use. That is why the OPEN statement is implemented. With only 1 serial port on board, you don't need this statement.

The statements that support the device are PRINT , PRINTHEX, INPUT and INPUTHEX.

 

Every opened device must be closed using the CLOSE #channel statement. Of course you must use the same channel number.

 

The software UART, only supports the GET and PUT statements to retrieve and send data and the PRINTBIN and INPUTBIN statement.

The SW UART uses timed loops and interrupts can slow down these loops.  So turn interrupts off before you use the SW UART.

COM1: and COM2: are hardware ports, and can be used with PRINT etc.

For the software UART it is important that the pin you use is bit addressable. In most cases a PORT is bit addressable but some chips have ports that are not bit addressable. When you use such a port you will get errors like : Error 208, bit variable not found.

Since the OPEN statement doesn't use real file handles like DOS but only serves as a compiler directive, it is important that you must use the CLOSE statement as the last statement in your program.

The following example shows when it will NOT WORK :

 

OPEN "COM2:" FOR BINARY AS #1        'open the port

PRINT #1, "Hello"                        'print to serial 1

Gosub Test

PRINT "Hello"                                'print to serial 0

CLOSE #1

 

Test:

Print #1, "test"

Return

 

Since the compiler frees the handle when it encounters the CLOSE statement, the PRINT #1, "test" code is never executed. To solve this you should put the CLOSE #1 statement under the Return statement.

 

OPEN "COM2:" FOR BINARY AS #1        'open the port

PRINT #1, "Hello"                        'print to serial 1

Gosub Test

PRINT "Hello"                                'print to serial 0

 

 

Test:

Print #1, "test"

Return

Close #1

 

 

 

See also

GET , PUT

 

 

Example 1

'only works with a 80517 or 80537

CONFIG BAUD1 = 9600                        'serial 1 baudrate

OPEN "COM2:" FOR BINARY AS #1        'open the port

PRINT #1, "Hello"                        'print to serial 1

PRINT "Hello"                                'print to serial 0

CLOSE #1                                'close the channel

 

 

Example 2

'works with every port pin

Dim A As Byte , S As String * 16 , I As Byte , Dum As Byte

 

'a software comport is named after the pin you use

'for example P3.0  will be "COM3.0:"  (so there is no P)

'for software comports, you must provide the baudrate

'So for 9600 baud, the devicename is "COM3.0:9600"

'When you want to use the pin for sending, you must open the device for OUTPUT

'When you want to use the pin for receiving, you must open the device for INPUT

 

'At this time only variables can be sent and received with the PUT and GET statements.

'In the feature PRINT etc. will support these software comports.

 

Open "com3.1:9600" For Output As #1      'p3.1 is normally used for tx so testing is easy

Open "com3.0:9600,INVERTED" For Input As #2       'p3.0 is normally used for RX so testing is easy

 

 

S = "test this"                           'assign string

Dum = Len(s)                           'get length of string

For I = 1 To Dum                      'for all characters from left to right

A = Mid(s , I , 1)                      'get character

Put #1 , A                              'write it to comport

Next

 

Do

Get #2 , A                              'get character from comport

Put #1 , A                              'write it back

Print A                                 'use normal channel

Loop

 

Close #1                                  ' finally close device

Close #2

End