int main(int argc, char **argv) { int rc = PLCTAG_STATUS_OK; pthread_t thread[MAX_THREADS]; int num_threads; int thread_id = 0; if(argc != 2) { fprintf(stderr,"ERROR: Must provide number of threads to run (between 1 and 300) argc=%d!\n",argc); return 0; } num_threads = (int)strtol(argv[1],NULL, 10); if(num_threads < 1 || num_threads > MAX_THREADS) { fprintf(stderr,"ERROR: %d (%s) is not a valid number. Must provide number of threads to run (between 1 and 300)!\n",num_threads, argv[1]); return 0; } /* create the tag */ tag = plc_tag_create(TAG_PATH, DATA_TIMEOUT); /* everything OK? */ if(tag < 0) { fprintf(stderr,"ERROR %s: Could not create tag!\n", plc_tag_decode_error(tag)); return 0; } if((rc = plc_tag_status(tag)) != PLCTAG_STATUS_OK) { fprintf(stderr,"Error setting up tag internal state. %s\n", plc_tag_decode_error(rc)); plc_tag_destroy(tag); return 0; } /* create the read threads */ fprintf(stderr,"Creating %d threads.\n",num_threads); for(thread_id=0; thread_id < num_threads; thread_id++) { pthread_create(&thread[thread_id], NULL, thread_func, (void *)(intptr_t)thread_id); } /* wait until ^C */ while(1) { util_sleep_ms(100); } done = 1; for(thread_id = 0; thread_id < num_threads; thread_id++) { pthread_join(thread[thread_id], NULL); } plc_tag_destroy(tag); return 0; }
int main() { int i; char str[STRING_DATA_SIZE] = {0}; plc_tag tag = create_tag(TAG_PATH); int rc; if(!tag) { fprintf(stdout,"ERROR: Unable to create tag!\n"); return 0; } /* test pre-read by writing first */ for(i=0; i<ARRAY_2_DIM_SIZE; i++) { snprintf_platform(str,sizeof(str), "string value for element %d", i); update_string(tag, i, str); } /* write the data */ rc = plc_tag_write(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { fprintf(stdout,"ERROR: Unable to read the data! Got error code %d: %s\n",rc, plc_tag_decode_error(rc)); return 0; } /* get the data */ rc = plc_tag_read(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { fprintf(stdout,"ERROR: Unable to read the data! Got error code %d: %s\n",rc, plc_tag_decode_error(rc)); return 0; } dump_strings(tag); plc_tag_destroy(tag); return 0; }
void test_debug(void) { int32_t tag = 0; int old_debug, new_debug; fprintf(stderr,"Testing debug tag.\n"); tag = plc_tag_create("make=system&family=library&name=debug&debug=4", TAG_CREATE_TIMEOUT); if(tag < 0) { fprintf(stderr,"ERROR %s: Could not create tag!\n", plc_tag_decode_error(tag)); return; } plc_tag_read(tag, 0); old_debug = plc_tag_get_int32(tag,0); fprintf(stderr,"Current debug level is %d\n",old_debug); new_debug = (old_debug == 3 ? 4 : 3); plc_tag_set_int32(tag, 0, new_debug); plc_tag_write(tag, 0); plc_tag_read(tag, 0); new_debug = plc_tag_get_int32(tag,0); fprintf(stderr,"Now debug level is %d\n",new_debug); new_debug = old_debug; plc_tag_set_int32(tag, 0, new_debug); plc_tag_write(tag, 0); plc_tag_read(tag, 0); new_debug = plc_tag_get_int32(tag,0); fprintf(stderr,"Reset debug level to %d\n",new_debug); plc_tag_destroy(tag); }
void test_version(void) { int32_t tag = 0; int i; char ver[16] = {0,}; fprintf(stderr,"Testing version tag.\n"); tag = plc_tag_create("make=system&family=library&name=version&debug=4", TAG_CREATE_TIMEOUT); if(tag < 0) { fprintf(stderr,"ERROR %s: Could not create tag!\n", plc_tag_decode_error(tag)); return; } plc_tag_read(tag, 0); for(i=0; i < 16 && plc_tag_get_uint8(tag,i) != 0; i++) { ver[i] = (char)plc_tag_get_uint8(tag,i); } fprintf(stderr,"Library version %s\n", ver); plc_tag_destroy(tag); }
void *thread_func(void *data) { int tid = (int)(intptr_t)data; int rc; int value; while(!done) { int64_t start; int64_t end; /* capture the starting time */ start = util_time_ms(); /* use do/while to allow easy exit without return */ do { rc = plc_tag_lock(tag); if(rc != PLCTAG_STATUS_OK) { value = 1000; break; /* punt, no lock */ } rc = plc_tag_read(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { value = 1001; } else { value = (int)plc_tag_get_int32(tag,0); /* increment the value */ value = (value > 500 ? 0 : value + 1); /* yes, we should be checking this return value too... */ plc_tag_set_int32(tag, 0, (int32_t)value); /* write the value */ rc = plc_tag_write(tag, DATA_TIMEOUT); } /* yes, we should look at the return value */ plc_tag_unlock(tag); } while(0); end = util_time_ms(); fprintf(stderr,"Thread %d got result %d with return code %s in %dms\n",tid,value,plc_tag_decode_error(rc),(int)(end-start)); util_sleep_ms(1); } return NULL; }
int main() { int32_t tag = 0; int rc; int b; /* create the tag */ tag = plc_tag_create(TAG_PATH, DATA_TIMEOUT); /* everything OK? */ if(tag < 0) { fprintf(stderr,"ERROR %s: Could not create tag!\n", plc_tag_decode_error(tag)); return 0; } /* let the connect succeed we hope */ while(plc_tag_status(tag) == PLCTAG_STATUS_PENDING) { util_sleep_ms(100); } if(plc_tag_status(tag) != PLCTAG_STATUS_OK) { fprintf(stderr,"Error setting up tag internal state. Error %s\n", plc_tag_decode_error(plc_tag_status(tag))); return 0; } /* get the data */ rc = plc_tag_read(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { fprintf(stderr,"ERROR: Unable to read the data! Got error code %d: %s\n",rc, plc_tag_decode_error(rc)); return 0; } /* print out the data */ b = plc_tag_get_uint8(tag,0); fprintf(stderr,"bool = %d\n", b); plc_tag_set_uint8(tag, 0, (b ? 0 : 255)); rc = plc_tag_write(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { fprintf(stderr,"ERROR: Unable to read the data! Got error code %d: %s\n",rc, plc_tag_decode_error(rc)); return 0; } /* get the data again*/ rc = plc_tag_read(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { fprintf(stderr,"ERROR: Unable to read the data! Got error code %d: %s\n",rc, plc_tag_decode_error(rc)); return 0; } /* print out the data */ b = plc_tag_get_uint8(tag,0); fprintf(stderr,"bool = %d\n", b); /* we are done */ plc_tag_destroy(tag); return 0; }