⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mobydic.c

📁 OBD软件给Palm平台系统所使用范例程式.
💻 C
字号:
// MobyPalm
// Copyright (C) 2005 Olivier Burgard

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include <PalmOS.h>

#include "Serial.h"
#include "Mobydic.h"
#include "Common.h"

static char MobydicVersion[10];
static BOOL MobydicVersionLoaded=false;
static char ChipIdent[10];
static BOOL ChipIdentLoaded=false;
static char FoundProtocol[30];
static BOOL FoundProtocolLoaded=false;
static char SupportOBD[30];
static BOOL SupportOBDLoaded=false;
static UInt8 SupportedPID[32];
static BOOL SupportedPIDLoaded=false;

BOOL checkMobydic(void) {
	UInt8 msg[1];
	UInt8 rcv[3];
	
	msg[0]=0x04;
	rcv[0]=0;
	
	SerialOpen(9600);
	SerialWrite(msg, sizeof(msg));
	SerialRead(rcv, sizeof(rcv), 1);
	SerialClose();
	
	if (rcv[0]==0x04)
		return true;
	
	return false;
}

char* readMobydicVersion(void) {
	UInt8 msg[1];
	UInt8 rcv[3];
	UInt16 ver;
	Char str[6];
	Char minor[3];
	
	if (!MobydicVersionLoaded) {
		MemSet(MobydicVersion,sizeof(MobydicVersion),0);
		msg[0]=0x04;
		rcv[0]=0;
		
		SerialOpen(9600);
		SerialWrite(msg, sizeof(msg));
		SerialRead(rcv, sizeof(rcv), SysTicksPerSecond()*2);
		SerialClose();
		
		if (rcv[0]==0x04) {
			ver=rcv[1]*256 + rcv[2];
			StrPrintF(str, "%u", ver);
			minor[0]=str[StrLen(str)-2];
			minor[1]=str[StrLen(str)-1];
			str[StrLen(str)-2]=0;
			StrPrintF(MobydicVersion,"%s.%s",str, minor);
			MobydicVersionLoaded=true;
			
			return MobydicVersion;
		}
		
		return "Erreur...\0";
	}
	
	return MobydicVersion;
}

char* readChipIdent(void) {
	UInt8 msg[1];
	UInt8 rcv[3];
	UInt16 ver;
	
	if (!ChipIdentLoaded) {
		MemSet(ChipIdent,sizeof(ChipIdent),0);
		msg[0]=0x06;
		rcv[0]=0;
		
		SerialOpen(9600);
		SerialWrite(msg, sizeof(msg));
		SerialRead(rcv, sizeof(rcv), SysTicksPerSecond()*2);
		SerialClose();
		
		if (rcv[0]==0x06) {
			ver=rcv[1]*256 + rcv[2];
			
			switch (ver) {
				case 0x0A28:
					StrPrintF(ChipIdent,"OE90C2600");
					break;
				default:
					StrPrintF(ChipIdent,"Inconnu");
					break;
			}
			ChipIdentLoaded=true;
			
			return ChipIdent;
		}
		
		return "Erreur...\0";
	}
	
	return ChipIdent;
}

char* readFoundProtocol(void) {
	UInt8 msg[1];
	UInt8 rcv[3];
	UInt16 protocol;
	
	if (!FoundProtocolLoaded) {
		MemSet(FoundProtocol,sizeof(FoundProtocol),0);
		msg[0]=0x05;
		rcv[0]=0;
		
		SerialOpen(9600);
		SerialWrite(msg, sizeof(msg));
		SerialRead(rcv, sizeof(rcv), SysTicksPerSecond()*2);
		SerialClose();
		
		if (rcv[0]==0x05) {
			protocol = 256*rcv[1]+rcv[2];
			
			switch (protocol) {
				case 0x0000:
					StrPrintF(FoundProtocol,"No Protocol");
					break;
				case 0x0001:
					StrPrintF(FoundProtocol,"ISO9141-2 keywords 08 08");
					break;
				case 0x0002:
					StrPrintF(FoundProtocol,"ISO9141-2 keywords 94 94");
					break;
				case 0x0004:
					StrPrintF(FoundProtocol,"KWP2000 slow init");
					break;
				case 0x0008:
					StrPrintF(FoundProtocol,"KWP2000 fast init");
					break;
				case 0x0010:
					StrPrintF(FoundProtocol,"J1850 PWM");
					break;
				case 0x0020:
					StrPrintF(FoundProtocol,"J1850 VPWM");
					break;
				case 0x0040:
					StrPrintF(FoundProtocol,"CAN 11 ident 250 KB");
					break;
				case 0x0080:
					StrPrintF(FoundProtocol,"CAN 11 ident 500 KB");
					break;
				case 0x0100:
					StrPrintF(FoundProtocol,"CAN 29 ident 250 KB");
					break;
				case 0x0200:
					StrPrintF(FoundProtocol,"CAN 29 ident 500 KB");
					break;
				case 0x0400:
					StrPrintF(FoundProtocol,"SAE J1939");
					break;
				case 0x0800:
					StrPrintF(FoundProtocol,"KW1281 / KW71");
					break;
				case 0x1000:
					StrPrintF(FoundProtocol,"KW82");
					break;
				default:
					StrPrintF(FoundProtocol,"Erreur...");
					break;
			}
			FoundProtocolLoaded=true;
			
			return FoundProtocol;
		}
		
		return "Erreur...\0";
	}
	
	return FoundProtocol;
}

char* readSupportOBD(void) {
	int msg[2];
	UInt8 rcv[9];
	int nbanswer;
	
	if (!SupportOBDLoaded) {
		MemSet(SupportOBD,sizeof(SupportOBD),0);
		msg[0]=0x1C;
		nbanswer=sendGenericOBDCommand(1, msg, 1, rcv);
			
		if (nbanswer>0) {
			switch (rcv[5]) {
				case 0x01:
					StrPrintF(SupportOBD,"OBD II");
					break;
				case 0x02:
					StrPrintF(SupportOBD,"OBD");
					break;
				case 0x03:
					StrPrintF(SupportOBD,"OBD and OBD II");
					break;
				case 0x04:
					StrPrintF(SupportOBD,"OBD I");
					break;
				case 0x05:
					StrPrintF(SupportOBD,"Not OBD compliant");
					break;
				case 0x06:
					StrPrintF(SupportOBD,"EOBD");
					break;
				case 0x07:
					StrPrintF(SupportOBD,"EOBD and OBD II");
					break;
				case 0x08:
					StrPrintF(SupportOBD,"EOBD and OBD");
					break;
				case 0x09:
					StrPrintF(SupportOBD,"EOBD, OBD and OBD II");
					break;
				case 0x0A:
					StrPrintF(SupportOBD,"JOBD");
					break;
				case 0x0B:
					StrPrintF(SupportOBD,"JOBD and OBD II");
					break;
				case 0x0C:
					StrPrintF(SupportOBD,"JOBD and EOBD");
					break;
				case 0x0D:
					StrPrintF(SupportOBD,"JOBD, EOBD and OBD II");
					break;
				default:
					StrPrintF(SupportOBD,"Inconnu");
					break;
				}
				SupportOBDLoaded=true;
				
				return SupportOBD;
		}
		
		return "Pas de r閜onse...\0";
	}
	
	return SupportOBD;
}

BOOL loadSupportedPIDPage(int page) {
	int msg[2];
	UInt8 rcv[12];
	int nbanswer;
	
	MemSet(rcv,sizeof(rcv),0);
	
	msg[0]=32*page;
	nbanswer=sendGenericOBDCommand(1, msg, 1, rcv);
		
	if (nbanswer>0) {
		SupportedPID[4*page]=rcv[DataA];
		SupportedPID[4*page+1]=rcv[DataB];
		SupportedPID[4*page+2]=rcv[DataC];
		SupportedPID[4*page+3]=rcv[DataD];
		if (page<7 && (rcv[DataD] & 0x01)) return loadSupportedPIDPage(page+1);
		else return true;
	}

	return false;
}

int sendGenericOBDCommand(int mode, int params[], int nbparams, UInt8 answer[]) {
	UInt8 msg[7];
	int msglen=0;
	UInt8 rcv[30];
	int received = 0;
	
	SerialOpen(9600);
	msg[0]=0x02;
	rcv[0]=0;
	SerialWrite(msg, 1);
	SerialRead(rcv, 1, SysTicksPerSecond()*2);
	if (rcv[0]==0x06) {
		msg[0]=mode;
		rcv[0]=0;
		msglen=0;
		switch (mode) {
			case 1:
				if (nbparams!=1) break;
				msg[1]=params[0];
				msglen=2;
				break;
			case 2:
				if (nbparams!=1) break;
				msg[1]=params[0];
				msglen=2;
				break;	
			case 3:
				if (nbparams!=0) break;
				msglen=1;
				break;	
			case 4:
				if (nbparams!=0) break;
				msglen=1;
				break;	
			case 5:
				if (nbparams!=2) break;
				msg[1]=params[0];
				msg[2]=params[1];
				msglen=3;
				break;	
			case 6:
				if (nbparams!=1) break;
				msg[1]=params[0];
				msglen=2;
				break;	
			case 7:
				if (nbparams!=0) break;
				msglen=1;
				break;	
			case 8:
				if (nbparams!=6) break;
				msg[1]=params[0];
				msg[2]=params[1];
				msg[3]=params[2];
				msg[4]=params[3];
				msg[5]=params[4];
				msg[6]=params[5];
				msglen=7;
				break;	
			case 9:
				if (nbparams!=1) break;
				msg[1]=params[0];
				msglen=2;
				break;	
		}

		if (msglen>0) {
			SerialWrite(msg, msglen);
			SerialRead(rcv, 1, SysTicksPerSecond()*2);
		
			if (rcv[0]==0x02) {
				rcv[0]=0;
				SerialRead(rcv, 1, SysTicksPerSecond()*2);
				
				if (rcv[0]!=0) {
					SerialRead(answer, rcv[0], SysTicksPerSecond()*2);
					received=rcv[0];
				}
			}
		}
	}
	
	SerialClose();
	
	return received;
}

BOOL loadSupportedPID(){
	if (!SupportedPIDLoaded) {
		MemSet(SupportedPID,sizeof(SupportedPID),0);
		SupportedPIDLoaded=loadSupportedPIDPage(0);
	}
	
	return SupportedPIDLoaded;
}

BOOL isPIDSupported(int pid) {
	return (SupportedPID[(int)pid/8] >> (pid-((int)pid/8)*8) & 0x01);
}

void setMobydicVersionToLabel(UInt16 LabelID){
	SetLabelText(FrmGetFormId(FrmGetActiveForm()), LabelID, readMobydicVersion());
}

void setChipIdentToLabel(UInt16 LabelID){
	SetLabelText(FrmGetFormId(FrmGetActiveForm()), LabelID, readChipIdent());
}

void setFoundProtocolToLabel(UInt16 LabelID){
	SetLabelText(FrmGetFormId(FrmGetActiveForm()), LabelID, readFoundProtocol());
}

void setSupportOBDToLabel(UInt16 LabelID){
	SetLabelText(FrmGetFormId(FrmGetActiveForm()), LabelID, readSupportOBD());
}

void setSupportedPIDToLabels(UInt16 LabelID1, UInt16 LabelID2){
	Char temp[40];
	
	convertHexToChars(SupportedPID, 16, temp);
	SetLabelText(FrmGetFormId(FrmGetActiveForm()), LabelID1, temp);
	
	convertHexToChars(SupportedPID+16*sizeof(UInt8), 16, temp);
	SetLabelText(FrmGetFormId(FrmGetActiveForm()), LabelID2, temp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -