// read mailbox and write to modbus void *reader(void *arg) { int ret,slave,function,buflen; unsigned char buf[256+1]; mbx.clear(); // clear old messages while((buflen = mbx.read(buf,sizeof(buf))) > 0) { slave = buf[0]; function = buf[1]; thread.lock(); rlsleep(MODBUS_IDLETIME); ret = modbus.write( slave, function, &buf[2], buflen-2); ret = modbus.response( &slave, &function, buf); if(ret != rlModbus::MODBUS_SUCCESS) sock.disconnect(); rlsleep(MODBUS_IDLETIME); thread.unlock(); if(ret < 0) { writeErrorCount++; if(writeErrorCount >= 256*128) writeErrorCount = 0; shm.write(modbusdaemon_WRITE_ERROR_COUNT_BASE,&writeErrorCount,2); } printf("mbx ret=%d slave=%d function=%d buf[2]=%d\n",ret,slave,function,buf[2]); } return arg; }
static void do_remove() { char candidate_name[100],name[100]; long pid; interpreter.copyStringParam(candidate_name,0); center.lock(); for(int irow=FIRST_ROW; spreadsheat.exists(irow,1); irow++) { rlstrncpy(name,spreadsheat.text(T_name,irow), sizeof(name)-1); if(strcmp(name,candidate_name) == 0) { #ifdef __VMS sscanf(spreadsheat.text(T_pid,irow),"%x",&pid); #else sscanf(spreadsheat.text(T_pid,irow),"%ld",&pid); #endif spreadsheat.printf(T_state, irow,""); spreadsheat.printf(T_pid, irow,""); spreadsheat.printf(T_name, irow,""); spreadsheat.printf(T_credit, irow,""); spreadsheat.printf(T_interval, irow,""); spreadsheat.printf(T_priority, irow,""); spreadsheat.printf(T_startup_command,irow,""); spreadsheat.printf(T_input, irow,""); spreadsheat.printf(T_output, irow,""); spreadsheat.printf(T_error, irow,""); spreadsheat.printf(T_initial_credit, irow,""); spreadsheat.printf(T_time, irow,""); break; } } center.unlock(); do_reload(1); }
int main(int ac, char **av) { int offset,ret,first; if(ac > 0) av0 = av[0]; first = 1; printf("\n%s starting\n",av0); modbus.registerSocket(&sock); thread.create(reader,NULL); watchdog.create(watchdogthread,NULL); shm.write(modbusdaemon_LIFE_COUNTER_BASE,&lifeCounter,2); shm.write(modbusdaemon_READ_ERROR_COUNT_BASE,&readErrorCount,2); shm.write(modbusdaemon_WRITE_ERROR_COUNT_BASE,&writeErrorCount,2); while(1) { lifeCounter++; if(lifeCounter >= 256*128) lifeCounter = 0; shm.write(modbusdaemon_LIFE_COUNTER_BASE,&lifeCounter,2); offset = 0; // modbusCycle(offset, slave, function, start_adr, num_register); ret = modbusCycle(offset,1,3,100,2); if(ret>0) offset += ret; else continue; } // we will never come here return 0; }
static void do_save() { rlPcontrol *item; char name[100]; //printf("%s",interpreter.line); interpreter.line[0] = '\0'; center.lock(); for(int irow=FIRST_ROW; spreadsheat.exists(irow,1); irow++) { rlstrncpy(name,spreadsheat.text(T_name,irow),sizeof(name)-1); if(name[0] > 0) { item = processlist; while(item != NULL) { if(strcmp(name,item->processName()) == 0) { #ifdef __VMS spreadsheat.printf(T_pid,irow,"%x",item->pid()); #else spreadsheat.printf(T_pid,irow,"%ld",item->pid()); #endif spreadsheat.printf(T_time,irow,item->processTime()->getTimeString()); } item = item->getNext(); } } } spreadsheat.write(SPREADSHEAT_FILE); center.unlock(); processes_have_been_modified = 0; }
static void do_reload(int write_flag) { char name[100]; //printf("%s",interpreter.line); interpreter.line[0] = '\0'; center.lock(); if(write_flag) { spreadsheat.write(SPREADSHEAT_FILE); } spreadsheat.read(SPREADSHEAT_FILE); set_header(); for(int irow=FIRST_ROW; spreadsheat.exists(irow,1); irow++) { rlstrncpy(name,spreadsheat.text(T_name,irow),sizeof(name)-1); if(name[0] > 0) { spreadsheat.printf(T_credit,irow,"%s",spreadsheat.text(T_initial_credit,irow)); } } construct_processlist(); center.unlock(); }
void *profibus(void *arg) { #ifdef _RL_HILSCHER_CIF_H_ THREAD_PARAM *p = (THREAD_PARAM *) arg; cif.debug = 1; if(cif.open() == DRV_NO_ERROR) { cif.debug = 0; while(p->running) { rlsleep(50); pbus.lock(); cif.devExchangeIO(0,4,sendData, 0,4,receiveData, 1000); pbus.unlock(); } } else { printf("failed to cif.open()\n"); printf("Please run me as root or\n"); printf("make /dev/cif readable by normal user\n"); } #else printf("WARNING: you will have to install the hilscher driver and link to it. Then you can remove the ifdef _WIN32\n"); #endif return arg; }
static void watchdog() { now.getLocalTime(); center.lock(); for(int row=FIRST_ROW; spreadsheat.exists(row,1); row++) { start_if_missing(row); } center.unlock(); }
static void do_sigkill() { long pid; rlPcontrol *item; //printf("%s",interpreter.line); sscanf(interpreter.line,"sigkill(%ld)",&pid); interpreter.line[0] = '\0'; center.lock(); item = processlist; while(item != NULL) { if(item->pid() == pid) { item->sigkill(); item->processTime()->getLocalTime(); // stop time break; } item = item->getNext(); } center.unlock(); }
// read cycle on modbus int modbusCycle(int offset, int slave, int function, int start_adr, int num_register) { unsigned char data[256+1]; int ret; watchcnt1++; if(watchcnt1 > 10000) watchcnt1 = 0; rlsleep(MODBUS_IDLETIME); thread.lock(); ret = modbus.request(slave, function, start_adr, num_register); if(ret >= 0) ret = modbus.response( &slave, &function, data); if(ret < 0) sock.disconnect(); thread.unlock(); if(ret > 0) shm.write(offset,data,ret); else { readErrorCount++; if(readErrorCount >= 256*128) readErrorCount = 0; shm.write(modbusdaemon_READ_ERROR_COUNT_BASE,&readErrorCount,2); } printf("cycle ret=%d slave=%d function=%d data[0]=%d\n",ret,slave,function,data[0]); return ret; }
int main(int ac, char **av) { PARAM p; int s; pvInit(ac,av,&p); /* here you may interpret ac,av and set p->user to your data */ memset(sendData,0,sizeof(sendData)); memset(receiveData,0,sizeof(receiveData)); pbus.create(profibus,NULL); while(1) { s = pvAccept(&p); if(s != -1) pvCreateThread(&p,s); else break; } return 0; }
int create_center() { center.create(centerMain,NULL); return 0; }