Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string sendText(byte start, byte cmd, byte mod, String datatekst)
- {
- String hex;
- int lendatatekst = datatekst.Length + 1;
- char[] chardatatekst = new char[lendatatekst];
- hex = start.ToString("X2") + lendatatekst.ToString("X2") + cmd.ToString("X2") + mod.ToString("X2");
- short suma = Convert.ToInt16(lendatatekst);
- suma += cmd;
- suma += mod;
- int i = 0; int a = 0; int b = 0;
- foreach (char s in datatekst)
- {
- chardatatekst[i] = (s);
- i++;
- string ascii = Convert.ToByte(s).ToString("X2");
- hex = hex + ascii;
- suma += Convert.ToInt16(s);
- }
- a = (suma & 0xff);
- b = ((suma >> 8) & 0xff);
- String MlodszyA = a.ToString("X2");
- String StarszyB = b.ToString("X2");
- hex += MlodszyA + StarszyB;
- return hex;
- }
- public static string sendHex(byte start, byte cmd, byte mod, String hexValues)
- {
- try
- {
- string[] hexValuesSplit = hexValues.Split(' ');
- int lenhexValuesSplit = hexValuesSplit.Length + 1;
- Byte[] data = new Byte[lenhexValuesSplit + 3];
- data[0] = start;
- data[1] = Convert.ToByte(lenhexValuesSplit);
- data[2] = cmd;
- data[3] = mod;
- int a = 0; int b = 0;
- int suma = cmd + lenhexValuesSplit + mod;
- int i = 4;
- foreach (String hex in hexValuesSplit)
- {
- data[i] = byte.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
- suma += data[i];
- i++;
- }
- a = (suma & 0xff);
- b = ((suma >> 8) & 0xff);
- string hexsend = BitConverter.ToString(data).Replace("-", string.Empty);
- String MlodszyA = a.ToString("X2");
- String StarszyB = b.ToString("X2");
- hexsend += MlodszyA + StarszyB;
- return hexsend;
- }
- catch (OverflowException)
- {
- MessageBox.Show("Niepoprawnie wpisane dane \n Wpisz 'XX XX XX...'", "Błąd typu OverflowException ",
- MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
- return "";
- }
- catch (FormatException)
- {
- MessageBox.Show("Niepoprawne wpisane dane", "Błąd typu FormatException",
- MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement