【问题标题】:How to convert Hex to ASCII based on this code?如何根据此代码将 Hex 转换为 ASCII?
【发布时间】:2016-09-11 06:07:47
【问题描述】:

主要问题在这里How to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?

现在我正在尝试获取 hex 的值并将其转换为 ascii 并显示。

主要代码是这个

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
            }
        }
    }

【问题讨论】:

    标签: c# hex ascii


    【解决方案1】:

    我在 Com 端口和 RS232 方面有经验,如果您能详细说明您的问题,我可以提供帮助。

    如果您的问题只是将基于字符串的十六进制值转换为 ASCII,请使用以下内容。

             //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();
    

    根据您的评论进行编辑

    033 ID_00:10.6公斤通过以下代码采集

     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();
    

    如果您有任何初始字符,您可以先对其进行子串化,但此正则表达式将为您匹配十六进制。请注意这一点,然后您可以编写另一个正则表达式来收集 kg 信息,例如 : (.+?) kg 是一种从您的 ASCII 表示中捕获 kg 信息的模式。

    【讨论】:

    • 我正在监听 com 端口 1 并获得诸如 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 之类的值.我想获取它们并将其更改为 ascii 值并将其显示在标签上。
    • sorrymy 输出是这样的。当我从设备获取输出到多行文本框时。我的程序在 1 秒后冻结。这是输出; 20 20 20 30 0D 28 02 71 70 30 20 20 20 30 20 20 20 20 20 0d 28 02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0d2020 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 和图片:s32.postimg.org/mke80rwtx/IMG_20160515_143350.jpg
    • 我没有这样集成秤,但我可以建议你检查RS232参数。波特率,派对,流量控制等。是收银台吗?
    • 不,它适用于卡车。当我开始听comport1时它是80公斤,因为只有一个人站在上面。
    • 你分享的一堆十六进制返回很多(qp0 80 0也许你的数据是这样来的,80代表实际重量。
    猜你喜欢
    • 2021-05-01
    • 2015-12-19
    • 1970-01-01
    • 2019-09-13
    • 2015-10-30
    • 2018-08-30
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多