Mohamed M. Fathy Ahmed Abdelsalam
Published

Mbed Soft PLC I/O Server

A soft PLC I/O card for PC-based automation commercial applications like Wonderware InControl.

IntermediateFull instructions provided5 hours890
Mbed Soft PLC I/O Server

Things used in this project

Hardware components

Cortex-M33
Arm Cortex-M33
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

Block Diagram for the proejct

Code

Mbed Application Code

C/C++
// MBED I/O Server FOR PC Based Control



#include "mbed.h"
#include "EthernetNetIf.h"
#include "TCPSocket.h"

DigitalOut led4(LED4, "led4");

// Declaring Digital outputs Pins
DigitalOut do1(p30); 
DigitalOut do2(p29);
DigitalOut do3(p28);
DigitalOut do4(p27);
DigitalOut do5(p26);
DigitalOut do6(p25);
DigitalOut do7(p24);
DigitalOut do8(p23);
DigitalOut do9(p22);
DigitalOut do10(p21);
// Declaring Digital Inptus Pins
DigitalIn di1(p5);
DigitalIn di2(p6);
DigitalIn di3(p7);
DigitalIn di4(p8);
DigitalIn di5(p9);
DigitalIn di6(p10);
DigitalIn di7(p11);
DigitalIn di8(p12);
DigitalIn di9(p13);
DigitalIn di10(p14);
DigitalIn di11(p15);
DigitalIn di12(p16);
DigitalIn di13(p17);
DigitalIn di14(p18);
DigitalIn di15(p19);
DigitalIn di16(p20);

char x[10],y[16];

// EthernetNetIf eth;
EthernetNetIf eth(
  IpAddr(192,168,1,25), //IP Address
  IpAddr(255,255,255,0), //Network Mask
  IpAddr(192,168,1,1), //Gateway
  IpAddr(192,168,1,1)  //DNS
);

#define TCP_LISTENING_PORT 12345

TCPSocket ListeningSock;
TCPSocket* pConnectedSock; // for ConnectedSock
Host client;
TCPSocketErr err;

void onConnectedTCPSocketEvent(TCPSocketEvent e)
{
   switch(e)
    {
    case TCPSOCKET_CONNECTED:
        printf("TCP Socket Connected\r\n");
        break;
    case TCPSOCKET_WRITEABLE:
      //Can now write some data...
        printf("TCP Socket Writable\r\n");
        break;
    case TCPSOCKET_READABLE:
      //Can now read dome data...
        printf("TCP Socket Readable\r\n");
       // Read in any available data into the buffer
      
       //Parsing for the messege from I/O Server
       //1)Updating thr output status
       while ( pConnectedSock->recv(x, 10) ) {
       if(x[0]==0)
       {
             do1=0;
       }
       if(x[0]==1)
       {
        
        do1=1;
       }
       if(x[1]==0)
       {
        
        do2=0;
       }
       if(x[1]==1)
       {
        
        do2=1;
       }
       if(x[2]==0)
       {
        
        do3=0;
       }
       if(x[2]==1)
       {
        
        do3=1;
       }
       if(x[3]==0)
       {
       do4=0;
       }
       if(x[3]==1)
       {
       do4=1;
       }
       if(x[4]==0)
       {
       do5=0;
       }
       if(x[4]==1)
       {
       do5=1;
       }
       if(x[5]==0)
       {
       do6=0;
       }
       if(x[5]==1)
       {
       do6=1;
       }
       if(x[6]==0)
       {
       do7=0;
       }
       if(x[6]==1)
       {
       do7=1;
       }
       if(x[7]==0)
       {
       do8=0;
       }
       if(x[7]==1)
       {
       do8=1;
       }
       if(x[8]==0)
       {
       do9=0;
       }
       if(x[8]==1)
       {
       do9=1;
       }
       if(x[9]==0)
       {
       do10=0;
       }
       if(x[9]==1)
       {
       do10=1;
       }
       
      
      
// 2) send the inputs status to I/O Server
if (di1==1)
{
y[0]=1;
}
if (di1==0)
{
y[0]=0;
}
if (di2==1)
{
y[1]=1;
}
if (di2==0)
{
y[1]=0;
}
if (di3==1)
{
y[2]=1;
}
if (di3==0)
{
y[2]=0;
}
if (di4==1)
{
y[3]=1;
}
if (di4==0)
{
y[3]=0;
}
if (di1==5)
{
y[4]=1;
}
if (di5==0)
{
y[4]=0;
}
if (di6==1)
{
y[5]=1;
}
if (di6==0)
{
y[5]=0;
}
if (di7==1)
{
y[6]=1;
}
if (di7==0)
{
y[6]=0;
}
if (di8==1)
{
y[7]=1;
}
if (di8==0)
{
y[7]=0;
}
if (di9==1)
{
y[8]=1;
}
if (di9==0)
{
y[8]=0;
}
if (di10==1)
{
y[9]=1;
}
if (di10==0)
{
y[9]=0;
}
if (di11==1)
{
y[10]=1;
}
if (di11==0)
{
y[10]=0;
}
if (di12==1)
{
y[11]=1;
}
if (di12==0)
{
y[11]=0;
}
if (di13==1)
{
y[12]=1;
}
if (di13==0)
{
y[12]=0;
}
if (di14==1)
{
y[13]=1;
}
if (di14==0)
{
y[13]=0;
}
if (di15==1)
{
y[14]=1;
}
if (di15==0)
{
y[14]=0;
}
if (di16==1)
{
y[15]=1;
}
if (di16==0)
{
y[15]=0;
}
     
          
           pConnectedSock->send(y, 16);
            // make terminater
         
       }
       break;
    case TCPSOCKET_CONTIMEOUT:
        printf("TCP Socket Timeout\r\n");
        break;
    case TCPSOCKET_CONRST:
        printf("TCP Socket CONRST\r\n");
        break;
    case TCPSOCKET_CONABRT:
        printf("TCP Socket CONABRT\r\n");
        break;
    case TCPSOCKET_ERROR:
        printf("TCP Socket Error\r\n");
        break;
    case TCPSOCKET_DISCONNECTED:
    //Close socket...
        printf("TCP Socket Disconnected\r\n");        
        pConnectedSock->close();
        break;
    default:
        printf("DEFAULT\r\n"); 
      }
}


void onListeningTCPSocketEvent(TCPSocketEvent e)
{
    switch(e)
    {
    case TCPSOCKET_ACCEPT:
        printf("Listening: TCP Socket Accepted\r\n");
        // Accepts connection from client and gets connected socket.   
        err=ListeningSock.accept(&client, &pConnectedSock);
        if (err) {
            printf("onListeningTcpSocketEvent : Could not accept connection.\r\n");
            return; //Error in accept, discard connection
        }
        // Setup the new socket events
        pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent);
        // We can find out from where the connection is coming by looking at the
        // Host parameter of the accept() method
        IpAddr clientIp = client.getIp();
        printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", 
           clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
       break;
    // the following cases will not happen
    case TCPSOCKET_CONNECTED:
        printf("Listening: TCP Socket Connected\r\n");
        break;
    case TCPSOCKET_WRITEABLE:
        printf("Listening: TCP Socket Writable\r\n");
        break;
    case TCPSOCKET_READABLE:
        printf("Listening: TCP Socket Readable\r\n");
        break;
    case TCPSOCKET_CONTIMEOUT:
        printf("Listening: TCP Socket Timeout\r\n");
        break;
    case TCPSOCKET_CONRST:
        printf("Listening: TCP Socket CONRST\r\n");
        break;
    case TCPSOCKET_CONABRT:
        printf("Listening: TCP Socket CONABRT\r\n");
        break;
    case TCPSOCKET_ERROR:
        printf("Listening: TCP Socket Error\r\n");
        break;
    case TCPSOCKET_DISCONNECTED:
    //Close socket...
        printf("Listening: TCP Socket Disconnected\r\n");        
        ListeningSock.close();
        break;
    default:
        printf("DEFAULT\r\n"); 
     };
}


int main() {
  printf("\r\n");
  printf("Setting up...\r\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\r\n", ethErr);
    return -1;
  }
  printf("Setup OK\r\n");

  IpAddr ip = eth.getIp();
  printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);

  
  // Set the callbacks for Listening
  ListeningSock.setOnEvent(&onListeningTCPSocketEvent); 
  
  // bind and listen on TCP
  err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT));
  printf("Binding..\r\n");
  if(err)
  {
   //Deal with that error...
    printf("Binding Error\n");
  }
   err=ListeningSock.listen(); // Starts listening
   printf("Listening...\r\n");
   if(err)
   {
    printf("Listening Error\r\n");
   }
 
  Timer tmr;
  tmr.start();

  while(true)
  {
    Net::poll();
    if(tmr.read() > 0.2) // sec
    {
      led4=!led4; //Show that we are alive
      tmr.reset();
    }
  }  
}

PC I/O DDE Server

C/C++
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
char x[10],y[16]   ;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------




void __fastcall TForm1::DI1Change(TObject *Sender)
{
if (StrToInt(DI1->Text)==1)
 {
 DII1->Brush->Color=clLime;
}
if (StrToInt(DI1->Text)==0)
 {
 DII1->Brush->Color=clOlive;
}        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DI2Change(TObject *Sender)
{
if (StrToInt(DI2->Text)==1)
 {
 DII2->Brush->Color=clLime;
}
if (StrToInt(DI2->Text)==0)
 {
 DII2->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI3Change(TObject *Sender)
{
if (StrToInt(DI3->Text)==1)
 {
 DII3->Brush->Color=clLime;
}
if (StrToInt(DI3->Text)==0)
 {
 DII3->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI4Change(TObject *Sender)
{
if (StrToInt(DI4->Text)==1)
 {
 DII4->Brush->Color=clLime;
}
if (StrToInt(DI4->Text)==0)
 {
 DII4->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI5Change(TObject *Sender)
{
if (StrToInt(DI5->Text)==1)
 {
 DII5->Brush->Color=clLime;
}
if (StrToInt(DI5->Text)==0)
 {
 DII5->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI6Change(TObject *Sender)
{
if (StrToInt(DI6->Text)==1)
 {
 DII6->Brush->Color=clLime;
}
if (StrToInt(DI6->Text)==0)
 {
 DII6->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI7Change(TObject *Sender)
{
if (StrToInt(DI7->Text)==1)
 {
 DII7->Brush->Color=clLime;
}
if (StrToInt(DI7->Text)==0)
 {
 DII7->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI8Change(TObject *Sender)
{
if (StrToInt(DI8->Text)==1)
 {
 DII8->Brush->Color=clLime;
}
if (StrToInt(DI8->Text)==0)
 {
 DII8->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI9Change(TObject *Sender)
{
if (StrToInt(DI9->Text)==1)
 {
 DII9->Brush->Color=clLime;
}
if (StrToInt(DI9->Text)==0)
 {
 DII9->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DI10Change(TObject *Sender)
{
if (StrToInt(DI10->Text)==1)
 {
 DII10->Brush->Color=clLime;
}
if (StrToInt(DI10->Text)==0)
 {
 DII10->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DO1Change(TObject *Sender)
{
if (StrToInt(DO1->Text)==1)
 {
 DOI1->Brush->Color=clLime;
 x[0]=1;
}
if (StrToInt(DO1->Text)==0)
 {
 DOI1->Brush->Color=clOlive;
 x[0]=0;
}        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DO2Change(TObject *Sender)
{
if (StrToInt(DO2->Text)==1)
 {
 DOI2->Brush->Color=clLime;
 x[1]=1;
}
if (StrToInt(DO2->Text)==0)
 {
 DOI2->Brush->Color=clOlive;
 x[1]= 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DO3Change(TObject *Sender)
{
if (StrToInt(DO3->Text)==1)
 {
 DOI3->Brush->Color=clLime;
 x[2]=1;
}
if (StrToInt(DO3->Text)==0)
 {
 DOI3->Brush->Color=clOlive;
 x[2]=0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DO4Change(TObject *Sender)
{
if (StrToInt(DO4->Text)==1)
 {
 DOI4->Brush->Color=clLime;
 x[3]=1;
}
if (StrToInt(DO4->Text)==0)
 {
 DOI4->Brush->Color=clOlive;
 x[3]=0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
con->Host=ip->Text;
con->Port=StrToInt(port->Text);
con->Open();
t->Enabled=true;
Button1->Enabled=false;
Button2->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
con->Close();
t->Enabled=false;
Button2->Enabled=false;
Button1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::conRead(TObject *Sender, TCustomWinSocket *Socket)
{
con->Socket->ReceiveBuf(y,16);
DI1->Text=IntToStr(y[0]);
DI2->Text=IntToStr(y[1]);
DI3->Text=IntToStr(y[2]);
DI4->Text=IntToStr(y[3]);
DI5->Text=IntToStr(y[4]);
DI6->Text=IntToStr(y[5]);
DI7->Text=IntToStr(y[6]);
DI8->Text=IntToStr(y[7]);
DI9->Text=IntToStr(y[8]);
DI10->Text=IntToStr(y[9]);
DI11->Text=IntToStr(y[10]);
DI12->Text=IntToStr(y[11]);
DI13->Text=IntToStr(y[12]);
DI14->Text=IntToStr(y[13]);
DI15->Text=IntToStr(y[14]);
DI16->Text=IntToStr(y[15]);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::tTimer(TObject *Sender)
{

con->Socket->SendBuf(x,10) ;        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DO10Change(TObject *Sender)
{
if (StrToInt(DO10->Text)==1)
 {
 DOI10->Brush->Color=clLime;
 x[9]=1;
}
if (StrToInt(DO10->Text)==0)
 {
 DOI10->Brush->Color=clOlive;
 x[9]=0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DO9Change(TObject *Sender)
{
if (StrToInt(DO9->Text)==1)
 {
 DOI9->Brush->Color=clLime;
 x[8]=1;
}
if (StrToInt(DO9->Text)==0)
 {
 DOI9->Brush->Color=clOlive;
 x[8]=0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DO8Change(TObject *Sender)
{
if (StrToInt(DO8->Text)==1)
 {
 DOI8->Brush->Color=clLime;
 x[7]=1;
}
if (StrToInt(DO8->Text)==0)
 {
 DOI8->Brush->Color=clOlive;
 x[7]=0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DO7Change(TObject *Sender)
{
if (StrToInt(DO7->Text)==1)
 {
 DOI7->Brush->Color=clLime;
 x[6]=1;
}
if (StrToInt(DO7->Text)==0)
 {
 DOI7->Brush->Color=clOlive;
 x[6]=0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DO6Change(TObject *Sender)
{
if (StrToInt(DO6->Text)==1)
 {
 DOI6->Brush->Color=clLime;
 x[5]=1;
}
if (StrToInt(DO6->Text)==0)
 {
 DOI6->Brush->Color=clOlive;
 x[5]=0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DO5Change(TObject *Sender)
{
if (StrToInt(DO5->Text)==1)
 {
 DOI5->Brush->Color=clLime;
 x[4]=1;
}
if (StrToInt(DO5->Text)==0)
 {
 DOI5->Brush->Color=clOlive;
 x[4]=0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DI11Change(TObject *Sender)
{
if (StrToInt(DI11->Text)==1)
 {
 DII11->Brush->Color=clLime;
}
if (StrToInt(DI11->Text)==0)
 {
 DII11->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DI16Change(TObject *Sender)
{
if (StrToInt(DI16->Text)==1)
 {
 DII16->Brush->Color=clLime;
}
if (StrToInt(DI16->Text)==0)
 {
 DII16->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DI15Change(TObject *Sender)
{
if (StrToInt(DI15->Text)==1)
 {
 DII15->Brush->Color=clLime;
}
if (StrToInt(DI15->Text)==0)
 {
 DII15->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DI14Change(TObject *Sender)
{
if (StrToInt(DI14->Text)==1)
 {
 DII14->Brush->Color=clLime;
}
if (StrToInt(DI14->Text)==0)
 {
 DII14->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DI13Change(TObject *Sender)
{
if (StrToInt(DI13->Text)==1)
 {
 DII13->Brush->Color=clLime;
}
if (StrToInt(DI13->Text)==0)
 {
 DII13->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DI12Change(TObject *Sender)
{
if (StrToInt(DI12->Text)==1)
 {
 DII12->Brush->Color=clLime;
}
if (StrToInt(DI12->Text)==0)
 {
 DII12->Brush->Color=clOlive;
}
}
//---------------------------------------------------------------------------

Credits

Mohamed M. Fathy Ahmed Abdelsalam
8 projects • 22 followers
Contact

Comments

Please log in or sign up to comment.