1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261 |
- /*
- * internalComm.c
- *
- * Created on: 2019年5月7日
- * Author: foluswen
- */
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/wireless.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <unistd.h> /*Unix 標準函數定義*/
- #include <fcntl.h> /*檔控制定義*/
- #include <termios.h> /*PPSIX 終端控制定義*/
- #include <errno.h> /*錯誤號定義*/
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- #include <math.h>
- #include "internalComm.h"
- #define PASS 1
- #define FAIL -1
- #define RS485_INFO(format, args...) StoreInternalCommLog("[%s:%4d][%s][RS485] "format, (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__), __LINE__, __FUNCTION__, ##args)
- struct Address Addr = {
- AUX_ADD,
- FAN_ADD,
- RELAY_ADD,
- ACPLUG_ADD,
- LED_ADD,
- RC1_ADD,
- RC2_ADD,
- RD_ADD,
- BROADCAST_ADD
- };
- struct Command Cmd = {
- MSGID_G_FW_VERSION,
- MSGID_G_HW_VERSION,
- MSGID_G_AC_INPUT_VOLTAGE,
- MSGID_G_DC_OUTPUT_VOLTAGE,
- MSGID_G_FAN_SPEED,
- MSGID_G_TEMPERATURE,
- MSGID_G_AUX_VOLTAGE,
- MSGID_G_RELAY_STATE,
- MSGID_G_GFD,
- MSGID_G_GPIO_INPUT,
- MSGID_G_MODEL_NAME,
- MSGID_G_AC_OUTPUT_CURRENT,
- MSGID_G_AC_STATUS,
- MSGID_G_AC_ALARM,
- MSGID_G_AC_OUTPUT_POWER,
- MSGID_G_DC_INPUT_VOLTAGE,
- MSGID_S_FAN_SPEED,
- MSGID_S_MODEL_NAME,
- MSGID_S_RELAY_STATE,
- MSGID_S_GPIO_OUTPUT,
- MSGID_S_RTC,
- MSGID_S_AC_LED_STATE,
- MSGID_S_AC_CP_DUTY,
- MSGID_S_AC_LEGACY_REQ,
- MSGID_S_GFD,
- MSGID_S_AC_RESET_MCU,
- MSGID_S_MCU_OPERATION,
- MSGID_S_LED_COLOR,
- MSGID_S_FW_CHECKSUM,
- MSGID_S_FW_ABORT,
- MSGID_S_FW_PACKET,
- MSGID_S_FW_FINISH
- };
- int StoreInternalCommLog(const char *fmt, ...)
- {
- char Buf[4096+256];
- char buffer[4096];
- va_list args;
- struct timeb SeqEndTime;
- struct tm *tm;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf,0,sizeof(Buf));
- ftime(&SeqEndTime);
- SeqEndTime.time = time(NULL);
- tm=localtime(&SeqEndTime.time);
- sprintf(Buf,"echo \'%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\' >> /Storage/SystemLog/[%04d.%02d]InternalCommLog",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1);
- system(Buf);
- return rc;
- }
- int tranceiveRelDelayTime(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx, unsigned short _delay, int debug)
- {
- int len;
- char buffer[1024];
- char temp[32];
- tcflush(fd,TCIOFLUSH);
- if(write(fd, cmd, cmd_len) >= cmd_len)
- {
- if(debug > 0)
- {
- memset(buffer, 0x00, sizeof(buffer));
- sprintf(buffer, "Tx->");
- for(int i = 0; i < cmd_len; i++)
- {
- sprintf(temp, " %02X", cmd[i]);
- strcat(buffer, temp);
- }
- RS485_INFO("%s", buffer);
- }
- usleep(_delay * 1000);
- len = read(fd, rx, 512);
- if(debug > 0)
- {
- memset(buffer, 0x00, sizeof(buffer));
- sprintf(buffer, "Rx<-");
- if(len > 0)
- {
- for(int i = 0; i < len; i++)
- {
- sprintf(temp, " %02X", rx[i]);
- strcat(buffer, temp);
- }
- }
- else
- {
- strcat(buffer, " No Data!");
- }
- RS485_INFO("%s", buffer);
- }
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("Serial command %s response fail.\n", cmd);
- #endif
- }
- return len;
- }
- int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx, int debug)
- {
- int len;
- char buffer[1024];
- char temp[32];
- tcflush(fd,TCIOFLUSH);
- if(write(fd, cmd, cmd_len) >= cmd_len)
- {
- if(debug > 0)
- {
- memset(buffer, 0x00, sizeof(buffer));
- sprintf(buffer, "Tx->");
- for(int i = 0; i < cmd_len; i++)
- {
- sprintf(temp, " %02X", cmd[i]);
- strcat(buffer, temp);
- }
- RS485_INFO("%s", buffer);
- }
- usleep(15000);
- len = read(fd, rx, 512);
- if(debug > 0)
- {
- memset(buffer, 0x00, sizeof(buffer));
- sprintf(buffer, "Rx<-");
- if(len > 0)
- {
- for(int i = 0; i < len; i++)
- {
- sprintf(temp, " %02X", rx[i]);
- strcat(buffer, temp);
- }
- }
- else
- {
- strcat(buffer, " No Data!");
- }
- RS485_INFO("%s", buffer);
- }
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("Serial command %s response fail.\n", cmd);
- #endif
- }
- return len;
- }
- int Internal_Comm_Request(unsigned char fd, unsigned char targetAddr, unsigned char msg, unsigned char *data, unsigned short dataLen, unsigned char *response, int debug)
- {
- int resLen = 0, rxLen = 0;
- unsigned char tx[512], rx[512];
- unsigned char txChksum = 0x00, rxChksum = 0;
- tx[0] = FRAME_ID;
- tx[1] = CSU_ADD;
- tx[2] = targetAddr;
- tx[3] = msg;
- tx[4] = dataLen & 0xFF;
- tx[5] = (dataLen >> 8) & 0xFF;
- if(dataLen > 0)
- {
- memcpy(&tx[6], &data[0], dataLen);
- for(int i = 0; i < dataLen; i++)
- {
- txChksum ^= tx[6 + i];
- }
- }
- tx[6 + dataLen] = txChksum;
- rxLen = tranceive(fd, &tx[0], INTERNAL_PACKET_MIN_LEN + dataLen, &rx[0], debug);
- if(rxLen >= INTERNAL_PACKET_MIN_LEN)
- {
- resLen = (rx[4] | (rx[5] << 8));
- if(rxLen == (INTERNAL_PACKET_MIN_LEN + resLen))
- {
- for(int i = 0; i < resLen; i++)
- {
- rxChksum ^= rx[6 + i];
- }
- if(rxChksum == rx[INTERNAL_PACKET_MIN_LEN - 1 + resLen])
- {
- if(rx[2] == tx[1] && rx[1] == tx[2] && rx[3] == tx[3])
- {
- if(resLen > 0)
- {
- memcpy(response, &rx[6], resLen);
- }
- #if 0
- printf("\r\n");
- for(int i = 0; i < rxLen; i++)
- {
- printf(" %02X", rx[i]);
- }
- #endif
- }
- else
- {
- // response packet(address or message) is not match
- resLen = -1;
- }
- }
- else
- {
- // checksum is not match
- resLen = -1;
- }
- }
- else
- {
- // rx packet length and data length is not match
- resLen = -1;
- }
- }
- else
- {
- // rx packet length is less than INTERNAL_PACKET_MIN_LEN
- resLen = -1;
- }
- return resLen;
- }
- unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_FW_Ver, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- // for (int i = 0; i < 7; i++)
- // printf("tx = %x \n", tx[i]);
- // for (int i = 0; i < len; i++)
- // printf("rx = %x \n", rx[i]);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]))
- {
- memcpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
- *(Ret_Buf->Version_FW + 8) = 0x00;
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_FW_Ver, 0, 0, &response[0], debug);
- if(resLen >= 0)
- {
- int maxLen = sizeof(Ret_Buf->Version_FW) - 1;
- strncpy((char *)Ret_Buf->Version_FW, (char *)response, resLen > maxLen ? maxLen : resLen);
- Ret_Buf->Version_FW[maxLen] = 0;
- result = PASS;
- }
- return result;
- }
- unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_HW_Ver, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]))
- {
- memcpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
- *(Ret_Buf->Version_HW + 8) = 0x00;
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_HW_Ver, 0, 0, &response[0], debug);
- if(resLen >= 0)
- {
- int maxLen = sizeof(Ret_Buf->Version_FW) - 1;
- strncpy((char *)Ret_Buf->Version_HW, (char *)response, resLen > maxLen ? maxLen : resLen);
- Ret_Buf->Version_HW[maxLen] = 0;
- result = PASS;
- }
- return result;
- }
- unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_InputVoltage, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 13)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- chksum != 0)
- {
- Ret_Buf->inputType = rx[6];
- Ret_Buf->L1N_L12 =(rx[7] | (rx[8]<<8))/10.0;
- Ret_Buf->L2N_L23 =(rx[9] | (rx[10]<<8))/10.0;
- Ret_Buf->L3N_L31 =(rx[11] | (rx[12]<<8))/10.0;
- if (Ret_Buf->L1N_L12 >= 320 ||
- Ret_Buf->L2N_L23 >= 320 ||
- Ret_Buf->L3N_L31 >= 320)
- {
- result = FAIL;
- }
- else
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_Present_InputVoltage, 0, 0, &response[0], debug);
- if(resLen >= 7)
- {
- Ret_Buf->inputType = response[0];
- Ret_Buf->L1N_L12 =(response[1] | (response[2] << 8)) / 10.0;
- Ret_Buf->L2N_L23 =(response[3] | (response[4] << 8)) / 10.0;
- Ret_Buf->L3N_L31 =(response[5] | (response[6] << 8)) / 10.0;
- if (Ret_Buf->L1N_L12 >= 320 || Ret_Buf->L2N_L23 >= 320 || Ret_Buf->L3N_L31 >= 320)
- {
- result = FAIL;
- }
- else
- {
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Query_DC_InputVoltage(unsigned char fd, unsigned char targetAddr, DCInputVoltage *Ret_Buf, int debug)
- {
- unsigned char result = FAIL;
- int resLen = 0;
- unsigned char response[512];
- memset(response, 0x00, 512);
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_DC_InputVoltage, 0, 0, &response[0], debug);
- if(resLen >= 0)
- {
- Ret_Buf->DC_Input_1 =(response[0] | (response[1] << 8)) / 10;
- Ret_Buf->DC_Input_2 =(response[2] | (response[3] << 8)) / 10;
- result = PASS;
- }
- return result;
- }
- unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Present_OutputVoltage, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]))
- {
- Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8));
- Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8));
- if((rx[4] | rx[5]<<8) > 4)
- {
- Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8));
- Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8));
- }
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_Present_OutputVoltage, 0, 0, &response[0], debug);
- if(resLen >= 8)
- {
- Ret_Buf->behindFuse_Voltage_C1 = (response[0] | (response[1] << 8));
- Ret_Buf->behindRelay_Voltage_C1 = (response[2] | (response[3] << 8));
- Ret_Buf->behindFuse_Voltage_C2 =(response[4] | (response[5] << 8));
- Ret_Buf->behindRelay_Voltage_C2 =(response[6] | (response[7] << 8));
- result = PASS;
- }
- return result;
- }
- unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Fan_Speed, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]))
- {
- for(int idx=0;idx < 4;idx++)
- Ret_Buf->speed[idx] = (rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_Fan_Speed, 0, 0, &response[0], debug);
- if(resLen >= 8)
- {
- for(int i = 0; i < 4; i++)
- {
- Ret_Buf->speed[i] = (response[2 * i] | (response[(2 * i) + 1] << 8));
- }
- result = PASS;
- }
- return result;
- }
- unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Relay_Output, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- // for (int i = 0; i < 7; i++)
- // printf("tx = %x \n", tx[i]);
- // for (int i = 0; i < len; i++)
- // printf("rx = %x \n", rx[i]);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]))
- {
- Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
- Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_Parallel_N = (rx[8] >> 2) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_Parallel_P = (rx[8] >> 3) & 0x01;
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_Relay_Output, 0, 0, &response[0], debug);
- if(resLen >= 3)
- {
- Ret_Buf->relay_event.bits.AC_Contactor = (response[0] >> 0) & 0x01;
- Ret_Buf->relay_event.bits.CCS_Precharge = (response[0] >> 1) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_N = (response[1] >> 0) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_P = (response[1] >> 1) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_Parallel_N = (response[1] >> 2) & 0x01;
- Ret_Buf->relay_event.bits.Gun1_Parallel_P = (response[1] >> 3) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_N = (response[2] >> 0) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_P = (response[2] >> 1) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_Parallel_N = (response[2] >> 2) & 0x01;
- Ret_Buf->relay_event.bits.Gun2_Parallel_P = (response[2] >> 3) & 0x01;
- result = PASS;
- }
- return result;
- }
- unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gfd_Adc, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- // for(int i = 0; i < 7; i++)
- // printf ("tx = %d \n", tx[i]);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- {
- //printf("Query_Gfd_Adc fail \n");
- return result;
- }
- // for(int i = 0; i < len; i++)
- // printf ("rx = %d \n", rx[i]);
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]))
- {
- Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
- Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
- Ret_Buf->result_conn1 = rx[10];
- Ret_Buf->rb_step_1 = rx[11];
- Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
- Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
- Ret_Buf->result_conn2 = rx[16];
- Ret_Buf->rb_step_2 = rx[17];
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_Gfd_Adc, 0, 0, &response[0], debug);
- if(resLen >= 12)
- {
- Ret_Buf->Resister_conn1 = (response[0] | (response[1] << 8));
- Ret_Buf->voltage_conn1 = (response[2] | (response[3] << 8));
- Ret_Buf->result_conn1 = response[4];
- Ret_Buf->rb_step_1 = response[5];
- Ret_Buf->Resister_conn2 = (response[6] | (response[7] << 8));
- Ret_Buf->voltage_conn2 = (response[8] | (response[9] << 8));
- Ret_Buf->result_conn2 = response[10];
- Ret_Buf->rb_step_2 = response[11];
- result = PASS;
- }
- return result;
- }
- unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[15] = {0xaa, 0x00, targetAddr, Cmd.config_Fan_Speed, 0x08, 0x00, Set_Buf->speed[0]&0xff, (Set_Buf->speed[0]>>8)&0xff, Set_Buf->speed[1]&0xff, (Set_Buf->speed[1]>>8)&0xff, Set_Buf->speed[2]&0xff, (Set_Buf->speed[2]>>8)&0xff, Set_Buf->speed[3]&0xff, (Set_Buf->speed[3]>>8)&0xff, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6+idx];
- tx[14] = chksum;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- chksum = 0x00;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- rx[6] == PASS)
- {
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- unsigned char data[8];
- data[0] = Set_Buf->speed[0] & 0xFF;
- data[1] = (Set_Buf->speed[0] >> 8) & 0xFF;
- data[2] = Set_Buf->speed[1] & 0xFF;
- data[3] = (Set_Buf->speed[1] >> 8) & 0xFF;
- data[4] = Set_Buf->speed[2] & 0xFF;
- data[5] = (Set_Buf->speed[2] >> 8) & 0xFF;
- data[6] = Set_Buf->speed[3] & 0xFF;
- data[7] = (Set_Buf->speed[3] >> 8) & 0xFF;
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.config_Fan_Speed, data, 8, &response[0], debug);
- if(resLen >= 0)
- {
- result = PASS;
- }
- return result;
- }
- unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[10] = {0xaa, 0x00, targetAddr, Cmd.config_Relay_Output, 0x03, 0x00, Set_Buf->relay_event.relay_status[0], Set_Buf->relay_event.relay_status[1], Set_Buf->relay_event.relay_status[2]};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6 + idx];
- tx[9] = chksum;
- // for (int i = 0; i < 10; i++)
- // printf("set relay cmd : tx = %x \n", tx[i]);
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- // for (int i = 0; i < len; i++)
- // printf("set relay cmd : rx = %x \n", rx[i]);
- chksum = 0x00;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- rx[6] == PASS)
- {
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.config_Relay_Output, Set_Buf->relay_event.relay_status, 3, &response[0], debug);
- if(resLen >= 0)
- {
- result = PASS;
- }
- return result;
- }
- unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gfd_Value, 0x02, 0x00, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- tx[6] = Set_Buf->index;
- tx[7] = Set_Buf->state;
- for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6+idx];
- tx[8] = chksum;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- chksum = 0x00;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]))
- {
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- unsigned char data[2];
- data[0] = Set_Buf->index;
- data[1] = Set_Buf->state;
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.config_Gfd_Value, data, 2, &response[0], debug);
- if(resLen >= 0)
- {
- result = PASS;
- }
- return result;
- }
- unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname, int len, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[21] = {0xaa, 0x00, targetAddr, Cmd.config_Model_Name, 0x0E, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- memcpy(tx + 6, modelname, 14);
- for(int idx = 0; idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6+idx];
- tx[20] = chksum;
- // for(int i = 0; i < 21; i++)
- // printf ("tx = %x \n", tx[i]);
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- // for(int i = 0; i < len; i++)
- // printf ("rx = %x \n", rx[i]);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- chksum = 0x00;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- rx[6] == PASS)
- {
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.config_Model_Name, modelname, len, &response[0], debug);
- if(resLen >= 0)
- {
- result = PASS;
- }
- return result;
- }
- unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[21] = { 0xaa, 0x00, targetAddr, Cmd.config_Rtc_Data, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
- Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
- Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
- chksum ^= tx[6 + idx];
- tx[20] = chksum;
- if (tranceive(fd, tx, sizeof(tx), rx, debug) > 0)
- {
- chksum = 0x00;
- for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
- {
- chksum ^= rx[6 + idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- rx[6] == PASS)
- {
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.config_Rtc_Data, (unsigned char *)&Set_Buf->RtcData[0], sizeof(Rtc), &response[0], debug);
- if(resLen >= 0)
- {
- result = PASS;
- }
- return result;
- }
- unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
- {
- unsigned char result = FAIL;
- unsigned char tx[11] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6+idx];
- tx[10] = chksum;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, 0);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- chksum = 0x00;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x00))
- {
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Start, 0x04, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, 0);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x00))
- {
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
- {
- unsigned char result = FAIL;
- unsigned char tx[11 + length];
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- tx[0] = 0xaa;
- tx[1] = 0x00;
- tx[2] = targetAddr;
- tx[3] = Cmd.update_Transfer;
- tx[4] = (4 + length) & 0xff;
- tx[5] = ((4 + length)>>8) & 0xff;
- tx[6] = (startAddr>>0) & 0xff;
- tx[7] = (startAddr>>8) & 0xff;
- tx[8] = (startAddr>>16) & 0xff;
- tx[9] = (startAddr>>24) & 0xff;
- memcpy(tx+10, data, length);
- for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6+idx];
- tx[sizeof(tx)-1] = chksum;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, 0);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x00))
- {
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.update_Finish, 0x04, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, 0);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x00))
- {
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Config_LED_Status(unsigned char fd, unsigned char targetAddr, Ac_Led_Status *Ret_Buf, int debug)
- {
- unsigned char result = FAIL;
- unsigned char tx[12] = {0xaa, 0x00, targetAddr, Cmd.config_ac_led_status, 0x05, 0x00, Ret_Buf->ActionMode, (Ret_Buf->AcAlarmCode >> 0) & 0xFF,
- (Ret_Buf->AcAlarmCode >> 8) & 0xFF, (Ret_Buf->AcAlarmCode >> 16) & 0xFF, (Ret_Buf->AcAlarmCode >> 24) & 0xFF};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
- chksum ^= tx[6 + idx];
- tx[11] = chksum;
- if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100, debug) > 0)
- {
- chksum = 0x00;
- for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++)
- {
- chksum ^= rx[6 + idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- rx[6] == PASS)
- {
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Config_Led_Color(unsigned char fd, unsigned char targetAddr, Led_Color *Ret_Buf, int debug)
- {
- #if 0
- unsigned char result = FAIL;
- unsigned char tx[13] = {0xaa, 0x00, targetAddr, Cmd.config_led_color, 0x06, 0x00,
- Ret_Buf->Connect_1_Red, Ret_Buf->Connect_1_Green, Ret_Buf->Connect_1_Blue,
- Ret_Buf->Connect_2_Red, Ret_Buf->Connect_2_Green, Ret_Buf->Connect_2_Blue};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
- chksum ^= tx[6 + idx];
- tx[12] = chksum;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx, debug);
- if(len > 6)
- {
- if (len < 6+(rx[4] | rx[5]<<8))
- return result;
- for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == PASS))
- {
- result = PASS;
- }
- }
- return result;
- #endif
- int resLen = 0;
- unsigned char result = FAIL;
- unsigned char response[512];
- unsigned char data[6];
- data[0] = Ret_Buf->Connect_1_Red;
- data[1] = Ret_Buf->Connect_1_Green;
- data[2] = Ret_Buf->Connect_1_Blue;
- data[3] = Ret_Buf->Connect_2_Red;
- data[4] = Ret_Buf->Connect_2_Green;
- data[5] = Ret_Buf->Connect_2_Blue;
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.config_led_color, data, 6, &response[0], debug);
- if(resLen >= 0)
- {
- result = PASS;
- }
- return result;
- }
|