I'm trying to read String data from TwinCAT PLC using WinForm and ADS.NET. I based my code from their sample codes. Writing the string into the console shows my data as: "123456????????????????????????". I can use a textbox and it will show and save "123456" but I want to save it directly without using textboxes.
What am I doing wrong? Or is there something missing from my code?
Thanks!
Additional info:
(I'm trying to save the data into a DB):
Code:
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;
}
}