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

📄 diag_test.c

📁 Freediag contains various drivers (ISO9141, ISO14230, SAEJ1850-VPW, SAEJ1850-PWM) for different adap
💻 C
字号:
/*
 *	freediag - Vehicle Diagnostic Utility
 *
 *
 * Copyright (C) 2001 Richard Almeida & Ibex Ltd (rpa@ibex.co.uk)
 *
 * 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 of 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *************************************************************************
 *
 * Diag library test harness
 */

#include "diag_os.h" /* operating specific includes */

#include <stdlib.h>
#include <stdio.h>

#include <fcntl.h>
#include <string.h>
#include <errno.h>

#include "diag.h"
#include "diag_l1.h"
#include "diag_l2.h"
#include "diag_iso14230.h"
#include "diag_err.h"

#include <signal.h>

#define BAUD 5

extern int diag_l0_debug;
extern int diag_l1_debug;
extern int diag_l2_debug;

static char *cvsid = "$Id: diag_test.c,v 1.3 2002/04/03 04:56:40 bjorn_helgaas Exp $";

#define TESTER_ID	0xF1

int
do_l2_raw_test(int funcaddr, int destecu, int inittype);

u_int8_t global_data[1024];
int global_datalen;


u_int8_t	global_pids[0x100];	/* Pids supported by ECU */



void
alarm_handler(int sig)
{
	alarm(1);
}

main(int argc, char **argv)
{
	int i;
	struct sigaction stNew;

	diag_l0_debug = 0xff;
	diag_l1_debug = 0xff;
	diag_l2_debug = 0xff;

	memset(&stNew, 0, sizeof(stNew));
	stNew.sa_handler = alarm_handler;
	stNew.sa_flags = 0;
	sigaction(SIGALRM, &stNew, NULL);
	alarm(1);

#if 1
	for (i=33 ; i < 0x100; i++)
	{
		do_l2_raw_test(0, i, DIAG_L2_TYPE_SLOWINIT);
	}
#else
	do_l2_raw_test(0, 32, DIAG_L2_TYPE_SLOWINIT);
#endif
    
}

void
l2_rcv(void *handle, diag_msg_t *msg)
{
	int len = msg->len;
	u_int8_t *data = msg->data;

	/* Store the data */
	memcpy(global_data, data, len);
	global_datalen = len;

	printf("Got %d bytes of data, src %x, dest %x !! - ", len,
		msg->src, msg->dest);

	while (len)
	{
		printf("0x%x ", *data);
		len--; data++;
	}

	printf("\n");
	
}


do_l2_raw_test(int funcaddr, int destecu, int inittype)
{
	int fd, rv, i;
	u_int8_t csum;
	diag_l2_conn_t *d_conn;
	diag_msg_t	msg;
	u_int8_t data[100];

	struct diag_l2_ic ic;
	struct diag_l1_init in;
	int flags = 0;


	rv = diag_l2_init();
	printf("init rv = 0x%x\n", rv);

	rv = diag_l2_open("SE9141", 0, 0);
	printf("open rv = 0x%x\n", rv);
	fd = rv;

	/*
	 * Initiate a RAW communications layer on the
	 * ISO9141 device we opened above
	 */
	if (funcaddr)
		flags |= DIAG_L2_TYPE_FUNCADDR;

	flags |= inittype;

#define DO_ISO 1

#if DO_ISO
	d_conn = diag_l2_StartCommunications(fd, DIAG_L2_PROT_ISO14230, flags,
		10400, destecu, 0xF1);
#else
	d_conn = diag_l2_StartCommunications(fd, DIAG_L2_PROT_RAW, flags,
		10400, destecu, 0xF1);
#endif


	if (d_conn) {
		printf("Connection to ECU established (con. %x)\n", d_conn);
printf("For %d %d\n", funcaddr, destecu);
	}
	else {
		diag_l2_close(fd);
		printf("Connection to ECU failed (con. %x)\n", d_conn);
		return(0);
	}

#if !DO_ISO

#define SLOWINIT

#ifdef SLOWINIT
	/* Now set it to 5 baud */
	ic.speed = 5;
	ic.databits = 7;
	ic.stopbits = 1;
	ic.parityflag = DIAG_L1_PAR_O ;

	diag_l2_ioctl(d_conn, DIAG_IOCTL_SETSPEED, &ic);

	/* Send init msg */
	data[0] = destecu;
	msg.len = 1;
	msg.data = data;
#else

	memset(data, 0, sizeof(data));
	if (funcaddr)
		data[0]=0xC1;
	else
		data[0]=0x81;
	data[1]=destecu;
	data[2]=TESTER_ID;	/* Source- docs recommend F1, but my scanner uses that */
	data[3]=0x81;	/* Request */

#define DATALEN 4

	csum = 0;
	for (i=0; i < DATALEN; i++)
		csum += data[i];
	data[DATALEN] = csum;

	msg.len = DATALEN + 1;
	msg.data = data;

	/* Do fast init stuff */
	in.type = inittype;
	diag_l2_ioctl(d_conn, DIAG_IOCTL_INITBUS, &in);
	/* And immediately send the StartCommunications msg */
#endif
	diag_l2_send(d_conn, &msg);

#ifdef SLOWINIT
	/* Change to 10400 baud */
	ic.speed = 10400;
	ic.databits = 8;
	ic.stopbits = 1;
	ic.parityflag = DIAG_L1_PAR_N ;

	diag_l2_ioctl(d_conn, DIAG_IOCTL_SETSPEED, &ic);
#endif

#endif /* DO_ISO */
	sleep(5);

	/* See what we got */
	rv = diag_l2_recv(d_conn, 2000, l2_rcv, NULL);
	if (rv < 0)
		printf("diag_l2_recv returns %d\n", rv);
	else
	{
		printf("Got response, destecu %d\n", destecu);
		exit(0);
	}
}

do_l1_test()
{
	int fd;
	int rv;
	char buf[1024];
	int rcvd;

	rv = diag_l1_init();
	printf("init rv = 0x%x\n", rv);

	rv = diag_l1_open("SE9141", 0, 0);
	printf("open rv = 0x%x\n", rv);
	fd = rv;

	rv = diag_l1_setspeed(fd, BAUD, 8, 2, DIAG_L1_PAR_N);
	printf("setspeed rv = 0x%x\n", rv);

	printf("sleeping for 5\n");
	sleep(5);

	diag_l1_send(fd, 0, "hello", 6, 4);

	printf("sent hello\n waiting for TX to finish\n");

	/* Wait for transmit to finish */
	if (BAUD < 500)
		sleep(15);
	else
		sleep(5);

	rcvd = diag_l1_recv(fd, 0, &buf[0], 100, 10);
	buf[rcvd] = 0;

	printf("got %d bytes %s\n", rcvd, buf);

	printf("sleeping for 5\n");
	sleep(5);

	diag_l1_close(fd);

}

⌨️ 快捷键说明

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