我的连接从C#到我的Simatic S7-1200有一个奇怪的问题。我使用LibNoDave连接,我想用C#程序设置我的SPS的位。这工作得很好,但它只能工作9次,然后writeBytes(..)函数返回-1025而不应该是0,它不再设置字节。然后我必须重新启动应用程序,它将再次工作9次。
这是我的LibNoDave类。
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());
}
}
}
我在这里调用函数:
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();
}
我没有看到代码中的任何问题,我不知道你的设备和LibNoDave,但问题可能与:
1-某些接收器是“单个数字”(如0到9)地址还是容器?
2-正如我在WritePLC函数中看到的那样,这个位置正在被重置?
3-“LibNoDave.WritePLC”正在递增其指针?
祝好运。
关闭连接可能有问题尝试:
public static void DisconnectPLC()
{
if (connection)
{
dc.disconnectPLC();
di.disconnectAdapter();
libnodave.closePort(fds.rfd);
connection = false;
}
}
这是旧的,但是对于其他人来说:在我的情况下,它是fds和di的声明:它们必须是类成员而不是connect方法的本地成员,否则超时值将在以后丢失。