Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1.  
  2. public static string sendText(byte start, byte cmd, byte mod, String datatekst)
  3. {
  4. String hex;
  5.  
  6. int lendatatekst = datatekst.Length + 1;
  7.  
  8. char[] chardatatekst = new char[lendatatekst];
  9.  
  10. hex = start.ToString("X2") + lendatatekst.ToString("X2") + cmd.ToString("X2") + mod.ToString("X2");
  11.  
  12. short suma = Convert.ToInt16(lendatatekst);
  13. suma += cmd;
  14. suma += mod;
  15.  
  16.  
  17. int i = 0; int a = 0; int b = 0;
  18. foreach (char s in datatekst)
  19. {
  20. chardatatekst[i] = (s);
  21. i++;
  22.  
  23. string ascii = Convert.ToByte(s).ToString("X2");
  24. hex = hex + ascii;
  25. suma += Convert.ToInt16(s);
  26. }
  27.  
  28. a = (suma & 0xff);
  29. b = ((suma >> 8) & 0xff);
  30.  
  31. String MlodszyA = a.ToString("X2");
  32. String StarszyB = b.ToString("X2");
  33.  
  34. hex += MlodszyA + StarszyB;
  35. return hex;
  36. }
  37.  
  38. public static string sendHex(byte start, byte cmd, byte mod, String hexValues)
  39. {
  40. try
  41. {
  42.  
  43. string[] hexValuesSplit = hexValues.Split(' ');
  44.  
  45. int lenhexValuesSplit = hexValuesSplit.Length + 1;
  46.  
  47. Byte[] data = new Byte[lenhexValuesSplit + 3];
  48.  
  49. data[0] = start;
  50. data[1] = Convert.ToByte(lenhexValuesSplit);
  51. data[2] = cmd;
  52. data[3] = mod;
  53.  
  54. int a = 0; int b = 0;
  55. int suma = cmd + lenhexValuesSplit + mod;
  56. int i = 4;
  57.  
  58. foreach (String hex in hexValuesSplit)
  59. {
  60.  
  61. data[i] = byte.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
  62.  
  63.  
  64. suma += data[i];
  65. i++;
  66.  
  67. }
  68.  
  69. a = (suma & 0xff);
  70. b = ((suma >> 8) & 0xff);
  71.  
  72. string hexsend = BitConverter.ToString(data).Replace("-", string.Empty);
  73.  
  74. String MlodszyA = a.ToString("X2");
  75. String StarszyB = b.ToString("X2");
  76.  
  77. hexsend += MlodszyA + StarszyB;
  78.  
  79. return hexsend;
  80. }
  81. catch (OverflowException)
  82. {
  83. MessageBox.Show("Niepoprawnie wpisane dane \n Wpisz 'XX XX XX...'", "Błąd typu OverflowException ",
  84. MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
  85. return "";
  86. }
  87. catch (FormatException)
  88. {
  89. MessageBox.Show("Niepoprawne wpisane dane", "Błąd typu FormatException",
  90. MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
  91. return "";
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement