|
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
////禁止接收事件时直接退出
if (ReceiveEventFlag) return;
#region 根据结束字节来判断是否全部获取完成
List<byte> _byteData = new List<byte>();
bool found = false;//是否检测到结束符号
while (comPort.BytesToRead > 0 || !found)
{
byte[] readBuffer = new byte[comPort.ReadBufferSize + 1];
int count = comPort.Read(readBuffer, 0, comPort.ReadBufferSize);
for (int i = 0; i < count; i++)
{
_byteData.Add(readBuffer[i]);
if (readBuffer[i] == EndByte)
{
found = true;
}
}
}
#endregion
////字符转换
string readString = System.Text.Encoding.Default.GetString(_byteData.ToArray(), 0, _byteData.Count);
////触发整条记录的处理
if (DataReceived != null)
{
DataReceived(new DataReceivedEventArgs(readString));
}
}
串口类中的一个子程序(串口接收),readString可以得到正确的值,但是我不会调用它,请问我咋样才能读到他的值?万分感谢! |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|