C#串口通信中当我点选HexSend发送的时候将输入框数据转化为十六进制,取消点选将数据转化过来,点选HexView将接收框数据转化为十六进制,取消点选将数据转
代码如下,请大侠帮忙完善,不胜感激using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using System.Text.RegularExpressions;
using System.Threading;
namespace TESS
{
public partial class fomMain : Form
{
public fomMain()
{
InitializeComponent();
}
private void fomMain_Load(object sender, EventArgs e)
{
for (int i = 1; i < 15; i++)
{
cmbxComNum.Items.Add("COM" + i.ToString());
}
string[] sacheck = Enum.GetNames((typeof()));
for (int i = 0; i < sacheck.Length; i++)
{
cmbxCheck.Items.Add(sacheck[i]);
}
string[] saStop = Enum.GetNames((typeof()));
for (int i = 0; i < saStop.Length; i++)
{
cmbxStop.Items.Add(saStop[i]);
}
cmbxBaudRate.SelectedIndex = 3;
cmbxCheck.SelectedIndex = 1;
cmbxData.SelectedIndex = 0;
cmbxComNum.SelectedIndex = 0;
cmbxStop.SelectedIndex = 1;
}
private void btnOpen_Click(object sender, EventArgs e)
{
serialPortMain.PortName = cmbxComNum.SelectedItem.ToString();
serialPortMain.BaudRate = int.Parse(cmbxBaudRate.SelectedItem.ToString());
serialPortMain.Parity = (Parity)(Enum.Parse(typeof(Parity), cmbxCheck.SelectedItem.ToString()));
serialPortMain.StopBits = (StopBits)(Enum.Parse(typeof(StopBits), cmbxStop.SelectedItem.ToString()));
//serialPortMain.StopBits = StopBits.One;
try
{
serialPortMain.Open();
btnOpen.Enabled = false;
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
if (serialPortMain.IsOpen)
{
serialPortMain.Close();
}
btnOpen.Enabled = true;
}
private void btnSend_Click(object sender, EventArgs e)
{
nt bytes = serialPortMain.BytesToRead; // 获取字节长度
byte[] buffer = new byte[bytes];// 创建字节数组
serialPortMain.Read(buffer, 0, bytes);// 读取缓冲区的数据到数组
string str = "";
for (int i = 0; i < buffer.Length; i++)
{
str += buffer[i].ToString("X2");
}
serialPortMain.WriteLine(txtSend.Text.Trim());
}
private void serialPortMain_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke((EventHandler)
(delegate
{
int bytes = serialPortMain.BytesToRead; // 获取字节长度
byte[] buffer = new byte[bytes];// 创建字节数组
serialPortMain.Read(buffer, 0, bytes);// 读取缓冲区的数据到数组,参数1字节数组,参数2数组起始位,参数3数组长度
if (ckbxView.Checked)
{
string str = "";
for (int i = 0; i < buffer.Length; i++)
{
str += buffer[i].ToString("X2");
}
txtRcv.Text = str;
//txtRcv.AppendText("\r\n" + DateTime.Now.ToString() + "接收到数据:" +str );
}
else
{
txtRcv.AppendText(Encoding.ASCII.GetString(buffer));
}
}
)
);
}
private void ckbxSend_CheckedChanged(object sender, EventArgs e)
{
string hexOutput = "";
if (ckbxSend.Checked)
{
char[] values = txtSend.Text.ToCharArray();
foreach (char letter in values)
{
int value = Convert.ToInt32(letter);
hexOutput += String.Format("{0:X}", value);
}
txtSend.Text = hexOutput;
}
else
{
}
}
private void ckbxView_CheckedChanged(object sender, EventArgs e)
{
if (ckbxView.Checked)
{
string hexOutput = "";
char[] values = txtRcv.Text.ToCharArray();
foreach (char letter in values)
{
int value = Convert.ToInt32(letter);
hexOutput += String.Format("{0:X}", value);
}
txtRcv.Text = hexOutput;
}
else
{
}
}
}
}