📄 sp30_drv.c
字号:
i = 0; to_sno = from_rno; if (receive_str[3] == 0x30) { to_rno = (from_sno + 1) % 8; } this_to_sno = to_sno; test_frame[8] = sp30_version == SP30 ? 0x97 : 0x66; test_frame[12] = 0x11; /* test V_R_CAP */ f_formulate(test_frame, to_sno, to_rno, 33); f_sp30_send_frame(port_fd, test_frame, 33); pack_type = BEGIN_PACK; while (!flag && i < max_retrys) { write_msg("i=", (unsigned char *) &i, 2); ret = f_sp30_result_receive(port_fd, receive_str, &receive_len); if (!ret) { write_msg("receive:", receive_str, receive_len); from_sno = receive_str[2] >> 4; from_rno = receive_str[2] & 0x0f; if (receive_str[3] == 0x30 && receive_str[6] == 0x03 && (receive_str[8] == 0x66 || receive_str[8] == 0x97)) { if (pack_type == BEGIN_PACK && receive_str[24] == 0x11 && receive_str[22] == 0x00) { pack_type = END_PACK; memcpy(result_str, receive_str + 26, 12); write_msg("Test V_R_CAP: Get first package!", (unsigned char *) "", 0); write_msg("Pack1:", (unsigned char *) result_str, 12); /*f_send_ack(port_fd); */ to_sno = this_to_sno; to_rno = (from_sno + 1) % 8; f_formulate(test_frame, to_sno, to_rno, 33); f_sp30_send_frame(port_fd, test_frame, 33); } else if (pack_type == END_PACK && receive_str[22] == 0x03) { memcpy(result_str + 12, receive_str + 24, 12); write_msg("Test V_R_CAP OK!", (unsigned char *) "", 0); write_msg("Pack2:", (unsigned char *) (result_str + 12), 12); /* f_send_ack(port_fd); ret=f_sp30_result_receive(port_fd,receive_str,&receive_len); if (!ret) { write_msg("receive:",receive_str,receive_len); from_sno=receive_str[2]>>4; from_rno=receive_str[2]&0x0f; } */ flag = 1; } else { write_msg("The package is not we needed!", (unsigned char *) "", 0); if ((to_sno + 1) % 8 != from_rno) { to_sno = this_to_sno; to_rno = (from_sno + 1) % 8; write_msg("from_rno does not right!", (unsigned char *) "", 0); f_formulate(test_frame, to_sno, to_rno, 33); f_sp30_send_frame(port_fd, test_frame, 33); } else { f_send_ack(port_fd); } } } else { if ((to_sno + 1) % 8 != from_rno) { to_sno = this_to_sno; to_rno = (from_sno + 1) % 8; write_msg("from_rno does not right!", (unsigned char *) "", 0); f_formulate(test_frame, to_sno, to_rno, 33); f_sp30_send_frame(port_fd, test_frame, 33); } else { f_send_ack(port_fd); } } } else { /* f_sp30_send_frame(port_fd,(unsigned char *)"\xff\xfe",2); */ if (ret == 1) /* verify error */ { /*from_rno=receive_str[2]&0x0f; to_rno=from_rno; */ } to_sno = this_to_sno; write_msg("no respone!\nsend test frame!", (unsigned char *) "", 0); f_formulate(test_frame, to_sno, to_rno, 33); f_sp30_send_frame(port_fd, test_frame, 33); } ++i; /*sleep(1); */ } if (i >= max_retrys) { ret = f_sp30_result_receive(port_fd, receive_str, &receive_len); if (!ret) { write_msg("receive:", receive_str, receive_len); from_sno = receive_str[2] >> 4; from_rno = receive_str[2] & 0x0f; } } if (flag) { f_sp30_cal_result(result_str, testmsg); } return flag;}/*********************************************************Function:void f_sp30_stop_test(int port_fd)Narrative: Active switch port by sending wakeup signal.Param: int port_fd - socket idReturn: 0 OK. -1 No expected string. -2 No fd ready when timeout. -3 An error occur when performing poll or read.*********************************************************/void f_sp30_stop_test(int port_fd){ int ret; write_msg("send stop test frame!", (unsigned char *) "", 0); to_sno = from_rno; if (receive_str[3] == 0x30) { to_rno = (from_sno + 1) % 8; } test_frame[8] = sp30_version == SP30 ? 0x97 : 0x66; test_frame[12] = 0x19; f_formulate(test_frame, to_sno, to_rno, 33); f_sp30_send_frame(port_fd, test_frame, 33); ret = f_sp30_result_receive(port_fd, receive_str, &receive_len); if (!ret) { write_msg("receive:", receive_str, receive_len); from_sno = receive_str[2] >> 4; from_rno = receive_str[2] & 0x0f; }}/*********************************************************Function:void f_sp30_release_line(int port_fd, char *phone_number)Narrative: Release accessed line.Param: int port_fd - socket id char *phone_number - a pointer to accessed telephone numberReturn: None.*********************************************************/void f_sp30_release_line(int port_fd, char *phone_number){ int ret; to_sno = from_rno; to_rno = (from_sno + 1) % 8; access_line_frame[8] = sp30_version == SP30 ? 0x96 : 0x64; access_line_frame[12] = 0x00; f_sp30_make_phone_number(access_line_frame, phone_number); f_formulate(access_line_frame, to_sno, to_rno, 43); write_msg("send release line frame!", (unsigned char *) "", 0); f_sp30_send_frame(port_fd, access_line_frame, 43); ret = f_sp30_result_receive(port_fd, receive_str, &receive_len); if (!ret) { write_msg("receive:", receive_str, receive_len); from_sno = receive_str[2] >> 4; from_rno = receive_str[2] & 0x0f; }}/*********************************************************Function:void f_sp30_make_phone_number(unsigned char *str, char *phone_number)Narrative: Generate accessed telephone string to switch.Param: unsigned char *str - a pointer to string to accessed switch char *phone_number - a pointer to telephone numberReturn: None.*********************************************************/void f_sp30_make_phone_number(unsigned char *str, char *phone_number){ int i, j, first; unsigned char temp[20]; unsigned char ch; first = 1; j = 0; memset(temp, 0, 20); for (i = 0; i < strlen(phone_number) && isdigit(phone_number[i]); ++i) { if (!isdigit(phone_number[i])) continue; else { ch = (unsigned char) phone_number[i]; if (ch == '0') ch = 0x0a; else ch -= '0'; if (first) { first = 0; temp[j] = ch; } else { first = 1; temp[j] += ch << 4; ++j; } } } memcpy(str + 22, temp, 4);}/*********************************************************Function:float f_sp30_cal_res(float value)Narrative: Get value of resistance.Param: float value - value of resistance that should be trimedReturn: r Value of rsistance(KOHM).*********************************************************/float f_sp30_cal_res(float value){ float v, r; v = value * 10 / 4096; r = (160 * (30 - v) / v - 380) / 1000; /* change to KOHM */ if (r < 0) r = 0; return (r);}/*********************************************************Function:float f_sp30_cal_cap(float value)Narrative: Get value of capccitance.Param: float value - value of capacitance that should be trimedReturn: v Value of capacitance.*********************************************************/float f_sp30_cal_cap(float value){ float v; v = value * 0.4330 / 79 - 0.21; if (v >= 0.19) v = value * 0.4330 / 80 - 0.21; if (v < 0) v = 0; return v;}/*********************************************************Function:float f_sp30_cal_av(float value)Narrative: Get value of AC voltage.Param: float value - value of AC voltage that should be trimedReturn: Value of AC voltage.*********************************************************/float f_sp30_cal_av(float value){ return (value / 409600L);}/*********************************************************Function:float f_sp30_cal_dv(float value)Narrative: Get value of DC voltage.Param: float value - value of DC voltage that should be trimedReturn: Value of DC voltage.*********************************************************/float f_sp30_cal_dv(float value){ return (value * 21.68 / 409600L);}/*********************************************************Function:void f_sp30_cal_result(unsigned char *result_str, TESTMSG * testmsg)Narrative: Get test result from recieved string.Param: unsigned char *result_str - a pointer to recieved string TESTMSG * testmsg - struct of test messageReturn: None.*********************************************************/void f_sp30_cal_result(unsigned char *result_str, TESTMSG * testmsg){ float value; float v; float result; float r_ab, r_ag, r_bg, c_ab, c_ag, c_bg, av_ab, av_ag, av_bg, dv_ab, dv_ag, dv_bg; unsigned char out_str[256]; /* ab_res */ value = result_str[13] * 65536 + result_str[12];; r_ab = f_sp30_cal_res(value); sprintf(testmsg->TestResult.TR2_DCres, "%9.3f", r_ab); write_msg(testmsg->TestResult.TR2_DCres, receive_str, 0); /* printf("ab_res=%f\n",r_ab); */ /* ag_res */ value = result_str[15] * 65536 + result_str[14];; r_ag = f_sp30_cal_res(value); sprintf(testmsg->TestResult.TG2_DCres, "%9.3f", r_ag); /* printf("ag_res=%f\n",r_ag); */ /* bg_res */ value = result_str[17] * 65536 + result_str[16];; r_bg = f_sp30_cal_res(value); sprintf(testmsg->TestResult.RG2_DCres, "%9.3f", r_bg); /*printf("bg_res=%f\n",r_bg); */ /* ab_cap */ value = result_str[19] * 65536 + result_str[18]; c_ab = f_sp30_cal_cap(value); sprintf(testmsg->TestResult.TR_cap, "%8.3f", c_ab); write_msg(testmsg->TestResult.TR_cap, receive_str, 0); /*printf("ab_cap=%f\n",c_ab); */ /* ag_cap */ value = result_str[21] * 65536 + result_str[20]; c_ag = f_sp30_cal_cap(value); sprintf(testmsg->TestResult.TG_cap, "%8.3f", c_ag); /*printf("ag_cap=%f\n",c_ag); */ /* bg_cap */ value = result_str[23] * 65536 + result_str[22]; c_bg = f_sp30_cal_cap(value); sprintf(testmsg->TestResult.RG_cap, "%8.3f", c_bg); /*printf("bg_cap=%f\n",c_bg); */ /* ab_av TR_ACvol */ value = result_str[1] * 65536 + result_str[0];; av_ab = f_sp30_cal_av(value); /* sprintf(testmsg->TestResult.TR_ACvol,"%8.3f",av_ab); *//* skip */ /*printf("ab_av=%f\n",av_ab); */ /* ag_av */ value = result_str[3] * 65536 + result_str[2];; av_ag = f_sp30_cal_av(value); sprintf(testmsg->TestResult.TG_ACvol, "%8.3f", av_ag); /*printf("ag_av=%f\n",av_ag); */ /* bg_av */ value = result_str[5] * 65536 + result_str[4];; av_bg = f_sp30_cal_av(value); sprintf(testmsg->TestResult.RG_ACvol, "%8.3f", av_bg); /*printf("bg_av=%f\n",av_bg); */ /* ab_dv */ value = result_str[7] * 65536 + result_str[6];; dv_ab = f_sp30_cal_dv(value); /* sprintf(testmsg->TestResult.TR2_DCvol,"%8.3f",dv_ab); *//* skip */ /*printf("ab_dv=%f\n",dv_ab); */ /* ag_dv */ value = result_str[9] * 65536 + result_str[8];; dv_ag = f_sp30_cal_dv(value); sprintf(testmsg->TestResult.TG2_DCvol, "%8.3f", dv_ag); /*printf("ag_dv=%f\n",dv_ag); */ /* bg_dv */ value = result_str[11] * 65536 + result_str[10];; dv_bg = f_sp30_cal_dv(value); sprintf(testmsg->TestResult.RG2_DCvol, "%8.3f", dv_bg); /*printf("bg_dv=%f\n",dv_bg); */}/* ** 函数 f_sp30_correct_package,用于校验接收到的包,正确回0,错误返回1 *//*********************************************************Function:int f_sp30_verify_package(unsigned char *str, int len)Narrative: Verigy if recieved string is correct.Param: unsigned char *str - a pointer to recieved string int len - length of recieved stringReturn: 0 OK. 1 Fail.*********************************************************/int f_sp30_verify_package(unsigned char *str, int len){ unsigned char sum; int i; sum = 0; for (i = 2; i < len - 2; ++i) { sum += str[i]; } sum %= 256; if (str[3] == 0x30) /* Message Frame */ { if (str[4] == len - 9 && sum == 0) return 0; else return 1; } else { if (sum == 0) return 0; else return 1; }}/* 84 85 00 0 *//* 0x48,0x58,0xAA,0x0A *//* Login String: FF,FF,41,30,22,00,00,00,27,00,01,00,00,00,00,00,00,00,00,00,02,00,91,79,00,00,00,00,00,00,00,00, 00,00,00,00,00,00,00,00,39,FF,FE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -