#include #include #include #include #include struct scada_point{ char name[35]; char tag[20]; double current_value; double next_value; unsigned char write_to_driver; //set to 1 to write to driver, set to 0 to write to real time DB unsigned int checksum; }; /* Internal scada variables and functions exit_loop : initialized at 0,on stop of driver its value is 1 scada_db : scada vector of struct scada_point items int numSamplePoints; : number of sample point inside scada database int processed_id; : itemID of the processed value void scan_rate(void) : scan rate void lock(void) : lock access of other threads to scada_db void unlock(void) : let other threads to access scada_db void post_value() : post the processed value to real time DB or to driver */ int calc_output_from_one_input(char* name_output, char* tag_output, char* name_input, int processed_id, char* processed_name, char* processed_tag, double delta, unsigned int bit_position); int main(int argc, char **argv) { int itemID; //index of item in database int r; double delta = 0.1; for(itemID = 0; ;) { if(exit_loop) { break; } lock(); processed_id = itemID; //////////Add your code from here///////////////////// //Example code calc_output_from_one_input("CALCpoint01","VALUE", "MDBPoint23", itemID, scada_db[itemID].name, scada_db[itemID].tag, delta, 4); calc_output_from_one_input("CALCpoint02","VALUE", "MDBPoint23", itemID, scada_db[itemID].name, scada_db[itemID].tag, delta, 5); //Uncomment this to see relation between itemID and sample point name e tag name //printf("itemID = %d\n", itemID); //printf("scada_db[%d].name = %s\n", itemID, scada_db[itemID].name); //printf("scada_db[%d].tag = %s\n", itemID, scada_db[itemID].tag); //printf("scada_db[%d].current_value = %lf\n", itemID, scada_db[itemID].current_value); ///////////////////to here////////////////////////////////////////////// unlock(); fflush(stdout); if(itemID == 0) { scan_rate(); } itemID++; if(itemID == numSamplePoints) { itemID = 0; } } return 0; } // Extracting the bit_position bit (0-based index) int calc_output_from_one_input(char* name_output, char* tag_output, char* name_input, int processed_id, char* processed_name, char* processed_tag, double delta, unsigned int bit_position) { int r; int item_id_input; double calculated; double input_value; unsigned int num; unsigned int mask; unsigned int extracted_bit; if((r = strcmp(processed_name, name_output) == 0) && (r = strcmp(processed_tag, tag_output) == 0)) { for(item_id_input = 0; item_id_input < numSamplePoints; item_id_input++) { if(r = strcmp(scada_db[item_id_input].name, name_input) == 0) { printf("scada_db[%d].current_value = %lf\n", processed_id, scada_db[processed_id].current_value); printf("scada_db[%d].current_value = %lf\n", item_id_input, scada_db[item_id_input].current_value); printf("\n"); input_value = scada_db[item_id_input].current_value; //////////Edit here your calculation//////////////////// // C Program to Extract a given bit of a binary number num = input_value; // Create a mask with only the 5th bit set to 1 mask = 1 << bit_position; // Extract the bit using AND and right shift extracted_bit = (num & mask) >> bit_position; // Print the result printf("Extracted Bit: %u\n", extracted_bit); calculated = extracted_bit; if((abs(scada_db[processed_id].current_value - calculated) > delta)) { scada_db[processed_id].write_to_driver = 0; scada_db[processed_id].current_value = calculated; post_value(); } return 1; } } } return 0; }