/// </summary>
/// <param name="type">消息类型</param>
public byte[] WaitReceive(byte type)
{
DateTime time = DateTime.Now;
while (true)
{
byte[] receiveData = m_ReceiveDataCollection.Get(type);
if (receiveData != null) return receiveData;
if (DateTime.Now.Subtract(time).TotalMinutes > 3) return null;
Thread.Sleep(50);
}
}
#endregion
#region 判断消息是否发送成功
/// <summary>
/// 判断消息是否发送成功
/// </summary>
public bool SendSuccess(byte type, byte subtype)
{
DateTime time = DateTime.Now;
while (true)
{
if (DateTime.Now.Subtract(time).TotalMinutes > 3)
{
return false;
}
byte[] ack = m_ReceiveDataCollection.Get(type, subtype);
byte[] nak = m_ReceiveDataCollection.Get(type, subtype);
if (ack != null) return true;
if (nak != null) return false;
Thread.Sleep(1);
}
}
#endregion
}
}
第第2部分:接收串口数据,并响应货机,向货机发送数据部分:接收串口数据,并响应货机,向货机发送数据
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using MachineJPDll.Models;
using MachineJPDll.Utils;
/*
* VMC->PC数据的接收,货机事件的分发
*/
namespace MachineJPDll
{
partial class MachineJP
{
#region serialPort_DataReceived
/// <summary>
/// 数据接收事件的方法
/// </summary>
public void serialPort_DataReceived(object obj, SerialDataReceivedEventArgs args)
{
byte[] receiveData = ReadPort();