/*----------------------------------------------------------------------------- * sio open and bus init */ static int InitBus(const char *comPort) { uint8_t ch; int handle; SioInit(); handle = SioOpen(comPort, eSioBaud9600, eSioDataBits8, eSioParityNo, eSioStopBits1, eSioModeHalfDuplex); if (handle == -1) { return -1; } while (SioGetNumRxChar(handle) > 0) { SioRead(handle, &ch, sizeof(ch)); } BusInit(handle); return handle; }
/*----------------------------------------------------------------------------- * main */ int main(int argc, char *argv[]) { int handle; int i; char comPort[SIZE_COMPORT] = ""; uint8_t len; uint8_t val8; /* get tty device */ for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-c") == 0) { if (argc > i) { strncpy(comPort, argv[i + 1], sizeof(comPort) - 1); comPort[sizeof(comPort) - 1] = 0; } break; } } if (strlen(comPort) == 0) { PrintUsage(); return 0; } SioInit(); handle = SioOpen(comPort, eSioBaud9600, eSioDataBits8, eSioParityNo, eSioStopBits1, eSioModeHalfDuplex); if (handle == -1) { printf("can't open %s\r\n", comPort); return 0; } // wait for sio input to settle and the flush usleep(100000); while ((len = SioGetNumRxChar(handle)) != 0) { SioRead(handle, &val8, sizeof(val8)); } BusInit(handle); #if 1 BusVarInit(67, BusVarNv); { uint8_t var1 = 1; uint16_t var2 = 0x1234; bool rc; uint8_t len; TBusVarHdl hdl1; TBusVarHdl hdl2; TBusTelegram *msg; TBusVarResult result; TBusVarState state; BusVarAdd(0, sizeof(var1), true); BusVarAdd(1, sizeof(var2), false); rc = BusVarWrite(1, &var2, sizeof(var2), &result); rc = BusVarWrite(0, &var1, sizeof(var1), &result); var1 = 0; var2 = 0; len = BusVarRead(1, &var2, sizeof(var2), &result); printf("len %d var2 %x\n", len, var2); len = BusVarRead(0, &var1, sizeof(var1), &result); printf("len %d var1 %x\n", len, var1); hdl2 = BusVarTransactionOpen(242, 1, &var2, sizeof(var2), eBusVarRead); hdl1 = BusVarTransactionOpen(242, 0, &var1, sizeof(var1), eBusVarRead); for (i = 0; i < 1000; i++) { BusVarProcess(); usleep(10000); if (BusCheck() == BUS_MSG_OK) { msg = BusMsgBufGet(); if ((msg->type == eBusDevRespGetVar) && (msg->msg.devBus.receiverAddr == 67)) { BusVarRespGet(msg->senderAddr, &msg->msg.devBus.x.devResp.getVar); } } if (hdl1 != BUSVAR_HDL_INVALID) { state = BusVarTransactionState(hdl1); if (state & BUSVAR_STATE_FINAL) { if (state == eBusVarState_Ready) { printf("var1 %02x\n", var1); } BusVarTransactionClose(hdl1); hdl1 = BUSVAR_HDL_INVALID; } } if (hdl2 != BUSVAR_HDL_INVALID) { state = BusVarTransactionState(hdl2); if (state & BUSVAR_STATE_FINAL) { if (state == eBusVarState_Ready) { printf("var2 %04x\n", var2); } BusVarTransactionClose(hdl2); hdl2 = BUSVAR_HDL_INVALID; } } } } return 0; #endif if (Test() == 0) { printf("OK\n"); } else { printf("ERROR\n"); } if (handle != -1) { SioClose(handle); } return 0; }