因为项目需要用到发那科机器的一些数据,在里面踩了无数的坑,终于写好了一个可以满足目前项目需求的程序。
回想起来真是心酸都不足以形容。。
先上一个采集端的界面,界面是很丑陋,但是胜在简单。:

机器的配置信息我都是写进数据库的,每次开启程序之前都要先查询一次数据库,然后再连接CNC机器一次,以获取相应的机器连接情况。这个也只是方便用户查看而已,没什么实际的作用,因为在这里获取的句柄在采集程序里面是用不了的。
原则上,我是没有用到全局变量,因为使用全局变量有很多问题需要考虑,什么全局加锁啦,什么的。反正我是不懂。
先上一个界面的代码:
先说一下数据库架构。
1:第一张表用来保存所有数据,方便以后统计停机时间什么的。
2:第二张表,只做更新数据,用来查询机器的当前运行状态。
所以我在程序了分了两个方法。
/***************************************************************
* date:2018-11-30
* By:Bevan
* * sappmis@163.com
* * 说明:IP,机号添加面板。主要实现多线程开启采集功能。
* *
* ***************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
namespace SmachCNC
{
public partial class Form1 : Form
{
public int sleepTime;
private List<Thread> threadPool = new List<Thread>(); //创建第一个线程池,用来写数据库
private List<Thread> threadPool2 = new List<Thread>(); //创建第二个线程池,用来更新实时数据
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ip_show(DGVip);
ChangLabStatue();
txtip1.Enabled = false;
txtmachno1.Enabled = false;
txtmodel.Enabled = false;
txtname.Enabled = false;
version.Enabled = false;
}
/// <summary>
/// 将采集方法实例化,以方便多线程调用。我采用的是委托方法,使用多线程开启采集程序,所以需要先将采集方法实例化,以方便给线程传递参数。
/// </summary>
public class IndataCnc
{
private int sleeptime;
private string IP;
private string machno;
public IndataCnc(string ip, string machno, int sleep) //这个方法是LinkCnc();采集方法中需要的参数。这里一定要处理好,如果看不懂这个代码,建议多看看多线程方面的文章。或者搜索“”c#多线程调用有参数方法“”。
{
this.machno = machno;
this.IP = ip;
this.sleeptime = sleep;
}
public void funRun()
{
LinkCnc Lcnc = new LinkCnc();
Lcnc.readCncToData(IP, machno, sleeptime);
}
}
/// <summary>
/// 使用实例化类给方法传参数。另一个方法。
/// </summary>
public class UpdataCnc
{
private string IP;
private string machno;
public UpdataCnc(string ip, string machno)
{
this.machno = machno;
this.IP = ip;
}
public void funRun()
{
ReadCnc cnc = new ReadCnc();
cnc.readCncUpData(IP, machno);
}
}
/// <summary>
/// 改变程序运行状态提示
/// </summary>
public void ChangLabStatue()
{
if (button2.Text == "启动程序")
{
labShowInfo1.Text = "程序已停止";
labShowInfo1.ForeColor = Color.Red;
}
else
{
labShowInfo1.Text = "程序正在运行....";
labShowInfo1.ForeColor = Color.Green;
}
}
/// <summary>
/// 将数据库中的机器信息带出面板。
/// </summary>
public void ip_show(DataGridView dgvw)
{
try
{
this.Invoke(new EventHandler(delegate
{
string sql = "select * from machinfo order by machno";
DataSet ds = new DbHelp().QueryDataset(sql);
dsNum = ds.Tables[0].Rows.Count;
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dgvw.Rows.Add();
dgvw.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["machno"];
dgvw.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["ip"];
dgvw.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["port"];
}
}
}
));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 判断机器是否连接成功
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public string getState(ushort num)
{
if (num > 0)
{
return "连接成功";
}
else return "机器离线";
}
/// <summary>
/// 连接机器取回句柄号
/// </summary>
public void GetCncHandle()
{
DGVip.DataSource = null;
string sql = "select * from machinfo";
DataSet ds = new DbHelp().QueryDataset(sql);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string Ip;
ushort port;
string statue;
ushort fwlibHandle = 0;
Ip = ds.Tables[0].Rows[i]["ip"].ToString();
port = Convert.ToUInt16(ds.Tables[0].Rows[i]["port"].ToString());
short ret = Focas1.cnc_allclibhndl3((object)Ip, port, 5, out fwlibHandle);
statue = getState(fwlibHandle);
DGVip.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i]["machno"];
DGVip.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i]["ip"];
DGVip.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i]["port"];
DGVip.Rows[i].Cells[3].Value = statue;
DGVip.Rows[i].Cells[4].Value = fwlibHandle;
}
}
/// <summary>
/// 开启线程,循环将IP读出并传递给DLL
/// </summary>
public void startThreadPool()
{
if (txtsleeptim

本文分享了发那科CNC机器数据采集的实战经验,包括界面设计、数据库架构、多线程采集和更新数据的方法。作者通过具体代码示例,详细介绍了如何连接CNC机器、获取数据及写入数据库的过程。
&spm=1001.2101.3001.5002&articleId=86015007&d=1&t=3&u=9c93eadb7b474677a0a1b8c526e366a1)
679

被折叠的 条评论
为什么被折叠?



