C# 尝试使用snap 7从plc s7 1200读取数据,并获取错误的字符串
我试图从数据块(DB60)读取数据,但只得到C# 尝试使用snap 7从plc s7 1200读取数据,并获取错误的字符串,c#,plc,siemens,s7-1200,snap7,C#,Plc,Siemens,S7 1200,Snap7,我试图从数据块(DB60)读取数据,但只得到?5。所以在数据块中应该是JAMES17 private void button1_Click(object sender, EventArgs e) { if (button1.Text == "Connect PLC") { button1.Text = "Disconnect PLC"; ClassPLCS7Client.P
?5
。所以在数据块中应该是JAMES17
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Connect PLC")
{
button1.Text = "Disconnect PLC";
ClassPLCS7Client.PLCClientConnect_Result ConnectResult = new
ClassPLCS7Client.PLCClientConnect_Result();
ConnectResult = PLCClient.Connect(("192.168.0.2"), 0, 1);
if (ConnectResult.State == ClassPLCS7Client.PLCClientConnectState.Connected)
{
this.label1.Text = "Connected PLC1 " + ConnectResult.ReSultString;
label1.ForeColor = Color.Green;
ClassPLCS7Client.ReadDataBlockString_Result read = new ClassPLCS7Client.ReadDataBlockString_Result();
read = PLCClient.ReadDataBlockString(60, 0, 7);
this.textBox1.Text = read.DataValue[0];
//this.textBox1.Text = arr4[];// read.ReSultString;
}
else
{
this.label1.Text = "Fail " + ConnectResult.ReSultString;
label1.ForeColor = Color.Red;
}
}
else
{
button1.Text = "Connect PLC";
disconnect_plc();
this.label1.Text = "Disconnect";
label1.ForeColor = Color.Black;
}
}<code>
从开始,我尝试将DB60更改为字符串“JAMES17” 结果是 我在ReadDataBlockString_类结果中找到了这段代码。所以我调用了这个函数,但不知道如何使用它
public ReadDataBlockString_Result ReadDataBlockString(int DataBlockNumber,
int StartAddress, int LenghtOfRead)
{
ReadDataBlockString_Result rt = new ReadDataBlockString_Result();
rt.MemoryByte = new byte[256];
//if (this.State == PLCClientConnectState.Connected)
//{
rt.DataValue = new string[LenghtOfRead];
int i = 0;
int CaptureIndex = StartAddress;
for (i = 0; i <= LenghtOfRead - 1; i++)
{
rt.ResultCode = PLCClient.DBRead(DataBlockNumber, CaptureIndex, 256, rt.MemoryByte);
CaptureIndex = CaptureIndex + 256;
if (rt.ResultCode == 0)
{
string Convertedvalue = null;
Convertedvalue = System.Text.Encoding.ASCII.GetString(rt.MemoryByte);
rt.DataValue[i] = Convertedvalue;
}
}
//}
//else
//{
// rt.ResultCode = -1;
//}
rt.ReSultString = PLCClient.ErrorText(rt.ResultCode);
return rt;
}
public ReadDataBlockString\u结果ReadDataBlockString(int-DataBlockNumber,
内部起始地址,内部长度起始地址)
{
ReadDataBlockString_结果rt=新的ReadDataBlockString_结果();
rt.MemoryByte=新字节[256];
//if(this.State==PLCClientConnectState.Connected)
//{
rt.DataValue=新字符串[LenghtOfRead];
int i=0;
int CaptureIndex=起始地址;
对于(i=0;iFrom),我尝试将DB60更改为字符串“JAMES17”
结果是
我在类ReadDataBlockString_Result中找到了这段代码。所以我调用了这个函数,但不确定如何使用它
public ReadDataBlockString_Result ReadDataBlockString(int DataBlockNumber,
int StartAddress, int LenghtOfRead)
{
ReadDataBlockString_Result rt = new ReadDataBlockString_Result();
rt.MemoryByte = new byte[256];
//if (this.State == PLCClientConnectState.Connected)
//{
rt.DataValue = new string[LenghtOfRead];
int i = 0;
int CaptureIndex = StartAddress;
for (i = 0; i <= LenghtOfRead - 1; i++)
{
rt.ResultCode = PLCClient.DBRead(DataBlockNumber, CaptureIndex, 256, rt.MemoryByte);
CaptureIndex = CaptureIndex + 256;
if (rt.ResultCode == 0)
{
string Convertedvalue = null;
Convertedvalue = System.Text.Encoding.ASCII.GetString(rt.MemoryByte);
rt.DataValue[i] = Convertedvalue;
}
}
//}
//else
//{
// rt.ResultCode = -1;
//}
rt.ReSultString = PLCClient.ErrorText(rt.ResultCode);
return rt;
}
public ReadDataBlockString\u结果ReadDataBlockString(int-DataBlockNumber,
内部起始地址,内部长度起始地址)
{
ReadDataBlockString_结果rt=新的ReadDataBlockString_结果();
rt.MemoryByte=新字节[256];
//if(this.State==PLCClientConnectState.Connected)
//{
rt.DataValue=新字符串[LenghtOfRead];
int i=0;
int CaptureIndex=起始地址;
对于(i=0;i首先:您将J字符串放在字节偏移量0中,A字符串放在字节偏移量256中,M字符串放在字节偏移量512中等等……您没有一个应该是连续字节的字符串
第二个:当S7存储字符串时,前两个字节被保留,以存储第一个最大字符串大小(以字节为单位)和第二个字符串的实际大小。因此,在您的情况下,您的内存必须包含以下内容:(假设保留内存大小为256字节)
偏移量0==254,偏移量1==7,偏移量2到8='JAMES17'首先:您将J字符串放在字节偏移量0中,A字符串放在字节偏移量256中,M字符串放在字节偏移量512中等等……您没有一个应该是连续字节的字符串
第二个:当S7存储字符串时,前两个字节被保留,以存储第一个最大字符串大小(以字节为单位)和第二个字符串的实际大小。因此,在您的情况下,您的内存必须包含以下内容:(假设保留内存大小为256字节)
偏移量0==254,偏移量1==7,偏移量2到8='JAMES17'在我看到这个问题之前,我从来没有听说过S7.net,但是您是否在DB属性中取消选中“优化块访问”?是的,我已经取消选中了优化块。啊,我知道您做了什么。您已经将每个字符定义为一个字符串。字符串的默认长度为255个字符。那么y你只需要一个条目String“JAMES17”
。我想你甚至可以用一个长度来定义字符串,这样它就会是String[7]“JAMES17”
。我不在工作,所以我不确定。我得到了这个“?a?m?E?S?1?7”.几乎正确,所以我不知道是怎么回事。请帮帮我。@JamesCass-听起来像是在读字符串,当它是一个双字节时,它是一个单字节/字符集,例如UTF-7而不是Unicode。在我看到这个问题之前,我从未听说过S7.net,但你是否取消了DB属性中的“优化块访问”复选框?是的,我没有n已经检查了优化的块。啊,我知道你做了什么。你已经将每个字符定义为一个字符串。一个字符串的默认长度为255个字符。所以你只需要一个条目string“JAMES17”
。我想你甚至可以定义长度为string[7]“JAMES17”的字符串
。我不在工作,所以我不确定。我收到这个“?J?A?m?E?S?1?7”.几乎正确,所以我不知道怎么回事。请帮帮我。@JamesCass-听起来像是在读字符串,当它是双字节时,每个字符集是一个单字节,例如UTF-7而不是Unicode。我认为这不起作用,因为它只是读取单字节,然后将它们转换为ASCII。你需要读取整个字符串一步到位,然后将其转换。您使用的是哪种参考软件包?(S7NetPlus、ezPLC_S7、ITools、西门子或其他产品?)虽然我的帮助有限,因为我的工作中没有C#。好的,您已经在原始帖子中使用了ReadDataBlockString
。您只需将参数更改为ReadDataBlockString(60,0,1)
,假设您的字符串是DB60中的第一个条目(data1string 0.0“JAMES17”
)。如果这不能解决任何问题,那么我不确定该建议什么。哦,谢谢,我只是将plc中的数据更改为char可以解决它。我认为这不起作用,因为它只是读取单个字节,然后将其转换为ASCII。您需要一次性读取整个字符串,然后再转换。您使用的是哪个参考包?(S7NetPlus、ezPLC_S7、ITools.Siemens或其他什么?)虽然我的帮助有限,因为我的工作中没有C#。好的,您已经在原始帖子中使用了ReadDataBlockString
。您只需将参数更改为ReadDataBlockString(60,0,1)
假设您的字符串是DB60中的第一个条目(data1 string 0.0“JAMES17”
)。如果这不能解决任何问题,那么我不知道该建议什么。噢,谢谢,我只是将plc中的数据更改为char,我可以解决它。