找传奇、传世资源到传世资源站!

C#实现环形缓冲区实例

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

因个人需求,需要测试串口数据,直接拿去应用就可以了,经历半个月的压力测试,都没问题, *组件功能:利用环形缓冲区的特点实现对接收到的数据环形缓冲进行缓冲,
 *进而减少应用程序对数据解析的压力,同时通过组件里面封装的读取缓冲
 *区的数据的函数,来读取缓冲区完整的一帧数据,从而减少或是避免数据
 *包的丢失的情况的发生,同时也减少了工程师重复编写数据解析方法的重,
from clipboard
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO.Ports;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;using TornadoJournal;namespace WindowsFormsApplication1{ public partial class Form1 : Form { #region 创建日志 TornadoJournal.Journal ReceivedJournal = new TornadoJournal.Journal(System.Environment.CurrentDirectory "\\" "ReceivedJournal", "Journal" DateTime.Now.ToString("yy-MM-dd-hh-mm-ss")); TornadoJournal.Journal SendJournal = new TornadoJournal.Journal(System.Environment.CurrentDirectory "\\" "SendJournal", "Journal" DateTime.Now.ToString("yy-MM-dd-hh-mm-ss")); #endregion #region 构造函数 public Form1() { InitializeComponent(); tornadoSerialPort1_DealAnalyticaledDataDwangD = new tornadoSerialPort1_DealAnalyticaledDataDelegate(tornadoSerialPort1_DealAnalyticaledDataDwang); string[] str; if (tornadoSerialPort1.ScanSerialPort(out str)) { comboBox1.Items.AddRange(str); comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//旋转第3个可用串口 } } #endregion #region 打开、关闭串口 private void button1_Click(object sender, EventArgs e) { if (button1.Text == "打开") { ReceivedJournal.CreateJournalFile(); SendJournal.CreateJournalFile(); button1.Text = "关闭"; comboBox1.Enabled = false; tornadoSerialPort1.SetSerialPortParameter(9600, Parity.None, StopBits.One,8,1); tornadoSerialPort1.OpenSerialPort(comboBox1.Text); } else if (button1.Text == "关闭") { button1.Text = "打开"; comboBox1.Enabled = true; tornadoSerialPort1.CloseSerialPort(); } } #endregion #region 处理解析好的数据 #region 创建更新textbox控件数据的委托 public delegate void tornadoSerialPort1_DealAnalyticaledDataDelegate(byte[] data); tornadoSerialPort1_DealAnalyticaledDataDelegate tornadoSerialPort1_DealAnalyticaledDataDwangD; #endregion private void tornadoSerialPort1_DealAnalyticaledData(byte[] data) { BeginInvoke(tornadoSerialPort1_DealAnalyticaledDataDwangD, data); } Int64 count = 1; public void tornadoSerialPort1_DealAnalyticaledDataDwang(byte[] data) { textBox1.AppendText("\r\n"); textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") "\r\n"); textBox1.AppendText("接收成功次数:" count "\r\n"); textBox1.AppendText("接收到的数据:"); textBox1.AppendText(String.Join(",", data) ); textBox1.AppendText("\r\n"); textBox1.AppendText("原始数据:"); foreach (byte b in data) { string hex16 =b.ToString("X2");//是否满足两位 textBox1.AppendText(hex16 " "); } textBox1.AppendText("\r\n"); int sum3 = data[0] data[1] data[2]; int sum2 = (data[4] << 8) data[3];//待修改 int temp = data[2] & 0x0F; textBox1.AppendText("帧起始符:" "\t" Convert.ToString(data[0], 16) "\r\n"); textBox1.AppendText("卡片编号:" "\t" Convert.ToString((((data[2] & 0x0F)<<8) data[1]),10) "\r\n"); textBox1.AppendText("校验和 :" "\t" Convert.ToString(sum2, 16) "\r\n"); if((data[2]&0xc0)==0x00) textBox1.AppendText("电池状态:满电" "\r\n"); else if((data[2] & 0xc0) == 0x40) textBox1.AppendText("电池状态:半满" "\r\n"); else if ((data[2] & 0xc0) == 0x80) textBox1.AppendText("电池状态:电量低" "\r\n"); else if ((data[2] & 0xc0) == 0xC0) textBox1.AppendText("电池状态:欠压" "\r\n"); if ((data[2] & 0x30) == 0x00) textBox1.AppendText("卡片状态:正常" "\r\n"); else textBox1.AppendText("卡片状态:异常" "\r\n"); if (sum3 == sum2) { textBox1.AppendText("校验成功" "\r\n"); } else { textBox1.AppendText("校验失败" "\r\n"); } ReceivedJournal.WriteStrMessageToJournalFile(textBox1.Text, Encoding.Default); count ; } #endregion #region 自动发送数据 private void button3_Click(object sender, EventArgs e) { try { if (SendDataThread != null) SendDataThread.Abort(); SendDataThread = new Thread(new ThreadStart(SendDataT)); SendDataThread.IsBackground = true;//设置为后台线程 SendDataThread.Start(); button3.Enabled = false; } catch { } } private Thread SendDataThread; private void SendDataT() { Int64 num = 0; Int64 count=1; byte[] bytedata = new byte[tornadoSerialPort1.FrameSize]; bytedata[0] = 0xFF; bytedata[1] = 0x38; while (true) { try { num ; if (num >= 380) num = 0; bytedata[0] = 0xFF; bytedata[1] = 0x38; bytedata[2] = 0x78; bytedata[3] = (byte)(0xFF & (num >> 8)); bytedata[4] = (byte)(0xFF & (num >> 0)); tornadoSerialPort1.SerialPortSendData(bytedata); SendJournal.WriteStrMessageToJournalFile(string.Join(" ", bytedata) " " count, Encoding.Default); Thread.Sleep(1); count ; } catch { } } } #endregion #region 关闭自动发送线程 private void buttonClear_Click(object sender, EventArgs e) { tornadoSerialPort1.SerialPortSendData(new byte[3]{1,2,3}); //try //{ // if (SendDataThread != null) SendDataThread.Abort(); // button3.Enabled = true; //} //catch { }; } #endregion private void textBox1_TextChanged(object sender, EventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void btnClear_Click(object sender, EventArgs e) { textBox1.Clear(); } private void Form1_Load(object sender, EventArgs e) { } private void groupBox1_Enter(object sender, EventArgs e) { } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复