110,534
社区成员
发帖
与我相关
我的任务
分享
public void JQHandler()
{
#if true
while (true)
{
//当前气量增加,流速/10
if (chkJQStart.Checked)
{
if (txtCurVol.InvokeRequired)
{
txtCurVol.BeginInvoke(new MethodInvoker(delegate()
{
fCurVol = float.Parse(txtCurVol.Text) + float.Parse(txtFlowRate.Text) / 10.0f;
txtCurVol.Text = fCurVol.ToString("#0.0000");
}));
}
//Thread.Sleep(100);
Thread.CurrentThread.Join(100);
}
}
#endif
}
private void frmFlowMeter_Load(object sender, EventArgs e)
{
//创建一个新的线程
threadTimer = new Thread(JQHandler);
threadTimer.IsBackground = true;
threadTimer.Start();
comport.DataReceived += new SerialDataReceivedEventHandler(ComDataRecv);//串口接到数据会激发这个事件
}
public void ComDataRecv(object sender, SerialDataReceivedEventArgs e)
{
int bytes = comport.BytesToRead;
byte[] buf = new byte[bytes];
comport.Read(buf, 0, bytes);
//1.缓存数据
buffer.AddRange(buf);
//2.完整性判断
while (buffer.Count >= 8)
{
if (buffer[0] > 2) { buffer.RemoveRange(0, buffer.Count); break; } //枪号不对,清除缓冲,重新开始
if (buffer[1] != 3 && buffer[1] != 5) { buffer.RemoveRange(0, buffer.Count); break; } //命令字不是3和5,清除缓冲,重新开始
byte[] ReceiveBytes = new byte[8];
//得到完整的数据,复制到ReceiveBytes中进行校验
buffer.CopyTo(0, ReceiveBytes, 0, 8);
Log(LogMsgType.Incoming, ByteArrayToHexString(ReceiveBytes) + "\n");
cp_ModBusDeed(ReceiveBytes);
buffer.RemoveRange(0, buffer.Count);
}
}
public void cp_ModBusDeed(byte[] RecvBuf)
{
//判断CRC是否正确
ushort usCRC16;
byte CRC16H, CRC16L;
byte[] SendBuf = new byte[9];
usCRC16 = CRC16(0xff, 0xff, RecvBuf, 6);
CRC16H = (byte)(usCRC16 >> 8); CRC16L = (byte)(usCRC16 & 0xff);
if ((RecvBuf[6] != CRC16L) || RecvBuf[7] != CRC16H) return;
if(RecvBuf[1] == 0x03) //读寄存器
{
String strAddr = BitConverter.ToString(RecvBuf,2,2);
strAddr = strAddr.Replace("-", "");
switch(strAddr)
{
case "00F4":
cp_SendComm(SendBuf, "0.0000", true);
break;
case "00F6": //读流速
cp_SendComm(SendBuf, txtFlowRate.Text,true);
break;
case "0102":
cp_SendComm(SendBuf, txtCurVol.Text, true);
break;
}
}
else if (RecvBuf[1] == 0x05) //写寄存器
{
String strAddr = BitConverter.ToString(RecvBuf, 2, 2);
strAddr = strAddr.Replace("-", "");
switch (strAddr)
{
case "0037": //清本次加气量
txtCurVol.BeginInvoke(new EventHandler(delegate { txtCurVol.Text = "0.0000"; }));
//Thread.Sleep(30);
cp_SendComm(RecvBuf,"",false);
break;
}
}
}
public void cp_SendComm(byte[] SendBuf, String strData, bool CountCRCF)
{
float TemSingle;
ushort usCRC16;
if (CountCRCF)
{
SendBuf[0] = 1;
SendBuf[1] = 3;
SendBuf[2] = 4;
TemSingle = float.Parse(strData);
unsafe
{
byte* p = (byte*)&TemSingle;
for (int i = 0; i < 4; i++) SendBuf[i + 3] = p[3 - i];
}
//填写CRC
usCRC16 = CRC16(0xff, 0xff, SendBuf, 7);
SendBuf[7] = (byte)(usCRC16 & 0xff);
SendBuf[8] = (byte)(usCRC16 >> 8);
}
//Thread.Sleep(50);
if(comport.IsOpen)
{
comport.Write(SendBuf, 0, SendBuf.Length);
Log(LogMsgType.Outgoing, ByteArrayToHexString(SendBuf) + "\n");
}
}
private void Log(LogMsgType msgtype, string msg)
{
rtfTerminal.BeginInvoke(new EventHandler(delegate
{
rtfTerminal.SelectedText = string.Empty;//表示空字符串。此字段为只读
rtfTerminal.SelectionFont = new Font(rtfTerminal.SelectionFont, FontStyle.Regular);
rtfTerminal.SelectionColor = LogMsgTypeColor[(int)msgtype];
if (msgtype == LogMsgType.Incoming)
rtfTerminal.AppendText("Recv:[" + DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond + "] ");
else if (msgtype == LogMsgType.Outgoing)
rtfTerminal.AppendText("Send:[" + DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond + "] ");
else
rtfTerminal.AppendText("[" + DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond + "] ");
rtfTerminal.AppendText(msg);
rtfTerminal.ScrollToCaret();
}));
}