我正在使用C#windows Forms设计界面,我想使用串口发送和接收1024位,我发送的数字是十六进制格式,所以我做的是下一个:
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中显示,问题是文本框显示输出为ASCII,并且我无法告诉如何将其转换为十六进制形式,(请参阅我的评论下面的评论):
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-如何强制文本框将输出显示为十六进制?
非常感谢你。更新这是我的完整代码,也许您了解我的问题:
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/view?usp=sharing
答案 0 :(得分:0)
此MCVE显示如何在byte
和十六进制string
之间进行转换:
class Program {
static void Main(string[] args) {
byte[] bytes = FromHexString("0123456789ABCEF");
string text = ToHexString(bytes);
Console.Write(text);
Console.ReadKey(true);
}
static byte[] FromHexString(string hexString) {
byte[] result;
//determine length and handle case of uneven digit count
int length = hexString.Length / 2;
bool even = length * 2 == hexString.Length;
if(!even) {
length++;
}
//allocate memory
result = new byte[length];
int offset;
if(even) {
offset = 0;
} else {
//convert first digit, if digit count is uneven
result[0] = Convert.ToByte(hexString[0].ToString(), 16);
offset = 1;
}
for(int i = offset; i < result.Length; i++) {
//convert digit to byte
result[i] = Convert.ToByte((hexString.Substring(i * 2 - offset, 2)), 16);
}
return result;
}
static string ToHexString(byte[] bytes, bool upperCase = true) {
string format = upperCase ? "X" : "x";
//initialize result with double capacity as a byte in hex notation occupies 2 chars
StringBuilder result = new StringBuilder(bytes.Length * 2);
foreach(var @byte in bytes) {
result.Append(@byte.ToString(format));
}
return result.ToString();
}
}
答案 1 :(得分:0)
我刚刚在代码中添加了以下内容:
string hex = "";
foreach (char c in RXstring)
{
int tmp = c;
hex += String.Format("{0:X2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
textBox5.AppendText(hex);
谢谢你:)