-1

The main question is here How to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?

Now i am trying to get the value of hex and convert it to ascii and display.

Main code is this

public partial class MainForm : Form
{
    private SerialPort _serialPort; // formda kullanilacak degisken
    private const int BaudRate = 9600; // BaudRate Constant. default 9600 ile oynanabilir 
    public MainForm()
    {
        InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        this.MinimizeBox = false;
        string[] portNames = SerialPort.GetPortNames(); // bütün kullanilabilecek com portlari okur
        foreach (var portName in portNames)
        {
            comboBox1.Items.Add(portName); // Adds Ports to combobox
        }
        if (comboBox1.SelectedIndex != -1)
        {
            comboBox1.SelectedIndex = 0; // Selects first entry (convenience purposes)
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // This block ensures that no exceptions happen
        if (_serialPort != null && _serialPort.IsOpen)
            _serialPort.Close();
        if (_serialPort != null)
            _serialPort.Dispose();
        // End of Block

        _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One);  //<-- Creates new SerialPort using the name selected in the combobox

        _serialPort.DataReceived += SerialPortOnDataReceived; //<-- this event happens everytime when new data is received by the ComPort
        _serialPort.Open(); //<-- make the comport listen
        textBox1.Text = string.Format("Listening on {0}...", comboBox1.Text);

    //here i am trying @Adam Casey 's code and serialReceived thing doesn't work.
         byte[] serialReceived;
         string reading = Encoding.UTF8.GetString(serialReceived);
         textBox2.Text = reading.Substring(13);
    }
    private delegate void Closure();
    private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
    {
        if (InvokeRequired)     //<-- Makes sure the function is invoked to work properly in the UI-Thread
            BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));     //<-- Function invokes itself
        else
        {
            while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
            {
                textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte()); //<-- bytewise adds inbuffer to textbox
            }
        }
    }
Community
  • 1
  • 1
ardai
  • 33
  • 1
  • 7

1 Answers1

0

I've experience on Com port and RS232 , if you elaborate your problem I can help .

If your problem is just converting a string based hex value to ASCII use following.

         //41 is ACII 'A'
        String hs = "41";
        var x = Convert.ToUInt32(hs, 16);
        StringBuilder sb= new StringBuilder();
        sb.Append(Convert.ToChar(x))
        String s = sb.ToString();

EDIT according to your comment

033 ID_00: 10.6 kg is gathered by following code

 String hs = "x30 30 33 33 20 49 44 5F 30 30 3A 20 20 20 31 30 2E 36 20 6B 67 20 0D 0A 0D 0A";
            System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(hs, "([A-Z-0-9]{2}) ");
            StringBuilder sb = new StringBuilder();
            System.Text.RegularExpressions.Match m = match.NextMatch();
            while(m.Success)
            {
                var x = Convert.ToUInt32(m.Value.Trim(),16);
                sb.Append(Convert.ToChar(x));
               m= m.NextMatch();
            }


            String s = sb.ToString();

If you have any initial char you may substring it first but this regex will mach par hex for you. Notice this and than you can code another regext to gather kg information , such as : (.+?) kg is a pattern to catch kg information from your ASCII representation.

Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
  • I am listenin com port 1 and getting values like Listening on COM1... 30 30 33 33 20 49 44 5F 30 30 3A 20 20 20 31 30 2E 36 20 6B 67 20 0D 0A 0D 0A. i want to get them and change it to ascii value and display it on a label. – ardai May 15 '16 at 11:43
  • sorrymy output is like this. when i'm getting the output from device to multiline textbox. my program is freezing after 1 second. here is the output; 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 and image:http://s32.postimg.org/mke80rwtx/IMG_20160515_143350.jpg – ardai May 15 '16 at 12:37
  • I didn't integrate a scale this way,but I can suggest you to check RS232 parameters. Baud rate,partiy, flow control etc. Is it a cashier scale ? – Davut Gürbüz May 15 '16 at 12:42
  • no it's for trucks. it was at 80kg when i started to listen comport1 because only 1 guy was standing on it. – ardai May 15 '16 at 12:46
  • The bunch of hex you shared returns many `(qp0 80 0` Maybe your data comes this way and 80 represents actual weight. – Davut Gürbüz May 15 '16 at 12:50
  • If so, you can code another regex something like `.+?qp(.+?)0` to catch weight or you can use simple string operations to handle. – Davut Gürbüz May 15 '16 at 12:52
  • yes it is. i want to get that data simultaneously, only the weight. and show it on a label. but it is freezing after some time. i don't know where to store the data) first and string operations to get only the weight and get the data from it to label. – ardai May 15 '16 at 12:53
  • Just use another thread for communication , this will solve your UI thread lock issue , this is out of this question's scope. Just seach on SO http://stackoverflow.com/a/2605991/413032 – Davut Gürbüz May 15 '16 at 13:07