abdul hayee

abdul hayee

  • 2k
  • 15
  • 131

ASCII String to HEX

Sep 19 2020 6:38 AM
Hi All
My query is to get Value from user in textbox, then process it and send HEX Values via Serial Port.
for examle value in textbox is 12345A6789BC01234 and i want to send hex of each value through serial port.
for this i made a dummy program, In this program, i use fixed data instead of getting it from textbox.
fix data is in byte[] dataToSend variable and this program is working fine, sending hex format through serial port.
following is the program and output of program
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO.Ports;
  11. using System.IO;
  12. namespace commport_2
  13. {
  14. public partial class Form1 : Form
  15. {
  16. string dataOUTBuffer;
  17. string[] dataPrepareToSend;
  18. int textLength = 0;
  19. byte[] dataToSend;
  20. // 1 2 3 4 5 A 6 7 8 9 B C 0 1 2 3 4
  21. byte[] bytetosend = { 0x01, 0x10, 0x80, 0x00, 0x00, 0x0A, 0x14, 0x31, 0x32, 0x33, 0x34, 0x35, 0x41, 0x36, 0x37, 0x38, 0x39, 0x42, 0x43, 0x30, 0x31, 0x32, 0x33, 0x34, 0x00, 0x00, 0x00, 0xC9, 0x21 };
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. string[] ports = SerialPort.GetPortNames();
  29. cBoxComPort.Items.AddRange(ports);
  30. }
  31. private void btnOpen_Click(object sender, EventArgs e)
  32. {
  33. try
  34. {
  35. serialPort1.PortName = cBoxComPort.Text;
  36. serialPort1.BaudRate = Convert.ToInt32(cBoxBaudRate.Text);
  37. serialPort1.DataBits = Convert.ToInt32(cBoxDataBits.Text);
  38. serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cBoxStopBits.Text);
  39. serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cBoxParityBits.Text);
  40. serialPort1.Open();
  41. progressBar1.Value = 100;
  42. }
  43. catch (Exception err)
  44. {
  45. MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  46. }
  47. }
  48. private void btnClose_Click(object sender, EventArgs e)
  49. {
  50. if(serialPort1.IsOpen)
  51. {
  52. serialPort1.Close();
  53. progressBar1.Value = 0;
  54. }
  55. }
  56. private void btnSendData_Click(object sender, EventArgs e)
  57. {
  58. if(serialPort1.IsOpen)
  59. {
  60. serialPort1.Write(bytetosend, 0, bytetosend.Length);//testing code
  61. }
  62. }
  63. private void button1_Click(object sender, EventArgs e)
  64. {
  65. tBoxDataOUT.Text = "";
  66. }
  67. }
  68. }
Now i want to use user input by means of textbox.
The value i will get from textbox will be a string value
like string dataFromTBox = tboxDataOut;
I want to know how to do further process in order to seperate each number or alphabet then convert it into hex and send it using serial port. Value send to serial port should be in byte form not in string form.

Brought to you by:

Answers (2)