PLC-to- Modem Communication Setup


A remotely located M-series or F-Series PLC can be connected to a host PC via public-switch telephone network (PSTN), radio or cellular phone network. This can be accomplished by using two analog modems, one connected to the PLC’s RS232 serial port, and another modem connected to the remote host PC as follow:

There are a some technical issues that need to be handled carefully in order to successfully implement the modem-linked host communications as described in the following sections.

1. Modem Connection

Modem1: The host PC may use any internal or external modem that can communicate at 2400 bps or faster. Connect the modem to the PC as instructed in the modem’s manual and connect the phone line to the phone jack on the back of the modem marked "WALL" or "Line".

Modem2: The modem to be attached to the PLC (modem2) must be an external modem with an RS232 connection port. Since modem are DCE type device, they most likely come with a female type DB25 or DB9 socket meant for plugging into the PC’s RS232 port. Since the PLC’s host link port is also a female DB9, we need to construct a DB9-male-to-DB25-male cable or DB9-male-to-DB9-male cable to link the PLC to the modem, as follow:

wpe6.jpg (12288 bytes)

 

wpe7.jpg (12106 bytes)

2. Communication Speed

When communicating via modems, there are two different definitions of communication speeds that you should be aware of:

The "DTE Speed" or "line rate" is the serial communication speed between the modem and the device connected to its RS232 port. Most modems can automatically detect the RS232 speed of the device and can assume any speed from 1200, 2400 all the way to 115,200 bps. The first ASCII character they receive from the device will determine the DTE speed that the modem will use to communicate with the device.

The "modem-to-modem communication speed" is what you read on the modem specifications, such as 33.6Kbps, 56Kbps etc. When two modems are connected, they automatically negotiate for the best speed to communicate between the two of them based on the quality of the phone connection and the maximum speed that both modems are able to achieve. We usually have no control of what speed they choose to communicate. But one thing is for sure, which is that the modem-to-modem speed is always lower than the DTE speed.

Since the default communication baud rate of the M-series and F-Series PLC’s RS232 serial port is 38,400 bps, the PLC should send a modem initialization string to the modem on the first scan pulse so that the modem can recognize its default DTE speed (i.e. 38,400 bps) in order to talk to the PLC. E.g. To reset the modem, you just have to send an ASCII string "ATZ" to the modem using the following TBASIC command:

PRINT #1 "ATZ"

If you want your modem to automatically answer to an incoming call (e.g. using TLServer 3.x modem dialing capability), then you should execute the following TBASIC statement:

PRINT #1 "ATS0=1"

The above statement will tell the modem to answer on first ring, you can also change the number 1 to other numbers, E.g. if ATS0=3 it will answer on the 3rd ring of the phone.

3. Software and Programming

The TLServer 3.x, which is part of the i-TRiLOGI software suite, already includes built-in support for dialing a modem. Hence if you are using the PLC in passive answer mode only, all the PLC needs to do is to send a modem initialization string "ATS0=1" using the "1st.Scan" pulse to put the modem in auto-answer mode whenever the PLC is powered up. The PLC does not need to issue any more commands to the modem. Whenever a user wants to communicate with the PLC, he/she will first use the TLServer to dial and connect to the PLC’s modem and when the connection is established, he/she will then be able to use the i-TRiLOGI client or the TRi-ExcelLink program to communicate with the PLC. The fact that the PLC is connected via modem and not via direct RS232 is totally transparent to the client programs. To prevent unauthorized access to the PLC, you may need to use the TBASIC command "SETPASSWORD" to set a protective password.

The great flexibility of the F-Series and M-series PLCs becomes even more apparent when you realize that you can easily program the PLC to automatically dial in to the TLServer to perform a number of tasks, such as using the PLC’s File Service to save or append data to hard disk files, send email to anyone via the Internet or even synchronize its real time clock with the host PC!

A number of examples have been included in the "C:\TRiLOGI\TL6\usr\samples\FileService_Modem" folder in TLServer 3.x. All these examples make use of a powerful yet easy to use custom function that was written entirely using the standard TBASIC commands (see source code listing in the text box below). You only need to create the following simple ladder circuit to use this function (assuming it is function #10):

D$ = "ATDT*802"  ' store the phone number - you can put it elsewhere
IF TESTIO(Connected) THEN ' already connected.
    IF TESTIO(DialModem)=0 ' connection no longer needed
       IF DM[3991]=0    ' used as timer for modem attention.
           PRINT #1      ' clear serial-out buffer.
           WHILE INCOMM(1)<> -1 ' clear whatever data in serial-in buffer
           ENDWHILE
       ELSE
           IF DM[3991]=5
               PRINT #1 "+++";  ' get modem attention
           ELSE
               IF DM[3991]>=10 ' Wait 5 second to gain attention.
                   PRINT #1 "ATH" ' hang up modem command.
                   CLRIO CONNECTED
                  DM[3991]=0
               ENDIF
           ENDIF          
       ENDIF
       DM[3991]=DM[3991]+1 ‘ increment the timer
    ENDIF
    RETURN
ENDIF

IF TESTIO(dialModem)=0 RETURN: ENDIF

' If DM[3990] > 0 it means a dialing action has started.
' If DM[3990] > 30 it means more than 30 seconds has passed
' and connection still not established, then retry.

IF DM[3990]=0 ' Use this DM as a flag
   WHILE INCOMM(1)<> -1 ' clear whatever data in serial buffer first.
   ENDWHILE
   PRINT #1 D$ ' Dial the number
   DM[3990]=1
   RETURN
ENDIF

A$ = INPUT$(1)
IF LEN(A$) = 0
   DM[3990]=DM[3990]+1 ' also use it to track the time-out
   IF DM[3990] = 28 ' 28 seconds has lapsed.
      PRINT #1 "ATH"
   ENDIF
   IF DM[3990]>=30: DM[3990]=0: ENDIF
   RETURN
ENDIF

SETLCD 4,1,A$
IF STRCMP(MID$(A$,2,7),"CONNECT")=0 ' is connected
   DM[3990] = 0 ' for next round of connection
   DM[3991] = 0 ' reset timer for hang-up modem use
   SETIO Connected ' set an I/O bit to indicate connection
ENDIF

All you need to do is to copy and paste this custom function to your own ladder+BASIC program, then create an I/O with label name "DialModem" – this may be an input, output, relay, timer or counter contact. The moment this I/O bit "DialModem" is turned on, the PLC will begin to execute the sequence of dialing the remote modem, waiting for a successful connection and then turning on an I/O bit with the label name "Connected". If the dialing cannot be completed within 30 seconds, this custom function will hang up and then re-dial. The process will be repeated indefinitely until either a successful connection is made or if the "DialModem" i/o has been turned OFF.

To disconnect from the modem (hang up), your PLC program just have to turn off the "Dialmodem" I/O bit and the abovementioned custom function will automatically perform the action of hanging up the modem.

Note: Since the PLC does not have a carrier detect (CD) connection to the modem, therefore if the connection is lost after a successful initial connection, the PLC would have no way of knowing it immediately. Your program would have to detect this condition (e.g. if it sends a file service command and does not receive a "<OK>" acknowledgement string from the host). Once the PLC notes that the connection is lost, it can re-establish the connection by simply turning off the I/O bit with label name "Connected". (say, by executing the "CLRIO Connected" statement). As long as the "DialModem" I/O bit is on, the custom function will re-dial and attempt to make another connection if it notices that the "Connected" bit has been turned OFF for whatever reason.