引用
引用第8楼flyfeky于2021-01-12 15:13发表的 :
你好。 我目前也在做此项目。 倍福的ADS通信 . 批量的读写数据(几千个变量),有什么思路吗 盼回。
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TwinCAT.Ads;//twincat安装目录中有
using System.IO;
namespace HmiAdsTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public static TcAdsClient tcAdsClient;//建立通讯
private int handle;
private void button1_Click(object sender, EventArgs e)
{
tcAdsClient = new TcAdsClient();//实例化
//tcAdsClient.Connect(851);//同一台机器上使用
tcAdsClient.Connect("169.254.71.120.1.1", 851);//amsNetID 851是twincat3 端口号
try
{
//handle = tcAdsClient.CreateVariableHandle("IOs.ConnectSucceed");
handle = tcAdsClient.CreateVariableHandle("IOs.Dog");
MainForm.VariableAssignment.ConnectSucceed = (bool)(tcAdsClient.ReadAny(handle, typeof(bool)));
tcAdsClient.DeleteVariableHandle(handle);
}
catch
{
MainForm.VariableAssignment.ConnectSucceed = false;
MessageBox.Show("未能连接网络");
}
if (MainForm.VariableAssignment.ConnectSucceed)
{
MessageBox.Show("成功连接网络");
button1.BackColor = Color.Green;
}
else { timerReadPeriod.Enabled = false; }
}
public class VariableAssignment
{
public static bool ConnectSucceed;
public static int AO_ProportionalValve2Dint;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
handle = tcAdsClient.CreateVariableHandle("IOs.AO_ProportionalValve2Dint");
tcAdsClient.WriteAny(handle, (int)numericUpDown1.Value);
tcAdsClient.DeleteVariableHandle(handle);
}
}
}
这个答案你满意吗