void fetchObdValue() { for (int i = 0; i < obdShowIndexs.Count; i++) { int index; try { index = obdShowIndexs[i]; } catch (Exception) { continue; } if (index % 0x20 == 0) { dataGridViewshow.Rows[index].Cells[2].Value = arrStrSupported[index / 0x20]; continue; } int parentIndex = index / 0x20; int childIndex = index - (parentIndex * 0x20) - 1; if (arrStrSupported[parentIndex][childIndex] == '0') { dataGridViewshow.Rows[index].Cells[2].Value = "此车不支持"; continue; } UDSResponseList retClass = null; UDSServiceFormat uds = listOBD[index]; for (int reTry = 0; reTry < 10; reTry++) { DateTime prev = DateTime.Now; while ((DateTime.Now - prev).TotalMilliseconds < DELAY) { Application.DoEvents(); } retClass = udsClass.getResponseList(uds); if ((retClass.errorCode != UDSResponseList.ERRORCODE.SUCCEED) || (retClass.responseList[1] != uds.parameterList[0])) { continue; } break; } if (retClass.errorCode != UDSResponseList.ERRORCODE.SUCCEED) { dataGridViewshow.Invoke(new Action(() => { dataGridViewshow.Rows[index].Cells[2].Value = "超时"; })); continue; } else if ((retClass.responseList[1] != uds.parameterList[0])) { dataGridViewshow.Invoke(new Action(() => { dataGridViewshow.Rows[index].Cells[2].Value = "帧出错,等待重发"; })); continue; } string obdVal = obdFormula(retClass); dataGridViewshow.Invoke(new Action(() => { dataGridViewshow.Rows[index].Cells[2].Value = obdVal; })); } }
private void buttonAddOBD_Click(object sender, EventArgs e) { byte externFlag = 0; if (radioButtonOS.Checked) { externFlag = 0; } else if (radioButtonOE.Checked) { externFlag = 1; } else { MessageBox.Show("请指定帧格式"); return; } if (listBoxOBD.SelectedIndex == -1) { MessageBox.Show("请指定OBD请求"); return; } protocolType = 0; udsReq = listOBD[listBoxOBD.SelectedIndex]; udsReq.format = externFlag; this.Close(); }
/*---------------------------------------------------------------------------------------------------*/ unsafe public bool createService(UDSServiceFormat udsservice) { udsCaller.address = udsservice.address; udsCaller.format = udsservice.format; //超过8字节 if (udsservice.parameterList.Count >= 7) { MessageBox.Show("参数超过7字节"); return(false); } usbCAN.arrSendBuf[0].ID = udsservice.address; usbCAN.arrSendBuf[0].ExternFlag = udsservice.format; usbCAN.arrSendBuf[0].RemoteFlag = 0; usbCAN.arrSendBuf[0].SendType = 1; usbCAN.arrSendBuf[0].DataLen = 8; usbCAN.arrSendBuf[0].Data[0] = (byte)(udsservice.parameterList.Count + 1); usbCAN.arrSendBuf[0].Data[1] = udsservice.serviceID; for (int d = 0; d < udsservice.parameterList.Count; d++) { usbCAN.arrSendBuf[0].Data[d + 2] = udsservice.parameterList[d]; } return(true); }
public Form_ModifyOptional(UDSServiceFormat uds, int pro) { InitializeComponent(); udsReq = uds; protocol = pro; readUdsReq(); }
/*---------------------------------------------------------------------------------------------------*/ public List <CALCULATIONRESULT> toGetResult(List <CAN_OBJ> dataPacket, UDSServiceFormat udsReq, double digit, int endian) { countIdClass(dataPacket); countUdsFrame(dataPacket); countFramePoint(dataPacket, udsReq); Train(digit, endian); makeResultOrderly(); return(calcResults); }
/*---------------------------------------------------------------------------------------------------*/ public void readUdsInstructions(string path) { listBoxOptional.Items.Clear(); listOptionals.Clear(); StreamReader sr = new StreamReader(path); string line = null; while ((line = sr.ReadLine()) != null) { UDSServiceFormat uds = new UDSServiceFormat(); string[] arr = line.Split('\t'); int len = arr.Length; int despSt = -1, despEd = -1; if ((despSt = arr[len - 1].IndexOf('"')) != -1) { len--; if ((despEd = arr[len].IndexOf('"', despSt + 1)) != -1) { uds.description = arr[len].Substring(despSt + 1, despEd - despSt - 1); } } try { uds.address = Convert.ToUInt32(arr[0], 16); } catch (Exception) { continue; } try { uds.serviceID = Convert.ToByte(arr[1], 16); } catch (Exception) { continue; } int l; for (l = 2; l < len; l++) { byte parm = 0; try { parm = Convert.ToByte(arr[l], 16); } catch (Exception) { break; } uds.parameterList.Add(parm); } if (l >= len) { listBoxOptional.Items.Add(uds.description); uds.description = uds.description.Replace("[OBD]", ""); uds.description = uds.description.Replace("[UDS]", ""); listOptionals.Add(uds); } } sr.Close(); }
/*---------------------------------------------------------------------------------------------------*/ public bool fetchStrSupported(byte externFlag) { if (usbCAN.startDevice() == false) { return(false); } for (int i = 0; i < arrStrSupported.Length; i++) { UDSServiceFormat uds = listOBD[0x20 * i]; uds.format = externFlag; if (uds.format == 0) { uds.address = 0x7DF; } else { uds.address = 0x18DB33F1; } uds.expectedFrame = 0; //单帧 UDSResponseList retClass = null; for (int reTry = 0; reTry < 10; reTry++) { DateTime prev = DateTime.Now; while ((DateTime.Now - prev).TotalMilliseconds < DELAY) { Application.DoEvents(); } retClass = udsClass.getResponseList(uds); if ((retClass.errorCode != UDSResponseList.ERRORCODE.SUCCEED) || (retClass.responseList.Count < 6) || (retClass.responseList[1] != uds.parameterList[0])) { continue; } break; } if ((retClass.errorCode != UDSResponseList.ERRORCODE.SUCCEED) || (retClass.responseList.Count < 6) || (retClass.responseList[1] != uds.parameterList[0])) { MessageBox.Show("初始化超时,请重试\n或者尝试切换帧格式"); return(false); } //address赋值 for (int g = 0x20 * i + 1; g < 0x20 * i + 0x20; g++) { if (uds.format == 0) { listOBD[g].address = retClass.respAddress - 0x08; } else { UInt32 TASA = retClass.respAddress & 0x0000FFFF; UInt32 addr = 0x18DA0000; TASA = (TASA >> 8) | ((TASA << 8) & 0x0000FF00); addr = addr | TASA; listOBD[g].address = addr; } listOBD[g].expectedFrame = 0; } arrStrSupported[i] = ""; for (int d = 0; d < 4; d++) { arrStrSupported[i] += Convert.ToString(retClass.responseList[d + 2], 2).PadLeft(8, '0'); } if (arrStrSupported[i][0x1f] == '0') { for (int j = i + 1; j < arrStrSupported.Length; j++) { arrStrSupported[j] = "00000000000000000000000000000000"; } break; } } if (usbCAN.shutDevice() == false) { return(false); } return(true); }
void countFramePoint(List <CAN_OBJ> dataPacket, UDSServiceFormat udsReq) { for (int i = 1; i < udsFrameIndex.Count - 1; i++) { int prevIndex = udsFrameIndex[i - 1]; int index = udsFrameIndex[i]; int nextIndex = udsFrameIndex[i + 1]; //检验UDS是否正确 CAN_OBJ udsFrame = dataPacket[index]; int valueIndex = 2, c; for (c = 0; c < udsReq.parameterList.Count; c++, valueIndex++) { if (udsReq.parameterList[c] != udsFrame.Data[valueIndex]) { break; } } if (c < udsReq.parameterList.Count) { continue; } //计算值 float udsValue = 0; int cnt = udsFrame.Data[0] & 0x0f; for (int d = valueIndex; d <= cnt; d++) { udsValue *= 256; udsValue += udsFrame.Data[d]; } int allIdCount; //找前面 allIdCount = idClasses.Count; setNotFoundFramePoint(); for (int p = index - 1; p > prevIndex && allIdCount > 0; p--) { IDCLASS idClass = idClasses.Find(x => x.id.Equals(dataPacket[p].ID)); if (idClass.hasFoundFramePoint == false) { idClass.hasFoundFramePoint = true; allIdCount--; idClass.framePoints.Add(new FRAMEPOINT(dataPacket[p], udsValue)); } } //找后面 allIdCount = idClasses.Count; setNotFoundFramePoint(); for (int n = index + 1; n < nextIndex && allIdCount > 0; n++) { IDCLASS idClass = idClasses.Find(x => x.id.Equals(dataPacket[n].ID)); if (idClass.hasFoundFramePoint == false) { idClass.hasFoundFramePoint = true; allIdCount--; idClass.framePoints.Add(new FRAMEPOINT(dataPacket[n], udsValue)); } } } }
private void buttonAddUDS_Click(object sender, EventArgs e) { byte externFlag = 0; if (radioButtonUS.Checked) { externFlag = 0; } else if (radioButtonUE.Checked) { externFlag = 1; } else { MessageBox.Show("请指定帧格式"); return; } uint canID = 0; try { canID = Convert.ToUInt32(textBoxCANID.Text, 16); } catch (Exception) { MessageBox.Show("请输入正确的16进制CAN ID"); return; } byte sID = 0; try { sID = Convert.ToByte(textBoxSID.Text, 16); } catch (Exception) { MessageBox.Show("请输入正确的16进制SID"); return; } List <byte> listParam = new List <byte>(); string sParams = textBoxPARAM.Text; for (int index = 0; index < sParams.Length;) { for (; index < sParams.Length && isHexLetter(sParams[index]) == false; index++) { ; } if (index >= sParams.Length) { break; } int end; for (end = index + 1; end < sParams.Length && isHexLetter(sParams[end]); end++) { ; } string sHex = sParams.Substring(index, end - index); byte bHex = 0; try { bHex = Convert.ToByte(sHex, 16); } catch (Exception) { MessageBox.Show("请输入正确的16进制参数"); return; } listParam.Add(bHex); index = end + 1; } if (listParam.Count == 0) { MessageBox.Show("参数不能为空"); return; } string desp = textBoxDES.Text; desp = desp.Replace(" ", ""); desp = desp.Replace("\t", ""); if (desp.Equals("")) { MessageBox.Show("描述不能为空"); return; } UDSServiceFormat uds = new UDSServiceFormat(); uds.address = canID; uds.expectedFrame = 0; uds.format = externFlag; uds.serviceID = sID; uds.description = desp; for (int i = 0; i < listParam.Count; i++) { uds.parameterList.Add(listParam[i]); } protocolType = 1; udsReq = uds; this.Close(); }
/*---------------------------------------------------------------------------------------------------*/ public UDSResponseList getResponseList(UDSServiceFormat udsservice) { DateTime prev; createService(udsservice); if (udsservice.expectedFrame == SINGLEFRAME) { responseListForSingleFrameExpected.responseList.Clear(); expectedReturnIsSingleFrame = true; usbCAN.sendFame(1); prev = DateTime.Now; while (expectedReturnIsSingleFrame) { if ((DateTime.Now - prev).TotalMilliseconds > DELAYTIME) { responseListForSingleFrameExpected.errorCode = UDSResponseList.ERRORCODE.SINGLETIMEOUT; //单帧超时 break; } Application.DoEvents(); } expectedReturnIsFirstFrame = false; return(responseListForSingleFrameExpected); } else { responseListForMultiFrameExpected.responseList.Clear(); expectedReturnIsFirstFrame = true; usbCAN.sendFame(1); prev = DateTime.Now; while (expectedReturnIsFirstFrame) { if ((DateTime.Now - prev).TotalMilliseconds > DELAYTIME) { responseListForMultiFrameExpected.errorCode = UDSResponseList.ERRORCODE.FIRSTTIMEOUT; break; } Application.DoEvents(); } if (udsservice.address == 0x7DF || responseListForMultiFrameExpected.errorCode == UDSResponseList.ERRORCODE.FIRSTTIMEOUT) { expectedReturnIsFirstFrame = expectedReturnIsConsecutiveFrame = false; return(responseListForMultiFrameExpected); } while (expectedReturnIsConsecutiveFrame) { if ((DateTime.Now - prev).TotalMilliseconds > DELAYTIME) { responseListForMultiFrameExpected.errorCode = UDSResponseList.ERRORCODE.MULTITIMEOUT; break; } Application.DoEvents(); } expectedReturnIsConsecutiveFrame = false; return(responseListForMultiFrameExpected); } }