int dump_strings(plc_tag tag) { char str_data[STRING_DATA_SIZE]; int str_index; int str_len; int num_strings = plc_tag_get_size(tag) / ELEM_SIZE; int i; /* loop over the whole thing. */ for(i=0; i< num_strings; i++) { /* get the string length */ str_len = plc_tag_get_int32(tag,i * ELEM_SIZE); /* copy the data */ for(str_index=0; str_index<str_len; str_index++) { str_data[str_index] = (char)plc_tag_get_uint8(tag,(i*ELEM_SIZE)+4+str_index); } /* pad with zeros */ for(;str_index<STRING_DATA_SIZE; str_index++) { str_data[str_index] = 0; } printf("String [%d] = \"%s\"\n",i,str_data); } 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 *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; }
void log_data(plc_tag tag) { static int log_year = 0; static int log_month = 0; static int log_day = 0; static FILE *log = NULL; int i = 0; char timestamp_buf[128]; time_t t = time(NULL); struct tm *tm_struct; tm_struct = localtime(&t); /* do we need to open the file?*/ if(log_year != tm_struct->tm_year || log_month != tm_struct->tm_mon || log_day != tm_struct->tm_mday) { char log_file_name[128]; log_year = tm_struct->tm_year; log_month = tm_struct->tm_mon; log_day = tm_struct->tm_mday; snprintf(log_file_name, sizeof(log_file_name),"log-%04d-%02d-%02d.log", 1900+log_year, log_month, log_day); if(log) { fclose(log); } log = fopen(log_file_name,"a"); } strftime(timestamp_buf, sizeof(timestamp_buf), "%Y/%m/%d %H:%M:%S", tm_struct); fprintf(log,"%s",timestamp_buf); for(i=0; i<ELEM_COUNT; i++) { fprintf(log,",%d",plc_tag_get_int32(tag,i*ELEM_SIZE)); } fprintf(log,"\n"); fflush(log); }
int main(int argc, char **argv) { plc_tag tag = PLC_TAG_NULL; int is_write = 0; uint32_t u_val; int32_t i_val; real32_t f_val; int i; int rc; int timeout; parse_args(argc, argv); /* check arguments */ if(!path || !data_type) { usage(); exit(0); } /* convert any write values */ if(write_str && strlen(write_str)) { is_write = 1; switch(data_type) { case PLC_LIB_UINT8: case PLC_LIB_UINT16: case PLC_LIB_UINT32: if(sscanf(write_str,"%u",&u_val) != 1) { printf("ERROR: bad format for unsigned integer for write value.\n"); usage(); exit(1); } break; case PLC_LIB_SINT8: case PLC_LIB_SINT16: case PLC_LIB_SINT32: if(sscanf(write_str,"%d",&i_val) != 1) { printf("ERROR: bad format for signed integer for write value.\n"); usage(); exit(1); } break; case PLC_LIB_REAL32: if(sscanf(write_str,"%f",&f_val) != 1) { printf("ERROR: bad format for 32-bit floating point for write value.\n"); usage(); exit(1); } break; default: printf("ERROR: bad data type!"); usage(); exit(1); break; } } else { is_write = 0; } /* create the tag */ tag = plc_tag_create(path); if(!tag) { printf("ERROR: error creating tag!\n"); return 0; } timeout = 5; while(timeout-- && plc_tag_status(tag) == PLCTAG_STATUS_PENDING) { my_sleep(1); } rc = plc_tag_status(tag); if(rc != PLCTAG_STATUS_OK) { printf("ERROR: tag creation error, tag status: %s\n",decode_error(rc)); plc_tag_destroy(tag); return 0; } do { if(!is_write) { int index = 0; rc = plc_tag_read(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { printf("ERROR: tag read error, tag status: %s\n",decode_error(rc)); plc_tag_destroy(tag); return 0; } /* display the data */ for(i=0; index < plc_tag_get_size(tag); i++) { switch(data_type) { case PLC_LIB_UINT8: printf("data[%d]=%u (%x)\n",i,plc_tag_get_uint8(tag,index),plc_tag_get_uint8(tag,index)); index += 1; break; case PLC_LIB_UINT16: printf("data[%d]=%u (%x)\n",i,plc_tag_get_uint16(tag,index),plc_tag_get_uint16(tag,index)); index += 2; break; case PLC_LIB_UINT32: printf("data[%d]=%u (%x)\n",i,plc_tag_get_uint32(tag,index),plc_tag_get_uint32(tag,index)); index += 4; break; case PLC_LIB_SINT8: printf("data[%d]=%d (%x)\n",i,plc_tag_get_int8(tag,index),plc_tag_get_int8(tag,index)); index += 1; break; case PLC_LIB_SINT16: printf("data[%d]=%d (%x)\n",i,plc_tag_get_int16(tag,index),plc_tag_get_int16(tag,index)); index += 2; break; case PLC_LIB_SINT32: printf("data[%d]=%d (%x)\n",i,plc_tag_get_int32(tag,index),plc_tag_get_int32(tag,index)); index += 4; break; case PLC_LIB_REAL32: printf("data[%d]=%f\n",i,plc_tag_get_float32(tag,index)); index += 4; break; } } } else { switch(data_type) { case PLC_LIB_UINT8: rc = plc_tag_set_uint8(tag,0,u_val); break; case PLC_LIB_UINT16: rc = plc_tag_set_uint16(tag,0,u_val); break; case PLC_LIB_UINT32: rc = plc_tag_set_uint32(tag,0,u_val); break; case PLC_LIB_SINT8: rc = plc_tag_set_int8(tag,0,i_val); break; case PLC_LIB_SINT16: rc = plc_tag_set_int16(tag,0,i_val); break; case PLC_LIB_SINT32: rc = plc_tag_set_int32(tag,0,i_val); break; case PLC_LIB_REAL32: rc = plc_tag_set_float32(tag,0,f_val); break; } /* write the data */ rc = plc_tag_write(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { printf("ERROR: error writing data: %s!\n",decode_error(rc)); } else { printf("Wrote %s\n",write_str); } } } while(0); if(write_str) free(write_str); if(path) free(path); if(tag) plc_tag_destroy(tag); printf("Done\n"); return 0; }
int main(int argc, char **argv) { plc_tag tag = PLC_TAG_NULL; int rc; int i; /* create the tag */ tag = plc_tag_create(TAG_PATH); /* everything OK? */ if(!tag) { fprintf(stderr,"ERROR: Could not create tag!\n"); return 0; } /* let the connect succeed we hope */ while(plc_tag_status(tag) == PLCTAG_STATUS_PENDING) { sleep(1); } if(plc_tag_status(tag) != PLCTAG_STATUS_OK) { fprintf(stderr,"Error setting up tag internal state.\n"); 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\n",rc); return 0; } /* print out the data */ for(i=0; i < ELEM_COUNT; i++) { int str_size = plc_tag_get_int32(tag,(i*ELEM_SIZE)); char str[83] = {0}; int j; for(j=0; j<str_size; j++) { str[j] = (char)plc_tag_get_uint8(tag,(i*ELEM_SIZE)+j+4); } str[j] = (char)0; printf("string %d (%d chars) = '%s'\n",i, str_size, str); } /* do it 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\n",rc); return 0; } /* print out the data */ for(i=0; i < ELEM_COUNT; i++) { int str_size = plc_tag_get_int32(tag,(i*ELEM_SIZE)); char str[83] = {0}; int j; for(j=0; j<str_size; j++) { str[j] = (char)plc_tag_get_uint8(tag,(i*ELEM_SIZE)+j+4); } str[j] = (char)0; printf("string %d (%d chars) = '%s'\n",i, str_size, str); } /* we are done */ plc_tag_destroy(tag); return 0; }
int main(int argc, char **argv) { plc_tag tag = PLC_TAG_NULL; char *write_str=NULL; int is_write = 0; char *path = NULL; int data_type=0; uint32_t u_val; int32_t i_val; real32_t f_val; int i; int c; int rc; while ((c=getopt(argc,argv,"t:w:p:?h"))!=EOF) { switch(c) { case 't': if(!strcasecmp("uint8",optarg)) { data_type = PLC_LIB_UINT8; } else if(!strcasecmp("sint8",optarg)) { data_type = PLC_LIB_SINT8; } else if(!strcasecmp("uint16",optarg)) { data_type = PLC_LIB_UINT16; } else if(!strcasecmp("sint16",optarg)) { data_type = PLC_LIB_SINT16; } else if(!strcasecmp("uint32",optarg)) { data_type = PLC_LIB_UINT32; } else if(!strcasecmp("sint32",optarg)) { data_type = PLC_LIB_SINT32; } else if(!strcasecmp("real32",optarg)) { data_type = PLC_LIB_REAL32; } else { printf("ERROR: unknown data type: %s\n",optarg); usage(); exit(1); } break; case 'w': write_str = strdup(optarg); break; case 'p': path = strdup(optarg); break; case 'h': case '?': usage(); exit(0); break; default: printf("ERROR: unknown option.\n"); usage(); exit(1); break; } } /* check arguments */ if(!path || !data_type) { usage(); exit(0); } /* convert any write values */ if(write_str && strlen(write_str)) { is_write = 1; switch(data_type) { case PLC_LIB_UINT8: case PLC_LIB_UINT16: case PLC_LIB_UINT32: if(sscanf(write_str,"%u",&u_val) != 1) { printf("ERROR: bad format for unsigned integer for write value.\n"); usage(); exit(1); } break; case PLC_LIB_SINT8: case PLC_LIB_SINT16: case PLC_LIB_SINT32: if(sscanf(write_str,"%d",&i_val) != 1) { printf("ERROR: bad format for signed integer for write value.\n"); usage(); exit(1); } break; case PLC_LIB_REAL32: if(sscanf(write_str,"%f",&f_val) != 1) { printf("ERROR: bad format for 32-bit floating point for write value.\n"); usage(); exit(1); } break; default: printf("ERROR: bad data type!"); usage(); exit(1); break; } } else { is_write = 0; } /* create the tag */ tag = plc_tag_create(path); if(!tag) { printf("ERROR: error creating tag!\n"); return 0; } rc = plc_tag_status(tag); if(rc != PLCTAG_STATUS_OK) { printf("ERROR: tag creation error, tag status: %d\n",rc); plc_tag_destroy(tag); return 0; } do { if(!is_write) { int index = 0; rc = plc_tag_read(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { printf("ERROR: tag read error, tag status: %d\n",rc); plc_tag_destroy(tag); return 0; } /* display the data */ for(i=0; index < plc_tag_get_size(tag); i++) { switch(data_type) { case PLC_LIB_UINT8: printf("data[%d]=%u (%x)\n",i,plc_tag_get_uint8(tag,index),plc_tag_get_uint8(tag,index)); index += 1; break; case PLC_LIB_UINT16: printf("data[%d]=%u (%x)\n",i,plc_tag_get_uint16(tag,index),plc_tag_get_uint16(tag,index)); index += 2; break; case PLC_LIB_UINT32: printf("data[%d]=%u (%x)\n",i,plc_tag_get_uint32(tag,index),plc_tag_get_uint32(tag,index)); index += 4; break; case PLC_LIB_SINT8: printf("data[%d]=%d (%x)\n",i,plc_tag_get_int8(tag,index),plc_tag_get_int8(tag,index)); index += 1; break; case PLC_LIB_SINT16: printf("data[%d]=%d (%x)\n",i,plc_tag_get_int16(tag,index),plc_tag_get_int16(tag,index)); index += 2; break; case PLC_LIB_SINT32: printf("data[%d]=%d (%x)\n",i,plc_tag_get_int32(tag,index),plc_tag_get_int32(tag,index)); index += 4; break; case PLC_LIB_REAL32: printf("data[%d]=%f\n",i,plc_tag_get_float32(tag,index)); index += 4; break; } } } else { switch(data_type) { case PLC_LIB_UINT8: rc = plc_tag_set_uint8(tag,0,u_val); break; case PLC_LIB_UINT16: rc = plc_tag_set_uint16(tag,0,u_val); break; case PLC_LIB_UINT32: rc = plc_tag_set_uint32(tag,0,u_val); break; case PLC_LIB_SINT8: rc = plc_tag_set_int8(tag,0,i_val); break; case PLC_LIB_SINT16: rc = plc_tag_set_int16(tag,0,i_val); break; case PLC_LIB_SINT32: rc = plc_tag_set_int32(tag,0,i_val); break; case PLC_LIB_REAL32: rc = plc_tag_set_float32(tag,0,f_val); break; } /* write the data */ rc = plc_tag_write(tag, DATA_TIMEOUT); if(rc != PLCTAG_STATUS_OK) { printf("ERROR: error writing data: %d!\n",rc); } else { printf("Wrote %s\n",write_str); } } } while(0); if(write_str) free(write_str); if(path) free(path); if(tag) plc_tag_destroy(tag); printf("Done\n"); return 0; }