Lean MES Open Source Manufacturing Execution System
Flexible Manufacturing Solutions through Open Standards and Free Software....

Single Client, Simple Echo Tcp Server for OPTO22 PAC


Tested with Microsoft WindowsXP Hyper Terminal Application.
Stable, confirmed on: SNAP-PAC-R2, S1 (Rev.8.4a); LCE, ADS, M64 (Rev.7.2h), OPC-SIM.

Deployment/Test Instructions:

1. Download the SimpleOPTO22EchoTcpServer.zip strategy and extract files.
2. Open strategy in PAC Control Proffesional.
3. Configure/Update Control Engine setting and IP address.
4. Compile strategy, load to PAC and click the Run Strategy button.
5. Open extracted Hyper-Terminal 8081.ht file, in properties set IP address of Active Control Engine.
6. Save 8081.ht file, click the "Connect" button. Type "Hello" press the "Enter" key.
//The code below is for reference use only, access the download link for released version.
//
//Single Client, Simple Echo Tcp Server for OPTO22 PACs
//Copyright (C) 2011  Peter Tiagunov peter.tiagunov@leanmes.net

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

// check if communication established
if (not(IsCommunicationOpen(chTcpServer1))) then
    /* ListenForIncomingCommunication(ch)
       0 = Success.
     -10 = Invalid port.
     -36 = Invalid command. Use this command only with a TCP communication handle; for FTP or File communication handles, use Open Outgoing Communication instead.
     -47 = Open failed. Handle has already been opened.
    -203 = Driver not found. */
    nTcpServer1_Sts=ListenForIncomingCommunication(chTcpServer1);
    //if listen port just open or already been opened
    if (nTcpServer1_Sts==0 or nTcpServer1_Sts== -47) then
        /* AcceptIncomingCommunication(ch)
           0 = Success
         -36 = Invalid command. Use this command only with a TCP communication handle; for FTP or File communication handles, use Open Outgoing Communication instead.
         -47 = Open failed. Handle has already been opened.
        -203 = Unknown driver on communication handle.
        -441 = Could not listen on socket.
        -442 = Could not accept on socket. No devices are currenlty attempting to connect on this port. */
        //accept incoming connection from client (ONLY ONE CONNECTION is POSSIBLE!)
        nTcpServer1_Sts=AcceptIncomingCommunication(chTcpServer1);
        if (nTcpServer1_Sts==0 or nTcpServer1_Sts== -47) then
            //Sets the communication timeout value for receiving data,
            //which you must specify in seconds. For example, use 0.5 to set the timeout to 1/2 second.
            //Default is 10 sec. In this implementation, if receive buffer is not empty, and no {CR} character
            //present, then data in buffer forced to receive after time out, e.g. 1.0 sec.
            nTcpServer1_Sts=SendCommunicationHandleCommand(chTcpServer1, "set.to:20.0");
            if(nTcpServer1_Sts==0)then
                SetEndOfMessageTerminator(chTcpServer1,13); //set to {CR} - carriage return
                ClearCommunicationReceiveBuffer(chTcpServer1);
                nTcpServer1_error=5698; //client connected
                nTcpServer1_commsts=1;//communication link established
                nTcpServer_CharsWaiting=0;
            else
                nTcpServer1_error=7724; //Establish Client Connection Error
                nTcpServer1_commsts=0;//no communication link
            endif
        elseif (nTcpServer1_Sts== -442) then
            nTcpServer1_error=6623; //Waiting for Client to Connect
            nTcpServer1_commsts=0;//no communication link
        else
            nTcpServer1_error=7724; //Establish Client Connection Error
            nTcpServer1_commsts=0;//no communication link
        endif
    endif
endif

//communication open, waiting for device to send data
if(IsCommunicationOpen(chTcpServer1)) then
    nTcpServer1_Sts=GetNumCharsWaiting(chTcpServer1); //nr of character(s) before next EOM character (EOM excluded), using FIFO
    //e.g. if data sent to server is: abcd123{CR}efgh456{CR}, then GetNumCharsWaiting=7
  /*Use to determine if the number of characters expected equals the number of characters actually ready to be received.
    If result > 0, there are characters available to be received.
    If result = 0, there are no characters to be received.
    If result < 0, there was an error executing this command. For example, the communication handle may not be opened (use Open Outgoing Communication).
        Queue Errors:
        -36 = Invalid command or feature not implemented. Cannot be used with ftp communication handle.
        -37 = Lock port timeout.
        -39 = Receive timeout.*/
    if (nTcpServer1_Sts>0) then
        /*  ReceiveString(String, ch);
            0 = Success.
          -23 = Destination string too short.
          -36 = Invalid command. Does not apply to the type of communication handle you are using. (For example, Receive commands do not apply to ftp comm handles; instead, use Send Communication Handle Command with the “get” option.)
          -37 = Lock port timeout.
          -39 = Timeout on receive.
          -44 = String too short.
          -52 = Invalid connection. Not opened. The comm handle may have been closed by a previous command that failed. Check status codes returned on other comm handle commands.
          -57 = String not found. No EOM found.
          -58 = No data received. Make sure I/O unit has power.
          -69 = Invalid parameter (null pointer) passed to command. Make sure communication handle is open. */
        // Test if more characters received
        if(nTcpServer_CharsWaiting<nTcpServer1_Sts)then
            // Attempt to receive string
            nTcpServer1_Sts =ReceiveString(sRxTxData, chTcpServer1);
            if(nTcpServer1_Sts==0) then
                ClearCommunicationReceiveBuffer(chTcpServer1); //flush buffer, simplex communication
                nTcpServer1_error=0;
                nTcpServer1_commsts=1;//communication ok
            elseif(nTcpServer1_Sts ==-39)then
                //handle receive timeout
            elseif(nTcpServer1_Sts ==-57)then
                //wait for string(EOM)
            else //Receive Device Data Error
                nTcpServer1_error=7895;
                nTcpServer1_commsts=0;//communication error
            endif
            nTcpServer_CharsWaiting=nTcpServer1_Sts;
        endif
    elseif (nTcpServer1_Sts==0)then //no data received from device
        nTcpServer1_error=0;
        //sRxData="";
        nTcpServer1_commsts=1;//communication ok
    else //GetNumCharsWaiting returned error
        nTcpServer1_error=7896;
        nTcpServer1_commsts=0;//communication error
    endif


    if (nTcpServer1_commsts and GetStringLength(sRxTxData)>0)then //process received data
        sRxTxData="Echo: "+sRxTxData+Chr(10)+Chr(13);
        /* TransmitString(<string>, <commhandle>)
          0 = Success
        -37 = Lock port timeout.
        -38 = Send timeout. For example, attempted to write to a file opened for reading.
        -52 = Invalid connection not opened. The communication handle may have been closed by a previous command that failed.
              Check status codes returned on other communication handle commands.
        -69 = Invalid parameter (null pointer) passed to command.
        -408= Error during file access. For example, attempted to write to a file open for reading.
        -531= Buffer full. You may be attempting to send data to the serial port faster than the port can send and buffer data.
              Try a faster baud rate or a delay between Transfer/Transmit commands.*/
        if(TransmitString(sRxTxData, chTcpServer1)==0)then
            nTcpServer1_error=5699; // Data transmit to client success
            nTcpServer1_commsts=1;//communication link established
        else  // Transmit string error
            nTcpServer1_error=7897;
            nTcpServer1_commsts=0;//communication error
        endif
        sRxTxData="";  
    endif

    //handle error(s)
    if(nTcpServer1_error>=7000)then //hard error
        nTcpServer1_Sts=CloseCommunication(chTcpServer1);
        nTcpServer1_commsts=0;//communication error
    endif


endif

DelayMsec(nTcpServer1_Scan);