-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I read ASCII data? #1
Comments
I was able to get it in the following way. PLCData<short> devices = new(PlcDeviceType.W, 0x100, 256);
devices.ReadData();
StringBuilder sb = new();
for (int i = 0; i < 160; i++)
{
string str = ASCIIEncoding.ASCII.GetString(Convert.FromHexString(devices[i].ToString("X2")));
if (str == "\0") break;
sb.Append(str);
}
sb.ToString(); Is there a smarter way or an official way? |
支持直接转的类型有:Boolean,Int32,Int16,UInt16,UInt32,Single,Char //< v2.1.6 |
Thank you for your response. However, since the byte order is reversed, it cannot be converted into a string as is. Therefore, it was necessary to change the byte order as shown below and convert it to a string. //It is necessary to loop to read all character strings, but it is omitted.
Encoding.ASCII.GetString(Convert.FromHexString(BinaryPrimitives.ReadInt16LittleEndian(bytes.AsSpan(intStart, intLength)).ToString("X2"))) AppendixI was able to get it in the following way. PLCData<short> devices = new(PlcDeviceType.W, 0x100, 256);
devices.ReadData();
byte[] devicesBytes = devices.Bytes;
byte[] cropBytes = bytes.AsSpan(0 * 2, 160 * 2).ToArray();
byte[] trimdBytes = cropBytes.TakeWhile(x => x != 0).ToArray();
byte[] sortedBytes = new byte[trimdBytes.Length];
for (int i = 0; i < sortedBytes.Length; i += 2)
{
if (i == sortedBytes.Length -1)
{
sortedBytes[i] = trimdBytes[i];
break;
}
sortedBytes[i] = trimdBytes[i + 1];
sortedBytes[i + 1] = trimdBytes[i];
}
Encoding.ASCII.GetString(sortedBytes); |
高低位转换需要自己转换,不是很难。不提供的原因是一般都是为ABCDE这样的,你自己转换就行了。
|
Thank you. I will refer to it. |
string 和 int bool .... 不同,你可以自己写一个类来完成自己的特殊化需求。 Please provide your code for my reference. |
I tried with below code.
However, I get "McProtocol.PLCData`1[System.Char]".
How can I get the string?
The text was updated successfully, but these errors were encountered: