Пример #1
0
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;

}
Пример #2
0
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);
}
Пример #3
0
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;
}
Пример #4
0
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;
}
Пример #5
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;
}
Пример #6
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;
}