使用ADS.NET从TwinCAT PLC读取字符串会返回????数据后

时间:2019-11-18 08:32:52

标签: c# winforms plc twincat-ads-.net

我正在尝试使用WinForm和ADS.NET从TwinCAT PLC读取字符串数据。我基于their sample codes的代码。 将字符串写入控制台会将我的数据显示为:“ 123456 ???????????????????????????”。 我可以使用文本框,它将显示并保存“ 123456”,但我想直接保存而不使用文本框。

我在做什么错?还是我的代码中缺少什么?

谢谢!

其他信息:

(我正在尝试将数据保存到数据库中):

Error message

代码:

private String readPLCData(String variableHandle)
        {
            int length;
            string text;
            AdsStream dataStream;
            BinaryReader reader;
            try
            {
                varHandle = adsClient.CreateVariableHandle(variableHandle);
                // length of the stream = length of string in sps + 1
                dataStream = new AdsStream(31);
                reader = new BinaryReader(dataStream, System.Text.Encoding.ASCII);
                length = adsClient.Read(varHandle, dataStream);
                text = new string(reader.ReadChars(length));
                return text;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return null;
            }
        }

0 个答案:

没有答案