private void RecvMsg() { DynoParamRecvEventArgs args = new DynoParamRecvEventArgs(); int bytesRead; _strRecv = ""; try { do { bytesRead = _clientStream.Read(_recvBuf, 0, _bufSize); _strRecv += Encoding.UTF8.GetString(_recvBuf, 0, bytesRead); } while (_clientStream.DataAvailable); } catch (Exception ex) { args.Code = "600"; args.Msg = "发送VIN号后,接收返回信息出错:" + ex.Message; DynoParamRecvEvent?.Invoke(this, args); return; } // TCP接收的数据会有粘包现象,需要拆包操作 if (_strRecv.StartsWith("200")) { if (_strRecv.Length == 3) { _strRecv = ""; try { do { bytesRead = _clientStream.Read(_recvBuf, 0, _bufSize); _strRecv += Encoding.UTF8.GetString(_recvBuf, 0, bytesRead); } while (_clientStream.DataAvailable); } catch (Exception ex) { args.Code = "600"; args.Msg = "接收测功机参数出错:" + ex.Message; DynoParamRecvEvent?.Invoke(this, args); return; } } else { _strRecv = _strRecv.Substring(3); } args.Code = "200"; args.Msg = _strRecv; } else { if (_strRecv.Length >= 3) { args.Code = _strRecv.Substring(0, 3); if (args.Code == "400") { args.Msg = "VIN号格式错误"; } else { args.Msg = _strRecv.Substring(3); } } else { args.Code = "600"; args.Msg = "未知错误"; } } DynoParamRecvEvent?.Invoke(this, args); }
private void OnDynoParamRecv(object sender, DynoParamRecvEventArgs e) { _dynoParamRecvFlag.Set(); if (e.Code == "200") { EI = JsonConvert.DeserializeObject <EmissionInfo>(e.Msg); VI.VehicleModel = EI.VehicleModel; EI.OpenInfoSN = string.Empty; VI.OpenInfoSN = string.Empty; EI.EngineSN = string.Empty; EI.Name = string.Empty; } else { _log.TraceError(string.Format("Get dyno parameter from MES error, Code: {0}, Error: {1}", e.Code, e.Msg)); MessageBox.Show(string.Format("从MES获取测功机参数失败, Code: {0}, Error: {1}", e.Code, e.Msg), "获取测功机参数", MessageBoxButtons.OK, MessageBoxIcon.Error); } Invoke((EventHandler) delegate { FillInputTextBox(); }); }