where to download example to use libfins?
fanucwj opened this issue ยท 4 comments
fanucwj commented
where to download example to use libfins?
juwalter commented
I found the following example http://www.venturii.net/blog/2019/02/19/omron-fins-protocol/ which helped me a great deal to get started.
/*
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 commented
note, while learning FINS I also found a very helpful introduction at https://d2000.ipesoft.com/blog/communication-omron-fins/ - from there I found how to use connection parameters for finslib_udp_connect
, specifically, setting of local_node
to e.g. 3
got my environment working (OMRON CJ2H-CPU64-EIP)
finslib_udp_connect(
struct fins_sys_tp *sys,
const char *address,
uint16_t port,
uint8_t local_net,
uint8_t local_node, <--------- in case of UDP, 0 does not work
uint8_t local_unit,
uint8_t remote_net,
uint8_t remote_node,
uint8_t remote_unit,
int *error_val,
int error_max
)
seems crucial in order to get UDP connections working.
technoshamanarchist commented
@juwalter Thank you for these :)