111,126
社区成员
发帖
与我相关
我的任务
分享
string s="2e";
byte b=byte.Parse(s, System.Globalization.NumberStyles.HexNumber);
System.Timers.Timer t = new System.Timers.Timer();//定时器
t.AutoReset = true;
t.Interval = 1000;//一秒
t.Enabled = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(comm_DataReceived);//触发执行事件
void comm_DataReceived(object sender, System.Timers.ElapsedEventArgs e)
{
int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
received_count += n;//增加接收计数
comm.Read(buf, 0, n);//读取缓冲数据
builder.Remove(0, builder.Length);//清除字符串构造器的内容
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler)(delegate
{
//判断是否是显示为16禁止
if (checkBoxHexView.Checked)
{
//依次的拼接出16进制字符串
foreach (byte b in buf)
{
builder.Append(b.ToString("X2") + " ");
}
}
else
{
//直接按ASCII规则转换成字符串
builder.Append(Encoding.ASCII.GetString(buf));
}
//追加的形式添加到文本框末端,并滚动到最后。
this.txGet.AppendText(builder.ToString() + "\n");
//修改接收计数
labelGetCount.Text = "Get:" + received_count.ToString();
}));
}
using System.IO.Ports;
using System.Text.RegularExpressions;
namespace SerialportSample
{
public partial class SerialportSampleForm : Form
{
private SerialPort comm = new SerialPort();
private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。
private long received_count = 0;//接收计数
private long send_count = 0;//发送计数
string s;
int headCount = 0, sizeCount = 0, datacount = 0;
public SerialportSampleForm()
{
InitializeComponent();
}
//窗体初始化
private void Form1_Load(object sender, EventArgs e)
{
//初始化下拉串口名称列表框
string[] ports = SerialPort.GetPortNames();
Array.Sort(ports);
comboPortName.Items.AddRange(ports);
comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
comboBaudrate.SelectedIndex = comboBaudrate.Items.IndexOf("9600");
//初始化SerialPort对象
comm.NewLine = "\r\n";
comm.RtsEnable = true;//根据实际情况吧。
//添加事件注册
comm.DataReceived += comm_DataReceived;
}
void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
received_count += n;//增加接收计数
comm.Read(buf, 0, n);//读取缓冲数据
builder.Remove(0, builder.Length);//清除字符串构造器的内容
//因为要访问ui资源,所以需要使用invoke方式同步ui。
this.Invoke((EventHandler)(delegate
{
//判断是否是显示为16禁止
if (checkBoxHexView.Checked)
{
//依次的拼接出16进制字符串
foreach (byte b in buf)
{
builder.Append(b.ToString("X2") + " ");
}
}
else
{
//直接按ASCII规则转换成字符串
builder.Append(Encoding.ASCII.GetString(buf));
}
int minDataLen;
s = builder.ToString();
minDataLen = s.Length;
if (minDataLen > 7)
{
int count1, count2, count3, count4;//count1记录帧头count2记录长度count3记录数据开始位count4记录数据结束位
for (count1 = 0; count1 < minDataLen - 4; count1++)
{
if (s.Substring(count1, 4) == "CT:4")
{
headCount += 1;
labelhead.Text = "head:" + headCount.ToString();
texthead.AppendText(s.Substring(count1, 4) + " \n");
count2 = count1 + 4;//长度起始位
string s1;
int size;
s1 = s.Substring(count2, 3);
byte b = byte.Parse(s1, System.Globalization.NumberStyles.HexNumber);
//textBox5.Text = b.ToString();
int.TryParse(b.ToString(), out size);
if (size >= 0 && size <= 4095)
{
sizeCount += 1;
labelsize.Text = "(0x)size:" + sizeCount.ToString();
textsize.AppendText(s.Substring(count2, 3) + "\n");
labelsize10.Text = "(十)size:" + sizeCount.ToString();
textsize10.AppendText(b.ToString() + "\n");
count3 = count2 + 3;//数据起始位
for (count4 = count3; (count4 - count3) <= size; count4++)
{
if ( s.Substring(count4, 4) == "CT:4")
{
datacount += 1;
labeldata.Text = "Data:" + datacount.ToString();
if (s.Substring(count3, count4 - count3).Length > 0)
{
textdata.AppendText(s.Substring(count3, count4 - count3) + "\n");
textcrc.AppendText(s.Substring(count4 - 1, 1) + "\n");
}
else { textdata.AppendText("Error\n"); textcrc.AppendText("Error\n"); }
count4 = count3 + size;
}
}
}
}
}
}
//追加的形式添加到文本框末端,并滚动到最后。
this.txGet.AppendText(builder.ToString());
//修改接收计数
labelGetCount.Text = "Get:" + received_count.ToString();
}));
}
private void buttonOpenClose_Click(object sender, EventArgs e)
{
//根据当前串口对象,来判断操作
if (comm.IsOpen)
{
//打开时点击,则关闭串口
comm.Close();
}
else
{
//关闭时点击,则设置好端口,波特率后打开
comm.PortName = comboPortName.Text;
comm.BaudRate = int.Parse(comboBaudrate.Text);
comm.ReadBufferSize = 4096;
comm.WriteBufferSize = 4096;
comm.DataBits = 8;
comm.ReceivedBytesThreshold = 10240;
try
{
comm.Open();
}
catch(Exception ex)
{
//捕获到异常信息,创建一个新的comm对象,之前的不能用了。
comm = new SerialPort();
//现实异常信息给客户。
MessageBox.Show(ex.Message);
}
}
//设置按钮的状态
buttonOpenClose.Text = comm.IsOpen ? "Close" : "Open";
buttonSend.Enabled = comm.IsOpen;
}
//动态的修改获取文本框是否支持自动换行。
private void checkBoxNewlineGet_CheckedChanged(object sender, EventArgs e)
{
txGet.WordWrap = checkBoxNewlineGet.Checked;
}
private void buttonSend_Click(object sender, EventArgs e)
{
//定义一个变量,记录发送了几个字节
int n = 0;
//16进制发送
if (checkBoxHexSend.Checked)
{
//我们不管规则了。如果写错了一些,我们允许的,只用正则得到有效的十六进制数
MatchCollection mc = Regex.Matches(txSend.Text, @"(?i)[\da-f]{2}");
List<byte> buf = new List<byte>();//填充到这个临时列表中
//依次添加到列表中
foreach (Match m in mc)
{
buf.Add(byte.Parse(m.Value));
}
//转换列表为数组后发送
comm.Write(buf.ToArray(), 0, buf.Count);
//记录发送的字节数
n = buf.Count;
}
else//ascii编码直接发送
{
//包含换行符
if (checkBoxNewlineSend.Checked)
{
comm.WriteLine(txSend.Text);
n = txSend.Text.Length + 2;
}
else//不包含换行符
{
comm.Write(txSend.Text);
n = txSend.Text.Length;
}
}
send_count += n;//累加发送字节数
labelSendCount.Text = "Send:" + send_count.ToString();//更新界面
}
private void buttonReset_Click(object sender, EventArgs e)
{
//复位接受和发送的字节数计数器并更新界面。
send_count = received_count = 0;
labelGetCount.Text = "Get:0";
labelSendCount.Text = "Send:0";
}
}
}
for (count4 = count3; (count4-count3) <=size; count4++)
{
if (s.Substring(count4, 4) == "PV:0" || s.Substring(count4, 4) == "TT:1")
{
datacount += 1;//数据个数计数
labeldata.Text = "Data:" + datacount.ToString();
textdata.AppendText(s.Substring(count3, count4 - count3) + "\n");
textcrc.AppendText(s.Substring(count4-1, 1) + "\n");
}
}