Introduction to C programming for SCADA monitoring systems

By Youssef Edward
July 25, 2019


SCADA monitoring refers to collecting the status of certain
indicators and then sends them to a central station. Those indicators may
represent the status of line voltages, the status of currents out of the
station, the degree of temperature at the current station, etc.

Writing c program for a SCADA system is not difficult
provided you understand the full architecture of the system and how to manage
the individual modules. As an example, here is a code representing the variables
in our system :

unsigned short  va[2];
unsigned short  vb[2]; unsigned
short  vc[2];

unsigned short  ia[2];
unsigned short  ib[2]; unsigned
short  ic[2];

unsigned short
vca[2]; unsigned short  vab[2];
unsigned short  vbc[2];

unsigned short  pa[2];
unsigned short  pb[2]; unsigned
short  pc[2];

unsigned short  qa[2];
unsigned short  qb[2]; unsigned
short  qc[2];

unsigned short
pfa[2]; unsigned short  pfb[2]; unsigned
short  pfc[2];

in the above code, VA,VB,VC refers to the voltages of each
phase with respect to the neutral in the station. It is described as an array
because there is two cells in the station. Each cell is outputting power to the
outside. If one cell is down, the second will take place.

IA,IB,IC are the currents originating from the cell. If those
current are greater than the upper limit, then the SCADA system must generate
alarm to the server.

Vca,Vab,Vbc are the voltages between two phase in the cell.

Pa ,Pb,Pc are the dissipative power load of each phase.

Qa,Qb,Qc are the  non-dissipative
power load for each phase.

PFa,PFb,PFc are the power factors for each phase.

You may refere to Three phase Electric
Power 
 for more information on those
variables. You must understand them well before going in the SCADA system for
power stations.

The above variables must be sent to the station on regular
basis. The minimum time that the system can handle identifies the quality of
the SCADA system. This is because a fault may occur that is real time based. In
this case the monitoring system must discover it in timely manner. The lower the
interval of the SCADA system, the better the quality of the system.

The following is a sample routine that read
Va from the DTU and then send the values to the modem:

void get_va()

{

unsigned short y=0;   unsigned int x=0;

unsigned short
index=0;

for(y=0;y<32;y++)

{

bytes[y]=50;

}

portc.f6=1;

enabletx();

delay_us(5);

index=0;

x=0;

sendbyte_485(2);
delay_us(1000);

sendbyte_485(3);
delay_us(1000);

sendbyte_485(0);  delay_us(1000);

sendbyte_485(0);  delay_us(1000);

sendbyte_485(0);  delay_us(1000);

sendbyte_485(1);  delay_us(1000);

sendbyte_485(132);
delay_us(1000);

sendbyte_485(57);     delay_us(100);

portc.f6=0;

enablerx();

while(1)

{

if(portc.f3==0)

{

y=receivebyte_485();

bytes[index]=y;

index++;

x=0;

}

x++;

if(x==65000) break;

}

bytes[0]=0;

senddta();

}

In the above routine, the SCADA CPU sends
485 commands to the DTU informing it to send the values of voltages at the current
time. Then it polls until get the results. Once the results are available, they
are sent to the modem, which in turn will be sent to the remote server.

The following routine is an example to reaf
the current of the phase number A and
then send it to the server :

void get_ia()

{

unsigned short y=0;   unsigned int x=0;

unsigned short
index=0;

for(y=0;y<32;y++)

{

bytes[y]=50;

}

portc.f6=1;

enabletx();

delay_us(5);

index=0;

x=0;

sendbyte_485(2);
delay_us(1000);

sendbyte_485(3);
delay_us(1000);

sendbyte_485(0);  delay_us(1000);

sendbyte_485(4);  delay_us(1000);

sendbyte_485(0);  delay_us(1000);

sendbyte_485(1);  delay_us(1000);

sendbyte_485(197);
delay_us(1000);

sendbyte_485(248);
delay_us(100);

portc.f6=0;

enablerx();

while(1)

{

if(portc.f3==0)

{

y=receivebyte_485();

bytes[index]=y;

index++;

x=0;

}

x++;

if(x==65000) break;

}

bytes[0]=3;

sendcipsend();

}

The following routine is a polling one that
continuously sends all the data on the system

while(1)

{

sendalive();

delay_ms(500);

get_di4_ch_1_4();

delay_ms(500);

get_vab();

delay_ms(500);

get_vbc();

delay_ms(500);

get_vca();

delay_ms(500);

get_di4_ch_1_4();

delay_ms(500);

get_ia();

delay_ms(500);

get_ib();

delay_ms(500);

get_ic();

delay_ms(500);

}

The above routine first
sends a live message informing the server it is connected. If the alive message
is not sent within a certain time, the server will end the session between it
and the client. In this case, the client must initiate again a connection to be
able to send data to the server.

The remaining steps are sending the data
collected from the Data terminal unit. Note that the delay of 500 MS is the
amount of time between sending the current and the next data.

Leave a Reply

Your email address will not be published. Required fields are marked *