<< Prev        Next >>

ID : 370

Comm.Open

Function

To open the line for data communication.

Syntax

Comm.Open line number

Guaranteed Entry

Line number
Designate a line number by integer type data.

Description

The line is opened for data communication.

Attention

Exclusive Control of Comm.Open

When a task, which has opened a line, is running, other tasks cannot open or use the line until the task executes Comm.Close command and the line is closed.

However, if the task stops without closing the line, other tasks can open and use the line.
To check whether the line is occupied by tasks, use Comm.State command.

To share the opened task with other tasks, set the program parameter [34:Permission for a line sharing for multiple tasks] to [1:Enable].
For details about the way of setting, refer to "Displaying and Changing Program Parameters" of TEACH PENDANT OPERATION GUIDE.
If multiple Comm.input commands or multiple Comm.output commands are executed as multitasking, the latter command will wait the completion of the former command.

Comm.Open after Program Stop

When once opened line stops the task without executing Comm.Close, the exclusive control of the task is released.
However, the line keeps open state. This helps to reduce CPU usage for Comm.Open from the second time or later.

(1)When the line number 1 opens, a program stops without Comm.Close or, a program stops incorrectly.

(2)CPU usage is saved when the same or other programs open the line number 1 again.

To close the line at every program stop, set the program parameter [32:Auto-disconnection at task end] to [1:Enable].
For details about the way of setting, refer to "Displaying and Changing Program Parameters" of TEACH PENDANT OPERATION GUIDE.
This setting will be ignored when [34:Permission for a line sharing for multiple tasks] is set to [1:Enabled].

Behaviors According to the Setting Values for the Task-Related Options

Setting value 0: Disable 1: Enable
Description A communication line keeps OPEN even if a task that has executed Comm.Open is stopped. A communication line is automatically disconnected if a task that has executed Comm.Open is stopped.
Advantage If "0: Disable" is selected, Comm.Output and Comm.Input can be executed quickly when tasks are continuously executed, since Open/Close processing are not repeated. A line does not keep Open-state because it is automatically disconnected as soon as the task stops (e.g; task-error).
Disadvantage Because a line is not automatically disconnected when a task is stopped by a task-error, you need to explicitly execute Comm.Close. Need to implement so that a line is used only when a task is running.

Example

'!TITLE "DENSO Robot Program"
Sub Sample_CommOpen 
  On Error GoTo Label1
  Comm.Open 8
  On Error GoTo 0
  
  'Describe a process
  
  Exit Sub
Label1:  'When you cannot open line number 8
  Delay 100  'Waiting for server process.
  Resume 'Execute Open line again.

End Sub

The following shows the sample code to establish communication between Client and Server through Ethernet.

Sample Code for the Server-Side (Server Setting : Text Mode)

After Comm.Open is executed, the Server-side needs to wait the connection from the Client-side by executing Comm.State.
An error will occur if Comm.Output or Comm.Input is executed when connection is not established.

Sub Main

  Comm.Open 4
  Comm.Clear 4 'Clear the reception buffer
  Wait Comm.State(4) = 2  'Wait for the connection from the client

  I1 = 0
  Do

   S1 = Comm.Input(4)
   PrintDbg S1

   Comm.Output 4, "OK " & Timer
   
   Delay 1
  Loop Until I1<>0

  Comm.Close 4
End Sub

Sample Code for the Client-Side (Client Setting : Text Mode)

Sub Main
  Comm.Open 8

  I1 = 0
  Do

    Comm.Output 8, "Do " & Timer

    S1 = Comm.Input(8)
    PrintDbg S1
   
    Delay 1
  Loop Until I1<>0 

  Comm.Close 8
End Sub

Sample Code for the Automatic Recovery at the Disconnection on the Server-Side (Server Setting : Text Mode)

The following sample code is used to explicitly write the processing for reconnection after the line has been disconnected at the Server-side.

Sub Main

  Comm.Open 4
  Comm.Clear 4
  Wait Comm.State(4) = 2  'Wait for the connection from the client

  On Error Goto ErrHandler
  I1 = 0
  Do

   S1 = Comm.Input(4)
   PrintDbg S1

   Comm.Output 4, "OK " & Now   

   Delay 1
  Loop Until I1<>0

  Comm.Close 4

  Exit Sub

ErrHandler:
  Comm.Close 4, -1  'Compulsory disconnect
  PrintDbg "Err = " & Hex(Err.OriginalNumber)
  Comm.Open 4
  Comm.Clear 4
  Wait Comm.State(4) = 2  'Wait for the connection from the client
  Resume 'Clear the error-state, and resume the program from the line where an error occurred  

End Sub

ID : 370

<< Prev        Next >>