Linux采集Fanuc机床数据测试

依赖库文件

  • 目录:FOCAS2\Linux
  • so文件:libfwlib32.so.1.0.5
  • 头文件:fwlib32.h

操作步骤

  • 拷贝库文件"libfwlib32.so.1.0.5" 到应用目录(可以直接复制到系统库目录更方便 “/usr/local/lib/”)
  • 运行如下3个命令:
sudo ldconfig
sudo ln –s /usr/local/lib/libfwlib32.so.1.0.0 /usr/local/lib/libfwlib32.so
sudo ln –s /usr/local/lib/libfwlib32.so.1.0.0 /usr/local/lib/libfwlib32.so.1(没有这一步会报错)

编译过程

GCC编译程序的时候添加如下库依赖 “-lfwlib32 -lstdc++ -lpthread”

  1. FOCAS2/Ethernet library(fwlib32)
  2. GNU Standard C++ library (stdc++)
  3. POSIX thread library (pthread)
gcc test.cpp -o test -lfwlib32 -lstdc++ -lpthread

测试程序

#include <iostream>
#include <fstream>
#include <string>
#include "fwlib32.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>

#define  USHORT unsigned short
#define  MAXLEN 1280

using namespace std;

short DownloadFile (USHORT h, const char *filepath);
short UploadFile (USHORT h, short progID, const char *filePath);
void HexDump (char *buf, int len, int addr);

int
main (int argc, char **argv)
{
  unsigned short h = 1;
  short ret;
  string ip = "192.168.0.190";

  if (argc > 1)
    {
      ip = argv[1];
    }

  if (EW_OK != cnc_startupprocess (0, "/var/log/foces32.log"))
    {
      printf ("statup proceess fialed");
      return -1;
    }
  cout << "ip: " << ip << endl;
  if (EW_OK != cnc_allclibhndl3 (ip.c_str (), 8193, 5, &h))	/// Allocate the library handle(for Ethernet connection)
    {
      printf ("Connect Failed!\n");
      system ("Pause");
      return -1;
    }

  cout << "machine: " << ip << endl;

  ODBST statinfo;
  memset ((void *) &statinfo, 0, sizeof (statinfo));
  ret = cnc_statinfo (h, &statinfo);

  if (EW_OK == ret)
    {
      cout << "AUTOMATIC/MANUAL mode selection \t:" << statinfo.aut << endl;
      cout << "Status of automatic operation \t\t: " << statinfo.run << endl;
      cout << "Status of axis movement, dwell \t\t: " << statinfo.motion <<
	endl;
      cout << "Status of M,S,T,B function \t\t: " << statinfo.mstb << endl;
      cout << "Status of alarm \t\t: " << statinfo.alarm << endl;
      cout << "Status of program editing \t\t: " << statinfo.edit << endl;
    }

  ODBSYS sysinfo;
  memset ((void *) &sysinfo, 0, sizeof (sysinfo));
  ret = cnc_sysinfo (h, &sysinfo);
  if (EW_OK == ret)
    {
      const int STR_MAX = 32;
      char cnc_type[STR_MAX] = { 0 };
      char mt_type[STR_MAX] = { 0 };
      char series[STR_MAX] = { 0 };
      char version[STR_MAX] = { 0 };
      memcpy (cnc_type, sysinfo.cnc_type, 2 * sizeof (char));
      memcpy (mt_type, sysinfo.mt_type, 2 * sizeof (char));
      memcpy (series, sysinfo.series, 4 * sizeof (char));
      memcpy (version, sysinfo.version, 4 * sizeof (char));
      cout << "cnc type:" << cnc_type << endl;
      cout << "mt type\t:" << mt_type << endl;
      cout << "series\t:" << series << endl;
      cout << "version\t:" << version << endl;
    }

  cout << "alarm===================" << endl;
  {
    ODBALMMSG msg[10];
    short rnum = 10;

    bool isOk = false;
    if (cnc_rdalmmsg ((unsigned short) h, -1, &rnum, msg) == EW_OK)
      {
	for (short i = 0; i < rnum; i++)
	  {
	    msg[i].alm_msg[31] = '\0';
	    cout << "ID_" << i << ": " << msg[i].alm_no << ", type: " <<
	      msg[i].type << endl;
	    cout << "Msg: " << msg[i].alm_msg << endl;
	    HexDump (msg[i].alm_msg, msg[i].msg_len, 0);
	  }
	isOk = true;
      }
  }

  cout << "test prog............." << endl;

  char ORG_NAME[] = "0221";
  char *prog_name = ORG_NAME;
  if (argc > 2)
    prog_name = argv[2];
  short code = cnc_delete (h, atoi (prog_name));
  printf ("Delete file: %s, code: %d\n", prog_name, code);
  code = DownloadFile (h, prog_name);
  printf ("download file: %s, code: %d\n", prog_name, code);
  string temp = prog_name;
  temp += ".nc";
  code = UploadFile (h, atoi (prog_name), temp.c_str ());
  printf ("upload file: %s, code: %d\n", prog_name, code);
  cnc_freelibhndl (h);


  cout << "end ..............:" << endl;
  return 0;
}

short
UploadFile (USHORT h, short progID, const char *filePath)
{
  short code = -1;
  if (h > 0)
    {
      FILE *outfile = NULL;
      outfile = fopen (filePath, "wb");
      if (outfile != NULL)
	{
	  if (cnc_upstart3 (h, 0, progID, progID) == EW_OK)
	    {
	      long len = MAXLEN;
	      char buf[MAXLEN + 1];

	      do
		{
		  len = MAXLEN;
		  code = cnc_upload3 (h, &len, buf);
		  if (code == EW_BUFFER)
		    {
		      continue;
		    }
		  if (code == EW_OK)
		    {
		      buf[len] = '\0';
		      fwrite (buf, sizeof (char), len, outfile);
		    }
		  if (buf[len - 1] == '%')
		    {
		      break;
		    }
		}
	      while ((code == EW_OK) || (code == EW_BUFFER));
	      code = cnc_upend3 (h);
	    }
	  fclose (outfile);
	}
    }
  return code;
}


short
DownloadFile (USHORT h, const char *filepath)
{
  short ret = -1;
  if (h > 0)
    {
      FILE *readFile = fopen (filepath, "rb");
      if (readFile != NULL)
	{
	  if (cnc_dwnstart3 (h, 0) == EW_OK)
	    {
	      long rc;
	      unsigned char buf[MAXLEN] = { 0 };
	      while (true)
		{
		  rc = fread (buf, sizeof (unsigned char), MAXLEN, readFile);
		  if (rc <= 0)
		    {
		      break;
		    }

		  do
		    {
		      ret = cnc_download3 (h, &rc, (char *) buf);
		    }
		  while (ret == EW_BUFFER);

		  if (ret != EW_OK)
		    {
		      break;
		    }
		}
	      ret = cnc_dwnend3 ((short) h);
	    }
	  fclose (readFile);
	}
    }
  return ret;
}


void
HexDump (char *buf, int len, int addr)
{
  int i, j, k;
  char binstr[80];

  for (i = 0; i < len; i++)
    {
      if (0 == (i % 16))
	{
	  sprintf (binstr, "%08x -", i + addr);
	  sprintf (binstr, "%s %02x", binstr, (unsigned char) buf[i]);
	}
      else if (15 == (i % 16))
	{
	  sprintf (binstr, "%s %02x", binstr, (unsigned char) buf[i]);
	  sprintf (binstr, "%s  ", binstr);
	  for (j = i - 15; j <= i; j++)
	    {
	      sprintf (binstr, "%s%c", binstr,
		       ('!' < buf[j] && buf[j] <= '~') ? buf[j] : '.');
	    }
	  printf ("%s\n", binstr);
	}
      else
	{
	  sprintf (binstr, "%s %02x", binstr, (unsigned char) buf[i]);
	}
    }
  if (0 != (i % 16))
    {
      k = 16 - (i % 16);
      for (j = 0; j < k; j++)
	{
	  sprintf (binstr, "%s   ", binstr);
	}
      sprintf (binstr, "%s  ", binstr);
      k = 16 - k;
      for (j = i - k; j < i; j++)
	{
	  sprintf (binstr, "%s%c", binstr,
		   ('!' < buf[j] && buf[j] <= '~') ? buf[j] : '.');
	}
      printf ("%s\n", binstr);
    }
}

到此就完成了所有工作

欢迎关注交流共同进步
奔跑阿甘
博客地址:wqliceman.top

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
Fanuc机床数据采集是指通过对Fanuc机床的各项数据进行收集和记录。Fanuc机床是一种先进的数控机床,具有高精度、高效率和高可靠性等特点。数据采集可以通过Fanuc机床的控制系统进行,也可以通过外部设备与机床进行连接进行数据采集Fanuc机床数据采集可以获取机床运行状态、工件加工过程中的各种数据信息,如加工速度、进给速度、刀具负载、温度等。这些数据可以被用来监控机床的运行状况,及时发现异常情况,并采取相应的措施,避免机床故障和加工质量问题的发生。 Fanuc机床数据采集还可以用于生产计划和生产管理。通过对机床的数据进行分析和统计,可以得到机床的运行时间、开机率、产量等相关数据,为生产计划和生产线的优化提供参考依据。通过对数据的分析,还可以找出机床加工过程中的瓶颈、问题以及改进方向,提高生产效率和产品质量。 同时,Fanuc机床数据采集也可以实现对机床的远程监控和管理。通过远程监控系统,可以实时接收和分析机床的各项数据,以及远程控制机床的操作。这种方式可以提高对机床的管理和维护效率,减少机床的故障率和维修时间。 综上所述,Fanuc机床数据采集对于机床运行状态的监控、生产计划和生产管理的优化以及远程监控和管理都具有重要意义。通过有效地采集和分析机床的各项数据,可以提高机床的运行效率,降低故障风险,同时为企业的生产管理和决策提供重要依据。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iot-genius

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值