C# serialport+mschart读取上位机数据绘制实时曲线之进制转换,与mschart数据源绑定

顽主小贝 2012-03-21 09:53:43
我现在的问题主要在于从串口读取数据后,进制转换,以及如何实现按照3秒的时间间隔来控制串口的读取和图像绘制、、、

小弟刚刚接触串口,还望各位大哥大姐多多指教。。。、

DBhelper sql = new DBhelper();
#region 变量声明
//定义 SerialPort对象
SerialPort serialPort1 = new SerialPort();
Byte[] vbReceive = new byte[6764];//声明字节型数组,用以存储串口中读取的数据
Byte[] receiveBuffer = new byte[6764];//声明字节型数组,用以存储串口中读取的数据
double[] gDiameter = new double[] { -1, -1, -1, 0.523, 0.542, 0.582, 0.626, 0.673, 0.723, 0.777, 0.835, 0.897, 0.964, 1.036, 1.113, 1.196, 1.286, 1.381, 1.485, 1.595, 1.714, 1.842, 1.98, 2.128, 2.286, 2.457, 2.64, 2.837, 3.052, 3.277, 3.521, 3.784, 4.066, 4.37, 4.696, 5.046, 5.423, 5.828, 6.263, 6.73, 7.232, 7.772, 8.352, 8.975, 9.645, 10.365, 11.138, 11.97, 12.863, 13.823, 14.854, 15.963, 17.154, 18.434, 19.81 }; //用于标识横轴坐标的[55]粒径数组
long[] gDataInDiamete = new long[55];// 每次从串口读取的粒径通道的数据(52个粒径通道)

delegate void ReceivData(byte[] bytes);//声明一个委托用于接收串口数据

serialPort1.ReadBufferSize = 6764; //接收缓冲区大小
#endregion




/// <summary>
/// 串口激发事件
/// </summary>
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
#region 判断接收数据包大小达到6764,并进行相关数据计算
if (serialPort1.ReceivedBytesThreshold >= 6764)
{
serialPort1.Read(vbReceive, 0, 6764);
if (vbReceive[0] == 255 && vbReceive[1] == 1&&vbReceive[6762]=255&&vbReceive[6763]=2)
{

gDataInDiamete[1] = -1;
gDataInDiamete[2] = -1;
gDataInDiamete[3] = -1;
gDataInDiamete[4] =(vbReceive, (2 + 2 * 52 * 64)) * 256;//(vbReceive[2 + 2 * 52 * 64] * 256) + BitConverter.ToInt64(vbReceive[2 + 2 * 52 * 64 + 1]);

dSum += gDataInDiamete[4];

for (int i = 1; i <= 51; i++)
{
gDataInDiamete[i + 4] = vbReceive[2 + 2 * 52 * 64 + 2 * i] * 256 + vbReceive[2 + 2 * 52 * 64 + 2 * i + 1];
dSum += gDataInDiamete[i + 4];
}
//将总浓度的值转换为string并赋值txtTC
txtTC.Text = Convert.ToString(dSum);
//}

#region 将数据实时保存在数据库中
//string strsql;
//strsql = "insert into APS (apsdatetime,sSum,apsTimeInterval,aps1,aps2,aps3,aps4,aps5,aps6,aps7,aps8,aps9,aps10,aps11,aps12,aps13,aps14,aps15,aps16,aps17,aps18,aps19,aps20,aps21,aps22,aps23,aps24,aps25,aps26,aps27,aps28,aps29,aps30,aps31,aps32,aps33,aps34,aps35,aps36,aps37,aps38,aps39,aps40,aps41,aps42,aps43,aps44,aps45,aps46,aps47,aps48,aps49,aps50,aps51,aps52) values ('" + TextBox1.Text + "','" + TextBox3.Text + "','" + + "','" + + "','" + TextBox2.Text + "')";
//BaseClass1.execsql(strsql);
#endregion

serialPort1.DiscardInBuffer();//清空数据缓存器

}
#endregion
}
...全文
410 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ant2009123 2014-09-02
  • 打赏
  • 举报
回复
灰常感谢,非常实用
清穆 2014-05-19
  • 打赏
  • 举报
回复
请问你最后做得如何??
顽主小贝 2012-03-21
  • 打赏
  • 举报
回复
忘了贴问题了,不好意思呃
上面的是我的源码,目前程序的问题主要有:
1、从串口独到的数据如何转换为long性;
2、将串口收到的实时数据保存在数据库中;
3、如何让mschart按照3秒的时间间隔更新图像、、、
最好在我的员马上做修改,小弟菜鸟,求教了、、、

4,816

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧