基本信息
源码名称:C# 通过网络和三菱的PLC通信
源码大小:0.06M
文件格式:.rar
开发语言:C#
更新时间:2016-12-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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;
namespace PLCTest2
{
public partial class Form1 : Form
{
private AxActUtlTypeLib.AxActUtlType axActUtlType = null;
public Form1()
{
InitializeComponent();
this.axActUtlType = new AxActUtlTypeLib.AxActUtlType();
//((System.ComponentModel.ISupportInitialize)(this.axActUtlType)).BeginInit();
this.Controls.Add(this.axActUtlType); //貌似这个控件必须要加到Controls中,不然就会引发异常。
}
private void btnConn_Click(object sender, EventArgs e)
{
try
{
int iStation = Convert.ToInt32(this.txtStationNo.Text.Trim());
this.axActUtlType.ActLogicalStationNumber = iStation;
this.axActUtlType.ActPassword = this.txtPassword.Text.Trim();
int rtn = this.axActUtlType.Open();
if (rtn == 0)
{
ShowMsg("连接成功!");
this.txtStationNo.Enabled = false;
this.txtPassword.Enabled = false;
this.btnConn.Enabled = false;
}
else
{
ShowMsg("连接失败");
}
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
private void ShowMsg(string msg)
{
string str = string.Format(DateTime.Now.ToString("HH:mm:ss") "_" msg);
this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
this.richTextBox1.SelectedText = (str Environment.NewLine);
this.richTextBox1.ScrollToCaret();
}
private void btnRead_Click(object sender, EventArgs e)
{
try
{
string strAddr = this.txtAddr.Text.Trim();
int num = Convert.ToInt32(this.txtNum.Text.Trim());
short[] arr = new short[num];
int rtn = this.axActUtlType.ReadDeviceBlock2(strAddr, num, out arr[0]);
if (rtn == 0)
{
ShowMsg("读取数据成功!");
for (int i = 0; i < arr.Length; i )
{
ShowMsg(string.Format("{0:X4}", arr[i]));
}
}
else
{
ShowMsg("读取数据失败");
}
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
private void btnWrite_Click(object sender, EventArgs e)
{
try
{
string strAddr = this.txtAddr.Text.Trim();
string[] strData = this.txtData.Lines;
int num = strData.Length;
short[] arr = new short[num];
for (int i = 0; i < num; i )
{
arr[i] = Convert.ToInt16(strData[i]);
}
int rtn = this.axActUtlType.WriteDeviceBlock2(strAddr, num, ref arr[0]);
if (rtn == 0)
{
ShowMsg("写入数据成功!");
this.txtNum.Text = num.ToString();
}
else
{
ShowMsg("写入数据失败");
}
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
}
}