Conversion.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Globalization;
  3. namespace S7.Net
  4. {
  5. /// <summary>
  6. /// Conversion methods to convert from Siemens numeric format to C# and back
  7. /// </summary>
  8. public static class Conversion
  9. {
  10. /// <summary>
  11. /// Converts a binary string to Int32 value
  12. /// </summary>
  13. /// <param name="txt"></param>
  14. /// <returns></returns>
  15. public static int BinStringToInt32(this string txt)
  16. {
  17. int ret = 0;
  18. for (int i = 0; i < txt.Length; i++)
  19. {
  20. ret = (ret << 1) | ((txt[i] == '1') ? 1 : 0);
  21. }
  22. return ret;
  23. }
  24. /// <summary>
  25. /// Converts a binary string to a byte. Can return null.
  26. /// </summary>
  27. /// <param name="txt"></param>
  28. /// <returns></returns>
  29. public static byte? BinStringToByte(this string txt)
  30. {
  31. if (txt.Length == 8) return (byte)BinStringToInt32(txt);
  32. return null;
  33. }
  34. /// <summary>
  35. /// Converts the value to a binary string
  36. /// </summary>
  37. /// <param name="value"></param>
  38. /// <returns></returns>
  39. public static string ValToBinString(this object value)
  40. {
  41. int cnt = 0;
  42. int cnt2 = 0;
  43. int x = 0;
  44. string txt = "";
  45. long longValue = 0;
  46. try
  47. {
  48. if (value.GetType().Name.IndexOf("[]") < 0)
  49. {
  50. // ist nur ein Wert
  51. switch (value.GetType().Name)
  52. {
  53. case "Byte":
  54. x = 7;
  55. longValue = (long)((byte)value);
  56. break;
  57. case "Int16":
  58. x = 15;
  59. longValue = (long)((Int16)value);
  60. break;
  61. case "Int32":
  62. x = 31;
  63. longValue = (long)((Int32)value);
  64. break;
  65. case "Int64":
  66. x = 63;
  67. longValue = (long)((Int64)value);
  68. break;
  69. default:
  70. throw new Exception();
  71. }
  72. for (cnt = x; cnt >= 0; cnt += -1)
  73. {
  74. if (((Int64)longValue & (Int64)Math.Pow(2, cnt)) > 0)
  75. txt += "1";
  76. else
  77. txt += "0";
  78. }
  79. }
  80. else
  81. {
  82. // ist ein Array
  83. switch (value.GetType().Name)
  84. {
  85. case "Byte[]":
  86. x = 7;
  87. byte[] ByteArr = (byte[])value;
  88. for (cnt2 = 0; cnt2 <= ByteArr.Length - 1; cnt2++)
  89. {
  90. for (cnt = x; cnt >= 0; cnt += -1)
  91. if ((ByteArr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
  92. }
  93. break;
  94. case "Int16[]":
  95. x = 15;
  96. Int16[] Int16Arr = (Int16[])value;
  97. for (cnt2 = 0; cnt2 <= Int16Arr.Length - 1; cnt2++)
  98. {
  99. for (cnt = x; cnt >= 0; cnt += -1)
  100. if ((Int16Arr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
  101. }
  102. break;
  103. case "Int32[]":
  104. x = 31;
  105. Int32[] Int32Arr = (Int32[])value;
  106. for (cnt2 = 0; cnt2 <= Int32Arr.Length - 1; cnt2++)
  107. {
  108. for (cnt = x; cnt >= 0; cnt += -1)
  109. if ((Int32Arr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
  110. }
  111. break;
  112. case "Int64[]":
  113. x = 63;
  114. byte[] Int64Arr = (byte[])value;
  115. for (cnt2 = 0; cnt2 <= Int64Arr.Length - 1; cnt2++)
  116. {
  117. for (cnt = x; cnt >= 0; cnt += -1)
  118. if ((Int64Arr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
  119. }
  120. break;
  121. default:
  122. throw new Exception();
  123. }
  124. }
  125. return txt;
  126. }
  127. catch
  128. {
  129. return "";
  130. }
  131. }
  132. /// <summary>
  133. /// Helper to get a bit value given a byte and the bit index.
  134. /// <br/>
  135. /// <example>
  136. /// Get the bit at DB1.DBX0.5:
  137. /// <code>
  138. /// byte data = ReadByte("DB1.DBB0");
  139. /// bool bit = data.SelectBit(5);
  140. /// </code>
  141. /// </example>
  142. /// </summary>
  143. /// <param name="data">The data to get from.</param>
  144. /// <param name="index">The zero-based index of the bit to get.</param>
  145. /// <returns>The Boolean value will get.</returns>
  146. public static bool SelectBit(this byte data, int index)
  147. {
  148. int mask = 1 << index;
  149. int result = data & mask;
  150. return (result != 0);
  151. }
  152. /// <summary>
  153. /// Helper to set a bit value to the given byte at the bit index.
  154. /// <br/>
  155. /// <example>
  156. /// Set the bit at index 4:
  157. /// <code>
  158. /// byte data = 0;
  159. /// data.SetBit(4, true);
  160. /// </code>
  161. /// </example>
  162. /// </summary>
  163. /// <param name="data">The data to be modified.</param>
  164. /// <param name="index">The zero-based index of the bit to set.</param>
  165. /// <param name="value">The Boolean value to assign to the bit.</param>
  166. public static void SetBit(this ref byte data, int index, bool value)
  167. {
  168. if ((uint)index > 7)
  169. {
  170. return;
  171. }
  172. if (value)
  173. {
  174. byte mask = (byte)(1 << index);
  175. data |= mask;
  176. }
  177. else
  178. {
  179. byte mask = (byte)~(1 << index);
  180. data &= mask;
  181. }
  182. }
  183. /// <summary>
  184. /// Converts from ushort value to short value; it's used to retrieve negative values from words
  185. /// </summary>
  186. /// <param name="input"></param>
  187. /// <returns></returns>
  188. public static short ConvertToShort(this ushort input)
  189. {
  190. short output;
  191. output = short.Parse(input.ToString("X"), NumberStyles.HexNumber);
  192. return output;
  193. }
  194. /// <summary>
  195. /// Converts from short value to ushort value; it's used to pass negative values to DWs
  196. /// </summary>
  197. /// <param name="input"></param>
  198. /// <returns></returns>
  199. public static ushort ConvertToUshort(this short input)
  200. {
  201. ushort output;
  202. output = ushort.Parse(input.ToString("X"), NumberStyles.HexNumber);
  203. return output;
  204. }
  205. /// <summary>
  206. /// Converts from UInt32 value to Int32 value; it's used to retrieve negative values from DBDs
  207. /// </summary>
  208. /// <param name="input"></param>
  209. /// <returns></returns>
  210. public static Int32 ConvertToInt(this uint input)
  211. {
  212. int output;
  213. output = int.Parse(input.ToString("X"), NumberStyles.HexNumber);
  214. return output;
  215. }
  216. /// <summary>
  217. /// Converts from Int32 value to UInt32 value; it's used to pass negative values to DBDs
  218. /// </summary>
  219. /// <param name="input"></param>
  220. /// <returns></returns>
  221. public static UInt32 ConvertToUInt(this int input)
  222. {
  223. uint output;
  224. output = uint.Parse(input.ToString("X"), NumberStyles.HexNumber);
  225. return output;
  226. }
  227. /// <summary>
  228. /// Converts from float to DWord (DBD)
  229. /// </summary>
  230. /// <param name="input"></param>
  231. /// <returns></returns>
  232. public static UInt32 ConvertToUInt(this float input)
  233. {
  234. uint output;
  235. output = S7.Net.Types.DWord.FromByteArray(S7.Net.Types.Real.ToByteArray(input));
  236. return output;
  237. }
  238. /// <summary>
  239. /// Converts from DWord (DBD) to float
  240. /// </summary>
  241. /// <param name="input"></param>
  242. /// <returns></returns>
  243. public static float ConvertToFloat(this uint input)
  244. {
  245. float output;
  246. output = S7.Net.Types.Real.FromByteArray(S7.Net.Types.DWord.ToByteArray(input));
  247. return output;
  248. }
  249. }
  250. }