libfins icon indicating copy to clipboard operation
libfins copied to clipboard

XX_finslib_decode_address problem

Open makay68g opened this issue 6 years ago • 9 comments

The function does not decode addresses like E0_, E1_, ... , E9_. The following patch corrects this problem:

diff -p -r -b /tmp/libfins-master/src/fins_decode.c libfins-master/src/fins_decode.c *** /tmp/libfins-master/src/fins_decode.c 2016-12-25 23:39:34.000000000 +0000 --- libfins-master/src/fins_decode.c 2018-09-12 07:10:03.429244324 +0000


*** 32,37 **** --- 32,38 ----

  • for use with devices over the Omron FINS protocol. */
  • #include <string.h> #include <ctype.h> #include "fins.h"

*************** bool XX_finslib_decode_address( const ch *** 66,77 **** --- 67,87 ----

    while ( isspace( *ptr ) ) ptr++;
  •   if (strchr(ptr,'_') && (strchr(ptr,'_')-ptr < 3))
    
  •   {
    
  •       num_char = strchr(ptr,'_')-ptr+1;
    
  •       strncpy(name,ptr,num_char);
    
  •       ptr = strchr(ptr,'_')+1;
    
  •   }
    
  •   else
    
  •   {
          while ( isalpha( *ptr ) &&  num_char < 3 ) {
    
              name[num_char] = (char) toupper( *ptr );
              num_char++;
              ptr++;
          }
    
  •   }
      if ( isalpha( *ptr ) ) return true;
    
      while ( num_char < 4 ) name[num_char++] = 0;
    

============================================

Also, the C1M, C1S, C02, C01, C22 addresses are not decoded correctly, but since I do not use them I did not patch the program for those.

makay68g avatar Sep 12 '18 07:09 makay68g

Thanks for your input. I will add it to the code together with fixes for the C1M, C1S, C01, C02 and C22 addresses as you mentioned.

lammertb avatar Nov 06 '18 07:11 lammertb

@makay68g - would you be able to share a quick snippet of working code? I have a hard time to get started. many thanks!!

juwalter avatar Dec 04 '22 17:12 juwalter

@juwalter: Look at this page, it is a working example, although the author did not encode html special characters:

http://www.venturii.net/blog/2019/02/19/omron-fins-protocol/

So I include the code here, I hope here it will be OK:

`/*

  • Program: libfins Test Program
  • File: test.c
  • Author: John Finlay
  • This file is licensed under the MIT License as stated below
  • Copyright (c) 2019 John Finlay
  • Permission is hereby granted, free of charge, to any person obtaining a copy
  • of this software and associated documentation files (the "Software"), to deal
  • in the Software without restriction, including without limitation the rights
  • to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  • copies of the Software, and to permit persons to whom the Software is
  • furnished to do so, subject to the following conditions:
  • The above copyright notice and this permission notice shall be included in all
  • copies or substantial portions of the Software.
  • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  • IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  • FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  • AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  • LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  • OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  • SOFTWARE. */

#include "include/fins.h" #include <stdio.h> #include <string.h>

int main() {

/* Initialize */

int err = 0, err_max = 10;
struct fins_sys_tp *c = NULL;

// memset(&amp;c, 0, sizeof(struct fins_sys_tp)); // Not needed - finslib_tcp_connect() only sets up default variables if it is passed a NULL pointer.
// init_system(&amp;c, err_max);

/* Connect to the PLC */

struct fins_sys_tp *sys = finslib_tcp_connect(c, "192.168.250.10", 9600, 1,
		5, 0, 1, 10, 0, &amp;err, err_max);
printf("Connection Call:   Error Code was [%u]\n", err);

char err_msg[64];
finslib_errmsg(err, err_msg, 64);
printf("Connection Call:   Error Message Was: [%s]\n", err_msg);

/* Find out what kind of PLC we are talking to */

struct fins_cpudata_tp cpudata;

int cuer = finslib_cpu_unit_data_read(sys, &amp;cpudata);

finslib_errmsg(cuer, err_msg, 64);
printf(
		"Read CPU Data:     Error Message Was: [%d] [%s] - sys-&gt;error_count = [%u] sockfd: [%u]\n",
		cuer, err_msg, sys-&gt;error_count, sys-&gt;sockfd);

/* Read CPU Status */

struct fins_cpustatus_tp cpustat;
int cpustat_ret = finslib_cpu_unit_status_read(sys, &amp;cpustat);
finslib_errmsg(cpustat_ret, err_msg, 64);
printf("CPU Unit Stat Read Error Message Was: [%s]\n", err_msg);

/* Read Memory Area */

uint16_t arr[2048];
int i;
for (i = 0; i &lt; 2048; i++) {
	arr[i] = 0;
} // Could use memset...

int num = 16;
int read_ret = finslib_memory_area_read_uint16(sys, "CIO100.0", arr, num);

finslib_errmsg(read_ret, err_msg, 64);
printf("Memory Area Read Error Message Was: [%s]\n", err_msg);

for (i = 0; i &lt; num; i++) {
	printf("arr[%u] = [%u]\n", i, arr[i]);
}

/* Write Memory Area */

for (i = 0; i &lt; 2048; i++) {
	arr[i] = 0;
} // Again, could use memset...

num = 16;
int write_ret = finslib_memory_area_write_uint16(sys, "CIO100.0", arr, num);
finslib_errmsg(write_ret, err_msg, 64);
printf("Memory Area Write Error Message Was: [%s]\n", err_msg);

for (i = 0; i &lt; num; i++) {
	printf("arr[%u] = [%u]\n", i, arr[i]);
}

/* Read Error Log */

struct fins_errordata_tp errordat;
size_t num_to_read = 1;
size_t num_read = 0;
int err_ret = finslib_error_log_read(sys, &amp;errordat, 0, &amp;num_to_read,
		&amp;num_read);

finslib_errmsg(err_ret, err_msg, 64);
printf(
		"Read Error Log:    Error Message Was: [%s] - Requested: [%zu] Records That Were Read: [%zu]\n",
		err_msg, num_to_read, num_read);

/* Read CPU Data */

struct fins_cpudata_tp cpuinfo;

int cpu_ret = finslib_cpu_unit_data_read(sys, &amp;cpuinfo);

finslib_errmsg(cpu_ret, err_msg, 64);
printf("CPU Unit Data Read Error Message Was: [%s]\n", err_msg);

/* Disconnect */

finslib_disconnect(sys);
printf("Connection closed.\n");
return 0;

} `

makay68g avatar Dec 04 '22 17:12 makay68g

@makay68g - thanks so much! this got me started!! your code from the issue comment did not work out of the box, but the code on your blog (nice write-up!!) did!

/*

    Program: libfins Test Program
    File: test.c
    Author: John Finlay
    This file is licensed under the MIT License as stated below
    Copyright (c) 2019 John Finlay
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    */

int main()
{

    /* Initialize */

    int err = 0, err_max = 10;
    struct fins_sys_tp *c = NULL;

    // memset(&c, 0, sizeof(struct fins_sys_tp)); // Not needed - finslib_tcp_connect() only sets up default variables if it is passed a NULL pointer.
    // init_system(&c, err_max);

    /* Connect to the PLC */

    struct fins_sys_tp *sys = finslib_tcp_connect(c, "192.168.250.10", 9600,
                                                  1, 5, 0,  // local_net, local_node, local_unit    - 0,0,0 might better
                                                  1, 10, 0, // remote_net, remote_node, remote_unit - 0,0,0 might better
                                                  &err, err_max);

    printf("Connection Call:   Error Code was [%u]\n", err);

    char err_msg[64];
    finslib_errmsg(err, err_msg, 64);
    printf("Connection Call:   Error Message Was: [%s]\n", err_msg);

    /* Find out what kind of PLC we are talking to */

    struct fins_cpudata_tp cpudata;

    int cuer = finslib_cpu_unit_data_read(sys, &cpudata);

    finslib_errmsg(cuer, err_msg, 64);
    printf(
        "Read CPU Data:     Error Message Was: [%d] [%s] - sys->error_count = [%u] sockfd: [%u]\n",
        cuer, err_msg, sys->error_count, sys->sockfd);

    /* Read CPU Status */

    struct fins_cpustatus_tp cpustat;
    int cpustat_ret = finslib_cpu_unit_status_read(sys, &cpustat);
    finslib_errmsg(cpustat_ret, err_msg, 64);
    printf("CPU Unit Stat Read Error Message Was: [%s]\n", err_msg);

    /* Read Memory Area */

    uint16_t arr[2048];
    int i;
    for (i = 0; i < 2048; i++)
    {
        arr[i] = 0;
    } // Could use memset...

    int num = 16;
    int read_ret = finslib_memory_area_read_uint16(sys, "CIO100.0", arr, num);

    finslib_errmsg(read_ret, err_msg, 64);
    printf("Memory Area Read Error Message Was: [%s]\n", err_msg);

    for (i = 0; i < num; i++)
    {
        printf("arr[%u] = [%u]\n", i, arr[i]);
    }

    /* Write Memory Area */

    for (i = 0; i < 2048; i++)
    {
        arr[i] = 0;
    } // Again, could use memset...

    num = 16;
    int write_ret = finslib_memory_area_write_uint16(sys, "CIO100.0", arr, num);
    finslib_errmsg(write_ret, err_msg, 64);
    printf("Memory Area Write Error Message Was: [%s]\n", err_msg);

    for (i = 0; i < num; i++)
    {
        printf("arr[%u] = [%u]\n", i, arr[i]);
    }

    /* Read Error Log */

    struct fins_errordata_tp errordat;
    size_t num_to_read = 1;
    size_t num_read = 0;
    int err_ret = finslib_error_log_read(sys, &errordat, 0, &num_to_read,
                                         &num_read);

    finslib_errmsg(err_ret, err_msg, 64);
    printf(
        "Read Error Log:    Error Message Was: [%s] - Requested: [%zu] Records That Were Read: [%zu]\n",
        err_msg, num_to_read, num_read);

    /* Read CPU Data */

    struct fins_cpudata_tp cpuinfo;

    int cpu_ret = finslib_cpu_unit_data_read(sys, &cpuinfo);

    finslib_errmsg(cpu_ret, err_msg, 64);
    printf("CPU Unit Data Read Error Message Was: [%s]\n", err_msg);

    /* Disconnect */

    finslib_disconnect(sys);
    printf("Connection closed.\n");
    return 0;
}

juwalter avatar Dec 04 '22 18:12 juwalter

@makay68g - on another note, do you happen to know how to figure out settings for local_net, local_node, local_unit and remote_net, remote_node, remote_unit?

For me, it seems it only works when using:

    uint8_t local_net = 0;
    uint8_t local_node = 0;
    uint8_t local_unit = 0;
    uint8_t remote_net = 0;
    uint8_t remote_node = 0;
    uint8_t remote_unit = 0;

thanks again!!

juwalter avatar Dec 04 '22 18:12 juwalter

@juwalter: It is not my blog, and not my code... :) . But still, glad to help.

The local part can be anything (as far as I know). The remote part must match the setting on the PLC side. I am not a PLC programmer, I got these numbers from the person that did the PLC programming.

makay68g avatar Dec 04 '22 18:12 makay68g

@juwalter: It is not my blog, and not my code... :) . But still, glad to help.

😄

The local part can be anything (as far as I know). The remote part must match the setting on the PLC side. I am not a PLC programmer, I got these numbers from the person that did the PLC programming.

thanks! After digging through the code, I think it is safe to initialize local with 0,0,0 and for remote it seems right to have to look into the PLC config first. Thanks again!!

juwalter avatar Dec 04 '22 18:12 juwalter

for UDP: https://github.com/lammertb/libfins/issues/15#issuecomment-1475282980

juwalter avatar Mar 19 '23 14:03 juwalter

@juwalter Hi, Based on my previous experience,fill in 0 for the first handshake message connection, and when the PLC returns the message, extract the automatically assigned node number and save it. PLC automatically assigns node numbers.

Norach-I avatar Jun 26 '23 08:06 Norach-I