-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathDataBlockValueSaver.cs
84 lines (70 loc) · 2.8 KB
/
DataBlockValueSaver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotNetSiemensPLCToolBoxLibrary.Communication;
using DotNetSiemensPLCToolBoxLibrary.DataTypes;
using DotNetSiemensPLCToolBoxLibrary.PLCs.S7_xxx.MC7;
namespace ToolboxForSiemensPLCs
{
public partial class DataBlockValueSaver : Form
{
private string ConnectionName = "";
private PLCConnection myConn;
public DataBlockValueSaver(string ConnectionName)
{
this.ConnectionName = ConnectionName;
InitializeComponent();
myConn = new PLCConnection(ConnectionName);
}
private void button1_Click(object sender, EventArgs e)
{
var lst = myConn.PLCListBlocks(PLCBlockType.DB);
listBox1.Items.AddRange(lst.ToArray());
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog saveDataDir=new FolderBrowserDialog();
if (saveDataDir.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (string db in listBox1.SelectedItems)
{
var size = myConn.PLCGetDataBlockSize(db);
var Tag = new PLCTag(db + ".DBX0.0");
Tag.TagDataType = TagDataType.ByteArray;
Tag.ArraySize = size;
myConn.ReadValue(Tag);
BinaryWriter wrt = new BinaryWriter(File.Open(Path.Combine(saveDataDir.SelectedPath, db + ".data"), FileMode.Create));
wrt.Write((byte[])Tag.Value);
wrt.Close();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog dlg=new OpenFileDialog();
dlg.Filter = "*.data|*.data";
dlg.Multiselect = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (var fileName in dlg.FileNames)
{
var rd = new BinaryReader(File.Open(fileName, FileMode.Open));
var bytes = rd.ReadBytes(Convert.ToInt32(rd.BaseStream.Length));
var fn = Path.GetFileName(fileName);
fn = fn.Substring(0, fn.Length - 5);
var Tag = new PLCTag(fn + ".DBX0.0");
Tag.TagDataType = TagDataType.ByteArray;
Tag.ArraySize = bytes.Length;
Tag.Controlvalue = bytes;
myConn.WriteValue(Tag);
}
}
}
}
}