Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BASCOM AVR, help reference (2007).PDF
Скачиваний:
293
Добавлен:
12.08.2013
Размер:
17.02 Mб
Скачать

© MCS Electronics, 1995-2007

'The software UART will not work on the hardware UART pins 'so you must choose other pins

'use normal hardware UART for printing 'Print B

'When you dont want to use a level inverter such as the MAX-232 'You can specify ,INVERTED :

'Open "comd.0:300,8,n,1,inverted" For Input As #2

'Now the logic is inverted and there is no need for a level converter 'But the distance of the wires must be shorter with this

End

CLOSESOCKET

Action

Closes a socket connection.

Syntax

CloseSocket socket [ , prm]

Remarks

Socket

Prm

The socket number you want to close in the range of 0-3. When the socket is already closed, no action will be performed.

An optional parameter to change the behavior of the CloseSocket statement. The following values are possible :

0 - The code will behave as if no parameter has been set.

1 - In normal cases, there is a test to see if all data written to the chip has been sent. When you set bit 0 (value of 1) , this test is not performed.

2 - In normal cases, there is a test to see if the socket is actually closed after the command has been given to the chip. When it is not closed, you can not re-use the socket. The statement will block program execution however and you could test at a later time if the connection has been closed.

You may combine the values. So 3 will combine parameter value 1 and 2. It is advised to use option value 1 with care.

You must close a socket when you receive the SOCK_CLOSE_WAIT status. You may also close a socket if that is needed by your protocol.

You will receive a SOCK_CLOSE_WAIT status when the server closes the connection.

When you use CloseSocket you actively close the connection.

Note that it is not needed to wait for a SOCK_CLOSE_WAIT message in order to close a socket connection.

After you have closed the connection, you need to use GetSocket in order to use the socket number again.

In normal conditions, without using the optional parameter, the statement can block your code for a short or longer time, depending on the connection speed.

page -308-

© MCS Electronics, 1995-2007

See also

CONFIG TCPIP, GETSOCKET , SOCKETCONNECT, SOCKETSTAT , TCPWRITE, TCPWRITESTR, TCPREAD, SOCKETLISTEN

Example

'-----------------------------------------------------------------------------

 

 

------------

: clienttest.bas

 

'name

 

'copyright

: (c) 1995-2005, MCS Electronics

 

'purpose

: start the easytcp.exe program and listen to port

5000

: Mega161

 

'micro

 

'suited for demo

: no

 

'commercial addon needed

: yes

 

'-----------------------------------------------------------------------------

 

 

------------

 

 

$regfile = "M161def.dat"

 

 

$crystal = 4000000

 

 

$baud = 19200

 

' default use 40

$hwstack = 40

 

for the hardware stack

 

' default use 40

$swstack = 40

 

for the SW stack

 

' default use64

$framesize = 64

 

for the frame space

 

 

Const Sock_stream = $01

 

' Tcp

Const Sock_dgram = $02

 

' Udp

Const Sock_ipl_raw = $03

 

' Ip Layer Raw

Sock

 

' Mac Layer Raw

Const Sock_macl_raw = $04

 

Sock

 

' Confirm Socket

Const Sel_control = 0

 

Status

 

' Confirm Tx Free

Const Sel_send = 1

 

Buffer Size

 

' Confirm Rx Data

Const Sel_recv = 2

 

Size

 

 

'socket status

 

' Status Of

Const Sock_closed = $00

 

Connection Closed

 

' Status Of Arp

Const Sock_arp = $01

 

Const Sock_listen = $02

 

' Status Of

Waiting For Tcp Connection Setup

' Status Of

Const Sock_synsent = $03

 

Setting Up Tcp Connection

 

' Status Of

Const Sock_synsent_ack = $04

Setting Up Tcp Connection

 

' Status Of

Const Sock_synrecv = $05

 

Setting Up Tcp Connection

 

' Status Of Tcp

Const Sock_established = $06

Connection Established

 

' Status Of

Const Sock_close_wait = $07

Closing Tcp Connection

 

' Status Of

Const Sock_last_ack = $08

 

Closing Tcp Connection

 

' Status Of

Const Sock_fin_wait1 = $09

 

Closing Tcp Connection

 

' Status Of

Const Sock_fin_wait2 = $0a

 

Closing Tcp Connection

 

' Status Of

Const Sock_closing = $0b

 

page -309-

© MCS Electronics, 1995-2007

 

Closing Tcp Connection

' Status Of

Const Sock_time_wait = $0c

Closing Tcp Connection

' Status Of

Const Sock_reset = $0d

Closing Tcp Connection

' Status Of Socket

Const Sock_init = $0e

Initialization

' Status Of Udp

Const Sock_udp = $0f

Const Sock_raw = $10

' Status of IP RAW

$lib "tcpip.lbx"

' specify the

tcpip library

' display a

Print "Init , set IP to 192.168.0.8"

message

' before we use

Enable Interrupts

config tcpip , we need to enable the interrupts

Config Tcpip = Int0 , Mac = 12.128.12.34.56.78 , Ip = 192.168.0.8 , Submask = 255.255.255.0 , Gateway = 0.0.0.0 , Localport = 1000 , Tx = $55 , Rx = $55

'Use the line below if you have a gate way

'Config Tcpip = Int0 , Mac = 12.128.12.34.56.78 , Ip = 192.168.0.8 , Submask = 255.255.255.0 , Gateway = 192.168.0.1 , Localport = 1000 , Tx = $55 , Rx = $55

Dim Bclient As Byte

' socket number

Dim Idx As Byte

' result

Dim Result As Word

Dim S As String * 80

 

For Idx = 0 To 3

' for all sockets

Bclient = Getsocket(idx , Sock_stream , 0 , 0)

' get socket for

client mode, specify port 0 so loal_port is used

' print local port

Print "Local port : " ; Local_port

that was used

 

Print "Socket " ; Idx ; " " ; Bclient

' connect to

Result = Socketconnect(idx , 192.168.0.3 , 5000)

easytcpip.exe server

 

Print "Result " ; Result

 

Next

 

Do

 

If Ischarwaiting() <> 0 Then

' is there a key

waiting in the uart?

' get the key

Bclient = Waitkey()

If Bclient = 27 Then

' send WHO , TIME

Input "Enter string to send " , S

or EXIT

 

For Idx = 0 To 3

 

Result = Tcpwritestr(idx , S , 255)

 

Next

 

End If

 

End If

 

For Idx = 0 To 3

' get status

Result = Socketstat(idx , 0)

Select Case Result

 

Case Sock_established

' get number of

Result = Socketstat(idx , Sel_recv)

bytes waiting

 

If Result > 0 Then

 

Do

 

Result = Tcpread(idx , S)

 

Print "Data from server: " ; Idx ; " " ; S

 

page -310-

© MCS Electronics, 1995-2007

Loop Until Result = 0

End If

Case Sock_close_wait

Print "close_wait"

Closesocket Idx

Case Sock_closed

'Print "closed"

End Select

Next

Loop

End

CONFIG

The CONFIG statement is used to configure the various hardware devices.

DIRECTIVE

RE-USABLE

CONFIG 1WIRE

NO

CONFIG ACI

YES

CONFIG ADC

NO

CONFIG ATEMU

NO

CONFIG BCCARD

NO

CONFIG CLOCK

NO

CONFIG CLOCKDIV

YES

CONFIG COM1

YES

CONFIG COM2 also COM3,

YES

COM4

 

CONFIG DATE

NO

CONFIG DCF77

NO

CONFIG DEBOUNCE

NO

CONFIG GRAPHLCD

NO

CONFIG I2CDELAY

NO

CONFIG I2CSLAVE

NO

CONFIG INPUT

NO

CONFIG INTx

YES

CONFIG KBD

NO

CONFIG KEYBOARD

NO

CONFIG LCD

NO

CONFIG LCDBUS

NO

CONFIG LCDMODE

NO

CONFIG LCDPIN

NO

CONFIG RC5

NO

CONFIG PORT

YES

CONFIG PRINT

NO

CONFIG PRINTBIN

NO

CONFIG SERIALIN

NO

CONFIG SERIALIN1

NO

CONFIG SERIALOUT

NO

CONFIG SERIALOUT1

NO

CONFIG SERVOS

NO

page -311-