12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274 |
- /*
- * 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
- struct Address Addr={0x01,0x02,0x03,0x05,0x06,0xFF};
- struct Command Cmd={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x24,0x27,0x28,0x29,0x2C,0x81,0x83,
- 0x85,0x86,0x87,0x88,0x089,0x8A,0x8B,0x8C,0x90,0x93,0xe0,0xe1,0xe2,0xe3};
- 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)
- {
- usleep(_delay * 1000);
- len = read(fd, rx, 512);
- }
- 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)
- {
- usleep(15000);
- len = read(fd, rx, 512);
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("Serial command %s response fail.\n", cmd);
- #endif
- }
- return len;
- }
- 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);
- 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_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;
- 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[13] = 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] == PASS))
- {
- result = PASS;
- }
- }
- return result;
- }
|