/* */ /* Functions used to check the DVM data as each sample is acquired */ /* */ /* Rev. 17-Dec-2020 */ /* */ /* */ /* First the function to print out in hex the buffer contents */ /* */ int dumpBuffer(unsigned char *buffer, int elements) { int j; printf(" ["); for (j = 0; j < elements; j++) { if (j > 0) printf(", "); printf("0x%02X", buffer[j]); } printf("]\n\n"); return 0; /* This ends the dumpBuffer function */ } /* */ /* Function to extract from the DVM Data Frame */ /* a floating point version of the Voltage */ /* read by the DVM and to print the Voltage. */ /* */ /* The routine returns the floating point */ /* Voltage read by the DVM. */ /* */ double Volts_from_Buffer(unsigned char *buffer) { double f_work_1 = 0.0, f_work_2 = 0.0; f_work_1 = 10.0 * ( 0x0f & buffer[4] ); f_work_2 = 10.0 * (( 0x0f & buffer[3] ) + f_work_1 ); f_work_1 = 10.0 * (( 0x0f & buffer[2] ) + f_work_2 ); f_work_2 = 10.0 * (( 0x0f & buffer[1] ) + f_work_1 ); f_work_1 = (( 0x0f & buffer[0] ) + f_work_2 ); f_work_2 = f_work_1 / 10000.0 ; if ((0x01 & buffer[5]) == 0x01){ f_work_1 = 10.0 + f_work_2; } else{ f_work_1 = f_work_2; } if ((0x04 & buffer[5]) == 0x04){ f_work_2 = -1.0 * f_work_1; } else{ f_work_2 = f_work_1; } printf("\n "); printf("Volts floating = %7.4f", f_work_2); printf(" \n\n"); return f_work_2; /* This ends the Volts_from_Buffer function */ } /* Function to verify that the current 8 byte DVM readout data frame is error free. This function returns a zero if there are no errors and it returns a one if any DVM Data Frame error is found. The tests made are: - All 8 bytes have 0x1 in the high order nibble, i.e. the Data Flag is set Hi in all 8 bytes. - The first 5 bytes contain only BCD data, i.e. they are <= 0x9 in the low order nibble. - The low order nibble of the 6th byte must be either: 0x0 --> Polarity = Low (positive), Over Range = Low 0x1 --> Polarity = Low (positive), Over Range = Hi 0x4 --> Polarity = Hi (negative), Over Range = Low 0x5 --> Polarity = Hi (negative), Over Range = Hi In all cases Bit D (Over Load) must be Low and Bit B (Gnd) must be Low. - The low order nibble of the 7th byte must be 0xC, i.e. Not S/H Mode, 10 Volt Range, Range Bit C = Hi, Range Bits B and A = Low. - The low order nibble of the 8th byte must be 0xC, i.e. Not Remote Mode, Not Ratio Mode, DC Volts Function Bits B and A = Low. */ int Check_DVM_Frame(unsigned char *buffer) { int error_flag = 0; /* Verify that the Data Flag is Hi in all 8 bytes of the frame. */ if (((0x10 & buffer[0]) == 0x10) && ((0x10 & buffer[1]) == 0x10) && ((0x10 & buffer[2]) == 0x10) && ((0x10 & buffer[3]) == 0x10) && ((0x10 & buffer[4]) == 0x10) && ((0x10 & buffer[5]) == 0x10) && ((0x10 & buffer[6]) == 0x10) && ((0x10 & buffer[7]) == 0x10)){ printf(" All 8 Data Flags are Hi. \n"); } else{ printf(" At least one Data Flag is Low. \n\n"); error_flag = 1; } /* Verify that the first 5 bytes contain only BCD data. */ if (((0x0f & buffer[0]) <= 0x09) && ((0x0f & buffer[1]) <= 0x09) && ((0x0f & buffer[2]) <= 0x09) && ((0x0f & buffer[3]) <= 0x09) && ((0x0f & buffer[4]) <= 0x09)){ printf(" The first 5 bytes are all BCD. \n"); } else{ printf("\n At least one of the first 5 bytes is Not BCD. \n\n"); error_flag = 1; } /* Verify that the low order nibble of the 6th byte is either: 0x0, 0x1, 0x4, or 0x5 i.e. in all cases bits D and B must be Low. */ if (((0x08 & buffer[5]) == 0x00) && ((0x02 & buffer[5]) <= 0x00)){ printf(" The 6th byte is one of the 4 legal values. \n"); } else{ printf("\n The 6th byte is NOT one of the 4 legal values. \n\n"); error_flag = 1; } /* Verify that the low order nibble of the 7th byte is 0xC, i.e. the correct DVM Scale Range and Not S/H Mode. */ if ((0x0f & buffer[6]) == 0x0c){ printf(" The 7th byte is the correct value. \n"); } else{ printf("\n The 7th byte is NOT the correct value. \n\n"); error_flag = 1; } /* Verify that the low order nibble of the 8th byte is 0xC, i.e. the correct DC Volts Function and NOT Ratio or Remote. */ if ((0x0f & buffer[7]) == 0x0c){ printf(" The 8th byte is the correct value. \n"); } else{ printf("\n The 8th byte is NOT the correct value. \n\n"); error_flag = 1; } if (error_flag != 0) { printf("\07"); } return error_flag; /* This ends the Check_DVM_Frame function */ }