123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485 |
- /*
- * 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_RAW_DATA 0
- #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 len;
- //sleep(2); //required to make flush work, for some reason
- tcflush(fd,TCIOFLUSH);
- if(write(fd, cmd, cmd_len) >= cmd_len)
- {
- #if RS485_RAW_DATA
- char buffer[1024];
- char temp[32];
- 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);
- #endif
- usleep(_delay * 1000);
- len = read(fd, rx, 512);
- #if RS485_RAW_DATA
- 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);
- #endif
- }
- 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 len;
- //sleep(2); //required to make flush work, for some reason
- tcflush(fd,TCIOFLUSH);
- if(write(fd, cmd, cmd_len) >= cmd_len)
- {
- #if RS485_RAW_DATA
- char buffer[1024];
- char temp[32];
- 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);
- #endif
- usleep(15000);
- len = read(fd, rx, 512);
- #if RS485_RAW_DATA
- 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);
- #endif
- }
- 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 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]);
- 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)
- {
- 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);
- // 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;
- }
- unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
- {
- 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);
- 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;
- }
- unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
- {
- 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);
- 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;
- }
- unsigned char Query_DC_InputVoltage(unsigned char fd, unsigned char targetAddr, DCInputVoltage *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char resLen = 0;
- unsigned char response[512];
- memset(response, 0x00, 512);
- resLen = Internal_Comm_Request(fd, targetAddr, Cmd.query_DC_InputVoltage, 0, 0, &response[0]);
- 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)
- {
- 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);
- 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;
- }
- unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
- {
- 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);
- 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;
- }
- unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Temperature, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
- 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->temperature[idx] = rx[6+idx] - 60;
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Aux_PowerVoltage, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
- 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<(rx[4] | rx[5]<<8);idx++)
- Ret_Buf->voltage[idx] = rx[6+idx];
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
- {
- 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);
- // 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;
- }
- unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
- {
- 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);
- // 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;
- }
- unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Gpio_In, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
- 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->AC_Connector = (rx[6] >> 0) & 0x01;
- Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
- Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
- Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
- Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
- Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
- Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
- Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
- Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
- Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
- Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
- Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
- Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
- Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
- Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Query_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_Model_Name, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
- 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]))
- {
- strncpy((char *)modelname, (char *)(rx + 6), (rx[4] | rx[5]<<8));
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
- {
- 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);
- 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;
- }
- unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
- {
- 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);
- 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;
- }
- unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Gpio_Output, 0x01, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
- for(int idx = 0;idx<2;idx++)
- tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
- for(int idx = 0;idx<4;idx++)
- tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
- 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);
- 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;
- }
- unsigned char Config_Gfd_Value(unsigned char fd, unsigned char targetAddr, Gfd_config *Set_Buf)
- {
- 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);
- 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;
- }
- unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, unsigned char *modelname)
- {
- 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);
- // 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;
- }
- unsigned char Config_Rtc_Data(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
- {
- 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) > 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 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);
- 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);
- 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);
- 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);
- 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 Query_AC_Status(unsigned char fd, unsigned char targetAddr, Ac_Status *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_status, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
- 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->CpStatus = rx[6];
- Ret_Buf->CurLimit = (rx[7] | (rx[8] << 8));
- Ret_Buf->PilotVol_P = (rx[9] | (rx[10] << 8));
- Ret_Buf->PilotVol_N = (rx[11] | (rx[12] << 8));
- Ret_Buf->LockStatus = rx[13];
- Ret_Buf->RelayStatus = rx[14];
- Ret_Buf->ShutterStatus = rx[15];
- Ret_Buf->MeterStatus = rx[16];
- Ret_Buf->PpStatus = rx[17];
- Ret_Buf->MaxCurrent = rx[18];
- Ret_Buf->RotateSwitchStatus = rx[19];
- //
- // Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
- // Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
- // Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
- // Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
- // Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
- // Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
- // Ret_Buf->AC_Drop = (rx[6] >> 6) & 0x01;
- //
- // Ret_Buf->Emergency_IO = (rx[7] >> 0) & 0x01;
- //
- // Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
- // Ret_Buf->Button_On_Press = (rx[8] >> 1) & 0x01;
- // Ret_Buf->Button_Off_Press = (rx[8] >> 2) & 0x01;
- // Ret_Buf->Key_1_Press = (rx[8] >> 3) & 0x01;
- // Ret_Buf->Key_2_Press = (rx[8] >> 4) & 0x01;
- // Ret_Buf->Key_3_Press = (rx[8] >> 5) & 0x01;
- // Ret_Buf->Key_4_Press = (rx[8] >> 6) & 0x01;
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Query_AC_Alarm_Code(unsigned char fd, unsigned char targetAddr, Ac_Alarm_code *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_alarm_code, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
- 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->AcAlarmCode = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Query_Charging_Energy(unsigned char fd, unsigned char targetAddr, Ac_Charging_energy *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_output_energy, 0x00, 0x00,0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
- 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->Energy = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Query_Charging_Current(unsigned char fd, unsigned char targetAddr, Ac_Charging_current *Ret_Buf)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, Cmd.query_ac_output_current, 0x00, 0x00, 0x00};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- unsigned char len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);
- 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->OuputCurrentL1 = rx[6] + (rx[7] << 8);
- Ret_Buf->OuputCurrentL2 = rx[8] + (rx[9] << 8);
- Ret_Buf->OuputCurrentL3 = rx[10] + (rx[11] << 8);
- result = PASS;
- }
- }
- return result;
- }
- unsigned char Config_LED_Status(unsigned char fd, unsigned char targetAddr, Ac_Led_Status *Ret_Buf)
- {
- 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) > 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_Legacy_Req(unsigned char fd, unsigned char targetAddr, unsigned char _switch)
- {
- unsigned char result = FAIL;
- unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_Legacy_Req, 0x02, 0x00, _switch, 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[8] = chksum;
- if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 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_Ac_Duty(unsigned char fd, unsigned char targetAddr, unsigned char _value)
- {
- unsigned char result = FAIL;
- unsigned char tx[8] = {0xaa, 0x00, targetAddr, Cmd.config_ac_duty, 0x01, 0x00, _value};
- unsigned char rx[512];
- unsigned char chksum = 0x00;
- for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++)
- chksum ^= tx[6 + idx];
- tx[7] = chksum;
- if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 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_CSU_Mode(unsigned char fd, unsigned char targetAddr)
- {
- unsigned char result = FAIL;
- unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_csu_mode, 0x02, 0x00, 0x01, 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[7] = chksum;
- if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 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_Reset_MCU(unsigned char fd, unsigned char targetAddr)
- {
- unsigned char result = FAIL;
- unsigned char tx[9] = {0xaa, 0x00, targetAddr, Cmd.config_reset_mcu, 0x02, 0x00, 0x01, 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[7] = chksum;
- if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 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)
- {
- 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;
- // for(int i = 0; i < 13; i++)
- // printf ("tx = %x \n", tx[i]);
- unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
- // 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]) &&
- (rx[6] == PASS))
- {
- result = PASS;
- }
- }
- return result;
- }
|