/// <summary> /// 用于正式接收串口设备的数据 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public virtual void ReceiveData(object sender, DataTransmissionEventArgs args) { if (DeviceDataRecerived != null) { DeviceDataRecerived(this, args); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public override void ReceiveData(object sender, DataTransmissionEventArgs args) { BaseCommand cmd = null; lock (m_ReadBuffer) { m_ReadBuffer.AddRange(args.EventData); if (m_ReadBuffer.Count >= MINACKLENTH) { int endIndex = m_ReadBuffer.FindLastIndex((x) => { return(x == 0x03); }); //帧尾找不到,返回 if (endIndex < MINACKLENTH - 1) { return; } else { cmd = Unpackage(); } } else { return; } } HangingBalanceEventArgs data = new HangingBalanceEventArgs(cmd, args.PortName); base.ReceiveData(sender, data); }
/// <summary> /// 只能用于检测串口,收到串口设备数据包 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public override void OnDetectDataReceived(object sender, DataTransmissionEventArgs args) { //List<byte> buffer = (List<byte>)_bufferByCom[args.PortName]; //if (buffer == null) // return; //buffer.AddRange(args.EventData); //if (buffer.Count >= _detectByteLength && buffer[9] == 0x03) //{ // _bufferByCom.Clear();//找到串口,清除缓存 // base.OnDetectDataReceived(sender, args); // System.Diagnostics.Debug.WriteLine("OnDetectDataReceived Invoked : " + args.PortName); //} }
/// <summary> /// 只能用于检测串口,收到串口设备数据包 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public override void OnDetectDataReceived(object sender, DataTransmissionEventArgs args) { List <byte> buffer = (List <byte>)_bufferByCom[args.PortName]; if (buffer == null) { return; } buffer.AddRange(args.EventData); if (buffer.Count >= _detectByteLength && buffer[0] == 0x7B && buffer[_detectByteLength - 1] == 0x7D) { _bufferByCom.Clear();//找到串口,清除缓存 base.OnDetectDataReceived(sender, args); System.Diagnostics.Debug.WriteLine("OnDetectDataReceived Invoked : " + args.PortName); } }
/// <summary> /// 用于正式接收串口设备的数据 /// -32768 ~ 32767在此范围的值,当传过来的值超过32767时一定是负值,此时要异或0xFFFF后再加1补码是为负数的绝对值 /// 数据格式:以一对大括号开始结束,共9个字节 /// { DP D5 D4 D3 D2 D1 UNIT }\{ E R R O R 0 1 }\{ E R R O R 0 2 }\{ E R R O R 0 3 } /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public override void ReceiveData(object sender, DataTransmissionEventArgs args) { byte[] temp = new byte[_detectByteLength]; try { lock (m_ReadBuffer) { m_ReadBuffer.AddRange(args.EventData); } PressureMeterArgs para = Analyze(m_ReadBuffer); if (para != null) { base.ReceiveData(sender, para); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); throw ex; } }
/// <summary> /// 只能用于检测串口,收到串口设备数据包 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public override void OnDetectDataReceived(object sender, DataTransmissionEventArgs args) { byte[] buffer = new byte[_detectByteLength]; lock (m_ReadBuffer) { m_ReadBuffer.AddRange(args.EventData); if (m_ReadBuffer.Count >= _detectByteLength) { int endIndex = m_ReadBuffer.FindIndex(0, (x) => { return(x == 0x0D); }); if (endIndex < 0) { return; } else if (endIndex > 0 && endIndex != _detectByteLength - 1) { m_ReadBuffer.RemoveRange(0, endIndex + 1); return; } else { m_ReadBuffer.CopyTo(0, buffer, 0, _detectByteLength); m_ReadBuffer.RemoveRange(0, _detectByteLength); } } else { return; } } int weight = 0; for (int iLoop = 7; iLoop >= 3; iLoop--) { weight += (buffer[iLoop] - 0x30) << (iLoop - 3) * 4; } int decimal_place = buffer[8] & 0x03; float fweightKiloGrams = 0f; switch (decimal_place) { case 0: fweightKiloGrams = weight * 1.0f; break; case 1: fweightKiloGrams = weight * 0.1f; break; case 2: fweightKiloGrams = weight * 0.01f; break; case 3: fweightKiloGrams = weight * 0.001f; break; default: fweightKiloGrams = weight * 1.0f; break; } PToolingDataEventArgs toolingData = new PToolingDataEventArgs(fweightKiloGrams, true); base.OnDetectDataReceived(sender, toolingData); }
/// <summary> /// 只能用于检测串口,收到串口设备数据包 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public virtual void OnDetectDataReceived(object sender, DataTransmissionEventArgs args) { }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public override void ReceiveData(object sender, DataTransmissionEventArgs args) { byte[] buffer = new byte[_detectByteLength]; lock (m_ReadBuffer) { m_ReadBuffer.AddRange(args.EventData); if (m_ReadBuffer.Count >= _detectByteLength) { int headIndex = m_ReadBuffer.FindIndex(0, (x) => { return(x == 0x55); }); if (headIndex < 0) { m_ReadBuffer.Clear(); return; } else if (headIndex > 0) { m_ReadBuffer.RemoveRange(0, headIndex); return; } else { m_ReadBuffer.CopyTo(0, buffer, 0, _detectByteLength); m_ReadBuffer.RemoveRange(0, _detectByteLength); } } else { return; } } ushort sensorValue = buffer[7]; sensorValue += (ushort)(buffer[8] << 8); byte decimal_place = buffer[9]; float fSensorValue = 0f; switch (decimal_place) { case 0: fSensorValue = sensorValue * 1.0f; break; case 1: fSensorValue = sensorValue * 0.1f; break; case 2: fSensorValue = sensorValue * 0.01f; break; case 3: fSensorValue = sensorValue * 0.001f; break; default: fSensorValue = sensorValue * 1.0f; break; } Graseby9600DataEventArgs toolingData = new Graseby9600DataEventArgs(fSensorValue, true); base.ReceiveData(sender, toolingData); }
/// <summary> /// 只能用于检测串口,收到串口设备数据包 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public override void OnDetectDataReceived(object sender, DataTransmissionEventArgs args) { }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public override void ReceiveData(object sender, DataTransmissionEventArgs args) { //从天平里读出的数据格式:53 20 44 20 20 20 31 38 2E 36 33 38 30 35 20 67 0D 0A // 也有可能是:53 20 53 20 20 20 20 30 2E 30 30 30 30 30 20 67 0D 0A //以0x53 0x20 0x44 0x20 0x20 0x20开头,0x0D 0x0A结束,并且带有单位0x20 0x67( g) byte[] buffer = new byte[_detectByteLength]; lock (m_ReadBuffer) { m_ReadBuffer.AddRange(args.EventData); if (m_ReadBuffer.Count >= _detectByteLength) { int endIndex = m_ReadBuffer.FindIndex(0, (x) => { return(x == 0x0A); }); if (endIndex < 0) { return; } else if (endIndex > 0 && endIndex != _detectByteLength - 1) { m_ReadBuffer.RemoveRange(0, endIndex + 1); return; } else { m_ReadBuffer.CopyTo(0, buffer, 0, _detectByteLength); m_ReadBuffer.RemoveRange(0, _detectByteLength); } } else { return; } } string weight = System.Text.Encoding.ASCII.GetString(buffer); if (weight.Length < 16) { return; } //get the balance unit string unitString = weight.Substring(14, 2); string unit = string.Empty; int iLoop = 0; while (iLoop < 2) { if (unitString[iLoop] != 0x20) { unit += unitString[iLoop]; } iLoop++; } WeightUint balanceUnit = WeightUint.g; foreach (string s in Enum.GetNames(typeof(WeightUint))) { if (s.Equals(unit)) { balanceUnit = (WeightUint)Enum.Parse(typeof(WeightUint), unit); break; } } int negative = weight[0] == '-' ? -1 : 1; float weightValue = 0f; bool bRet = false; bRet = float.TryParse(weight.Substring(4, 10), out weightValue); float weightGrams = 0f; switch (balanceUnit) { case WeightUint.kg: weightGrams = weightValue * 1000 * negative; break; case WeightUint.mg: weightGrams = weightValue / 1000 * negative; break; case WeightUint.g: weightGrams = weightValue * negative; break; default: weightGrams = weightValue * negative; break; } BalanceDataEventArgs balanceData = new BalanceDataEventArgs(weightGrams, bRet); base.ReceiveData(sender, balanceData); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public override void ReceiveData(object sender, DataTransmissionEventArgs args) { }