我正在编写一个小型c#应用程序,它将一些数据读写到S7-300PLC的DB内存中。我正在使用PLCSim模拟器和DotNetSiemensPLCToolBoxLibrary来执行一些测试。正如您所知道的,DotNetSiemensPLCToolBoxLibrary是一个在libnodave之上的层,所以我经常直接使用libnodave。我可以连接成功的PLC,我可以写/读输入,merker和字符串。但是当我试图写/读一个结构时,我会遇到一些问题。下面是在plc中编写结构的代码:
//PLCTagGeneric
PLCTag<TestStruct> tst = new PLCTag<TestStruct>() { DataBlockNumber = 1, ByteAddress = 0 };
tmpConn.ReadValue(tst);
TestStruct read = tst.GenericValue;
TestStruct wrt = new TestStruct();
wrt.bb = 1;
wrt.cc = true;
wrt.ee = 14;
wrt.test = "Bin da!";
tst.Controlvalue = wrt;
tmpConn.WriteValue(tst);这是阅读的代码:
PLCTag<TestStruct> tst = new PLCTag<TestStruct>() { DataBlockNumber = 1, ByteAddress = 0 };
tmpConn.ReadValue(tst);
byte[] buf = new byte[18];
int res = tmpConn._dc.readBytes(libnodave.daveDB, 1, 0, 18, buf);
tst._readValueFromBuffer(buf, 0);
TestStruct t = tst.GenericValue; 这是一个结构:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct TestStruct
{
public SByte bb;
public Boolean cc;
public UInt32 ee;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string test;
}阅读的输出是:
bb: 0
ee: 920583
cc: false
test: n da!为什么?我需要帮助。谢谢
发布于 2014-08-04 19:47:12
解决了。在PLC中,数据只是字节流。因此,如果您写入一个序列的in 16\ in 32\x字符串,您必须按照相同的顺序读取,否则将导致解析字节中的错误。希望这能有所帮助
https://stackoverflow.com/questions/25106456
复制相似问题