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 USBtoPLC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
connetti();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private bool initSuccess = false;
static libnodave.daveOSserialType fds;
static libnodave.daveInterface di;
static libnodave.daveConnection dc;
static int localMPI = 0;
static int plcMPI = 2;
static string s7online = "S7ONLINE";
private int connetti()
{
int i, a = 0, j, res, b = 0, c = 0;
float d = 0;
byte[] buf1 = new byte[libnodave.davePartnerListSize];
fds.rfd = libnodave.openS7online(s7online);
fds.wfd = fds.rfd;
if (fds.rfd >= 0)
{
di = new libnodave.daveInterface(fds, "IF1", localMPI, libnodave.daveProtoS7online, libnodave.daveSpeed187k);
di.setTimeout(1000000);
for (i = 0; i < 3; i++)
{
if (0 == di.initAdapter())
{
initSuccess = true;
a = di.listReachablePartners(buf1);
MessageBox.Show("daveListReachablePartners List length: " + a);
if (a > 0)
{
for (j = 0; j < a; j++)
{
if (buf1[j] == libnodave.daveMPIReachable)
MessageBox.Show("Device at address: " + j);
}
}
label1.Text = "Bağlantı Açık";
break;
}
else di.disconnectAdapter();
}
if (!initSuccess)
{
MessageBox.Show("Couldn't connect to Adapter!.\n Please try again. You may also try the option -2 for some adapters.");
return -3;
}
dc = new libnodave.daveConnection(di, plcMPI, 0, 0);
return dc.connectPLC();
}
else
{
MessageBox.Show("Couldn't open s7 online ");
return -1;
}
}
private void button1_Click(object sender, EventArgs e)
{
byte[] array1 = new byte[1];
array1[0] = 1;
// dc.writeBits(libnodave.daveFlags, 0, 8, 1, array1); //q1.0 Start biti 0 ise M0 8 ise M1 16 ise M2
// dc.writeBits(libnodave.daveOutputs, 0, 0, 1, array1); // Q0.0
// dc.writeBits(libnodave.daveOutputs, 0, 8, 1, array1); // Q1.0
// dc.writeBits(libnodave.daveOutputs, 0, 10, 1, array1); // q1.1
dc.writeBits(libnodave.daveOutputs, 0, 12, 1, array1); // q1.3
}
private void button2_Click(object sender, EventArgs e)
{
byte[] bArr = new byte[0];
// dc.writeBits(libnodave.daveFlags, 0, 8, 1, bArr);
// dc.writeBits(libnodave.daveOutputs, 0, 8, 1, bArr); //m1.0
dc.writeBits(libnodave.daveOutputs, 0, 8, 1, bArr);
}
private void button3_Click(object sender, EventArgs e)
{
di.disconnectAdapter();
libnodave.closeS7online(fds.rfd);
GC.Collect();
GC.WaitForPendingFinalizers();
label1.Text = "Bağlantı Kapalı";
}
private void button4_Click(object sender, EventArgs e)
{
connetti();
}
}
}
//------------------------------------------------