如何发送和接收1024位? [英] How to send and receive 1024 Bits?

查看:160
本文介绍了如何发送和接收1024位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#Windows窗体设计一个界面,我想发送和接收使用串行端口1024位,我要送的数字是十六进制形式,所以我所做的就是下一个:

I'm designing an interface using C# windows Forms and I want to send and receive 1024 bits using serial port, the number I'm sending is in the hex-form, so what I did was the next:

private void button2_Click(object sender, EventArgs e)
{
    int i;
    int a = 0;
    byte[] mychar;
    mychar = new byte[128];
    string M = textBox1.Text;

    for (i = 0; i < 127; i++ )
    {
        mychar[i] = Convert.ToByte((M.Substring(a,2)),16);
        a += 2;
    }
    serialPort1.Write(mychar,0,127);
}

和检查数据正确与否,我短路了两个发射器和接收器,让我可以看到textbox5中显示我从TextBox1中送什么,问题是显示输出为ASCII文本框,我不能告诉如何将其转换为十六进制形式,(见我的尝试是评论波纹管):

and to check if the data is correct or not, I shorted out both the transmitter and receiver so I can see what I send from textbox1 to be shown in textbox5, the problem is the textbox is shown the output as ASCII, and I couldn't tell how to convert it to Hex form ,(see my attempt as commented bellow):

private void displaytext(object s, EventArgs e)
{
    textBox5.Clear();
    textBox5.AppendText(RXstring);
    //int value = Convert.ToInt32(RXstring, 16);
    //string stringValue = Char.ConvertFromUtf32(value);
    //textBox4.AppendText(stringValue);
}



所以为总结一下我的问题
1是要发送的数据是正确的代码?
2 - 我如何可以强制文本框来显示输出为十六进制?

so to summarize my problems: 1- Is the code to send data is correct? 2- How can I force the textbox to show the output as Hex?

非常感谢你。

更新这是我的全部代码,也许你明白我的问题:

UPDATE this is my full code, maybe then you understand my problem :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        string RXstring = "";
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                RXstring = serialPort1.ReadExisting();
                this.Invoke(new EventHandler(displaytext));
            }
            catch (System.TimeoutException)
            {

            }
        }
            private void displaytext(object s, EventArgs e)
        {

            textBox5.Clear();
             textBox5.AppendText(RXstring);
             //int value = Convert.ToInt32(RXstring, 16);
             //string stringValue = Char.ConvertFromUtf32(value);
             //textBox4.AppendText(stringValue);
        }

            private void pictureBox2_Click(object sender, EventArgs e)
            {
                serialPort1.Close();
                Form1 myForm = new Form1();
                this.Close();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!serialPort1.IsOpen)
                    {

                        serialPort1.Open();
                        button1.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("Port is Open by other party!");

                    }


                }
                catch (UnauthorizedAccessException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            private void Form3_FormClosed(object sender, FormClosedEventArgs e)
            {
                serialPort1.Close();
            }

            private void button3_Click(object sender, EventArgs e)
            {
                textBox1.Text = "0";
                textBox2.Text = "0";
                textBox3.Text = "0";
                textBox4.Text = "0";
                textBox5.Text = "";
            }

            private void button2_Click(object sender, EventArgs e)
            {
                int i;
                int a=0;
                byte[] mychar;
                mychar = new byte[128];
                string M = textBox1.Text;

                for (i = 0; i < 127; i++ ) {
                mychar[i] = Convert.ToByte((M.Substring(a,2)),16);
                a += 2;
                }
                serialPort1.Write(mychar,0,127);
            }

        }
    }

当我从TextBox1中发送数据我想显示正是我在textbox5发送,你可以帮我吗?
https://drive.google.com/file/d / 0B5PXKMhwKWQRREtuMXBaZDA1LUU /视图USP =共享

when I send data from textbox1 I want to be shown exactly as I send it in textbox5, can you help me in that ? https://drive.google.com/file/d/0B5PXKMhwKWQRREtuMXBaZDA1LUU/view?usp=sharing

推荐答案

我只是添加下面的代码:

I just added the following to the code :

string hex = "";
             foreach (char c in RXstring)
             {
                 int tmp = c;
                 hex += String.Format("{0:X2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
             }
             textBox5.AppendText(hex);

感谢您的任何方式:)

这篇关于如何发送和接收1024位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆