Reputation: 53
I have a weird problem with my Connection from C# to my Simatic S7-1200. I am using LibNoDave to connect and i want to set bits of my SPS with the C#-program. This is working just fine but it only works 9 times and then the writeBytes(..) function returns -1025 and not 0 as it should and it is not setting the byte anymore. I then have to restart the application and it will work 9 times again.
This is my LibNoDave class.
static class LibNoDave
{
static libnodave.daveConnection dc;
static bool connection = false;
public static bool CreateConnection()
{
const string IP = "192.168.1.250";
const int Rack = 0;
const int Slot = 0;
libnodave.daveOSserialType fds;
libnodave.daveInterface di;
try
{
fds.rfd = libnodave.openSocket(102, IP);
fds.wfd = fds.rfd;
if (fds.rfd > 0)
{
di = new libnodave.daveInterface(fds, "IF1",
0, libnodave.daveProtoISOTCP,
libnodave.daveSpeed187k);
di.setTimeout(1000000);
int res = di.initAdapter();
if (res == 0)
{
dc = new libnodave.daveConnection(di, 0,
Rack, Slot);
res = dc.connectPLC();
if (res == 0)
{
connection = true;
return true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
public static void DisconnectPLC()
{
if (connection)
{
dc.disconnectPLC();
libnodave.closeSocket(102);
connection = false;
}
}
public static void WritePLC(int anfangswert, byte wert)
{
if (connection)
{
byte[] buf = new Byte[1];
buf[0] = wert;
int res = dc.writeBytes(libnodave.daveFlags, 0, anfangswert, 1, buf);
MessageBox.Show(res.ToString());
}
}
}
Here I am calling the functions:
private void frmAuslagerung_Load(object sender, EventArgs e)
{
if (!LibNoDave.CreateConnection())
{
finished = true;
this.Close();
}
LibNoDave.WritePLC(1, Byte.Parse(auto.Position.Lade.ToString()));
}
private void frmAuslagerung_FormClosing(object sender, FormClosingEventArgs e)
{
if (!finished)
{
//e.Cancel = true; //Not in code for testing
LibNoDave.DisconnectPLC(); //Only for testing
}
else
LibNoDave.DisconnectPLC();
}
Upvotes: 1
Views: 1829
Reputation: 36
It's old, but for others to find: in my case it was the declaration of fds and di: they had to be class members and not local to the connect method, or the timeout value was being lost later on.
Upvotes: 0
Reputation: 1
it can be problem closing the connection try with:
public static void DisconnectPLC()
{
if (connection)
{
dc.disconnectPLC();
di.disconnectAdapter();
libnodave.closePort(fds.rfd);
connection = false;
}
}
Upvotes: 0
Reputation: 1892
I don´t see any problem in code and I don´t know neither your device nor LibNoDave, but the problem may be related to:
1- Is some receiver an "One single digit" (like 0 to 9) address or container ?
2- The position is being truly reseted as I see in WritePLC function?
3- The "LibNoDave.WritePLC" is incrementing its pointer?
Good luck.
Upvotes: 0