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

📄 about.c

📁 国外一个非常经典的OBD2车辆检测的实例源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <string.h>
#include "globals.h"
#include "custom_gui.h"
#include "serial.h"
#include "sensors.h"
#include "options.h"
#include "version.h"
#include "about.h"

#define MSG_REFRESH  MSG_USER

static int logo_proc(int msg, DIALOG *d, int c);
static int large_text_proc(int msg, DIALOG *d, int c);
static int about_this_computer_proc(int msg, DIALOG *d, int c);
static int obd_info_proc(int msg, DIALOG *d, int c);
static int refresh_proc(int msg, DIALOG *d, int c);
static int obd_info_getter_proc(int msg, DIALOG *d, int c);
static int thanks_proc(int msg, DIALOG *d, int c);

static char whatisit[256];
static char whatcanitdo[256];
static char wheretoget[256];

static char obd_interface[16];
static char obd_mfr[64];
static char obd_protocol[64];
static char obd_system[64];

#define VER_STR   "Version " SCANTOOL_VERSION_EX_STR " for " SCANTOOL_PLATFORM_STR ", " SCANTOOL_COPYRIGHT_STR

static DIALOG about_dialog[] =
{
   /* (proc)                   (x)  (y)  (w)  (h)  (fg)     (bg)          (key) (flags) (d1) (d2) (dp)                    (dp2) (dp3) */
   { d_clear_proc,             0,   0,   0,   0,   0,       C_WHITE,       0,    0,      0,   0,   NULL,                   NULL, NULL },
   { logo_proc,                105, 25,  430, 58,  0,       0,             0,    0,      0,   0,   NULL,                   NULL, NULL },
   { st_ctext_proc,            320, 86,  216, 18,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   VER_STR,                NULL, NULL },
   { large_text_proc,          25,  112, 256, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "What is it?",          NULL, NULL },
   { super_textbox_proc,       25,  136, 407, 80,  C_BLACK, C_WHITE,       0,    0,      0,   0,   whatisit,               NULL, NULL },
   { large_text_proc,          25,  224, 256, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "What can it do?",      NULL, NULL },
   { super_textbox_proc,       25,  248, 407, 98,  C_BLACK, C_WHITE,       0,    0,      0,   0,   whatcanitdo,            NULL, NULL },
   { large_text_proc,          25,  354, 256, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "Where can I get it?",  NULL, NULL },
   { super_textbox_proc,       25,  378, 590, 80,  C_BLACK, C_WHITE,       0,    0,      0,   0,   wheretoget,             NULL, NULL },
   { d_box_proc,               440, 136, 175, 231, C_BLACK, C_LIGHT_GRAY,  0,    0,      0,   0,   NULL,                   NULL, NULL },
   { about_this_computer_proc, 448, 144, 160, 48,  C_BLACK, C_GREEN,       'c',  D_EXIT, 0,   0,   "About this &computer", NULL, NULL },
   { obd_info_proc,            448, 200, 160, 48,  C_BLACK, C_DARK_YELLOW, 'o',  D_EXIT, 0,   0,   "&OBD Information",     NULL, NULL },
   { thanks_proc,              448, 256, 160, 48,  C_BLACK, C_PURPLE,      'r',  D_EXIT, 0,   0,   "C&redits",             NULL, NULL },
   { d_button_proc,            448, 312, 160, 48,  C_BLACK, C_GREEN,       'm',  D_EXIT, 0,   0,   "Main &Menu",           NULL, NULL },
   { NULL,                     0,   0,   0,   0,   0,       0,             0,    0,      0,   0,   NULL,                   NULL, NULL }
};

static DIALOG thanks_dialog[] =
{
   /* (proc)            (x)  (y)  (w)  (h)  (fg)   (bg)            (key) (flags) (d1) (d2) (dp)      (dp2) (dp3) */
   { d_shadow_box_proc, 0,   0,   608, 266, C_BLACK, C_LIGHT_GRAY,  0,    0,      0,   0,   NULL,     NULL, NULL },
   { d_shadow_box_proc, 0,   0,   608, 24,  C_BLACK, C_DARK_GRAY,   0,    0,      0,   0,   NULL,     NULL, NULL },
   { caption_proc,      304, 2,   301, 19,  C_WHITE, C_TRANSP,      0,    0,      0,   0,   "We would like to thank the following people and organizations:", NULL, NULL },
   { d_text_proc,       24,  36,  560, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "- DJ Delorie (www.delorie.com) for the DJGPP compiler ", NULL, NULL },
   { d_text_proc,       24,  60,  560, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "- Bloodshed Software (www.bloodshed.net) for the Dev-C++ IDE", NULL, NULL },
   { d_text_proc,       24,  84,  560, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "- Shawn Hargreaves for creating the Allegro library", NULL, NULL },
   { d_text_proc,       24,  108, 560, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "- Dim Zegebart and Neil Townsend for the DZComm serial library", NULL, NULL },
   { d_text_proc,       24,  132, 560, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "- Julien Cugniere for his Allegro dialog editor", NULL, NULL },
   { d_text_proc,       24,  156, 560, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "- Eric Botcazou and Allegro mailing list folks for their tips and suggestions", NULL, NULL },
   { d_text_proc,       24,  180, 560, 24,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "- All users who provided feedback and bug reports", NULL, NULL },
   { d_button_proc,     248, 211, 112, 40,  C_BLACK, C_DARK_YELLOW, 'c',  D_EXIT, 0,   0,   "&Close", NULL, NULL },
   { NULL,              0,   0,   0,   0,   0,       0,             0,    0,      0,   0,   NULL,     NULL, NULL }
};

static DIALOG obd_info_dialog[] =
{
   /* (proc)               (x)  (y)  (w)  (h)  (fg)     (bg)           (key) (flags) (d1) (d2) (dp)               (dp2) (dp3) */
   { d_shadow_box_proc,    0,   0,   444, 188, C_BLACK, C_LIGHT_GRAY,  0,    0,      0,   0,   NULL,              NULL, NULL },
   { d_shadow_box_proc,    0,   0,   444, 24,  C_BLACK, C_DARK_GRAY,   0,    0,      0,   0,   NULL,              NULL, NULL },
   { caption_proc,         222, 2,   218, 19,  C_WHITE, C_TRANSP,      0,    0,      0,   0,   "OBD Information", NULL, NULL },
   { d_rtext_proc,         12,  36,  108, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "Interface:",      NULL, NULL },
   { d_text_proc,          124, 36,  316, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   obd_interface,     NULL, NULL },
   { d_rtext_proc,         12,  60,  108, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "Manufacturer:",   NULL, NULL },
   { d_text_proc,          124, 60,  316, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   obd_mfr,           NULL, NULL },
   { d_rtext_proc,         12,  84,  108, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "Protocol:",       NULL, NULL },
   { d_text_proc,          124, 84,  316, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   obd_protocol,      NULL, NULL },
   { d_rtext_proc,         12,  108, 108, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   "OBD System:",     NULL, NULL },
   { d_text_proc,          124, 108, 316, 16,  C_BLACK, C_TRANSP,      0,    0,      0,   0,   obd_system,        NULL, NULL },
   { refresh_proc,         140, 144, 76,  32,  C_BLACK, C_GREEN,       0,    D_EXIT, 0,   0,   "Refresh",         NULL, NULL },
   { d_button_proc,        231, 144, 76,  32,  C_BLACK, C_DARK_YELLOW, 0,    D_EXIT, 0,   0,   "Close",           NULL, NULL },
   { obd_info_getter_proc, 0,   0,   0,   0,   0,       0,             0,    0,      0,   0,   NULL,              NULL, NULL },
   { NULL,                 0,   0,   0,   0,   0,       0,             0,    0,      0,   0,   NULL,              NULL, NULL }
};


int display_about()
{
   strcpy(whatisit, "ScanTool.net is multi-platform, open-source software designed to work with the ElmScan family of OBD-II interfaces, our inexpensive alternatives to professional diagnostic scan tools.");
   sprintf(whatcanitdo, "ScanTool.net v%s allows you to read and clear trouble codes, and display real-time sensor data.  Software with more advanced features, such as data logging,  virtual dashboard, and support for additional test modes is available from third-party vendors.", SCANTOOL_VERSION_STR);
   strcpy(wheretoget, "You can download the latest version of this software, and buy your scan tool from www.ScanTool.net. There, you can also find contact information for your local distributor, and links to third-party software.");

   return do_dialog(about_dialog, -1); // do the dialog
}


int logo_proc(int msg, DIALOG *d, int c)
{
   switch (msg)
   {
      case MSG_START:
         d->dp = create_bitmap(430, 58);
         pivot_sprite(d->dp, datafile[LOGO_BMP].dat, 430, 0, 0, 0, itofix(64));
         break;
         
      case MSG_END:
         destroy_bitmap(d->dp);
         break;
   }
   
   return d_bitmap_proc(msg, d, c);
}


int obd_info_proc(int msg, DIALOG *d, int c)
{
   int ret = d_button_proc(msg, d, c);

   if (msg == MSG_START)
      centre_dialog(obd_info_dialog);

   if (ret == D_CLOSE)
   {
      if (comport.status == READY)
      {
         obd_interface[0] = 0;
         obd_mfr[0] = 0;
         obd_protocol[0] = 0;
         obd_system[0] = 0;

         popup_dialog(obd_info_dialog, -1);
      }
      else
      {
         while (comport.status != READY)
         {
            if (alert("Port is not ready.", "Please check that you specified the correct port", "and that no other application is using it", "Configure &Port", "&Cancel", 'p', 'c') == 1)
               display_options(); // let the user choose correct settings
            else
               return D_REDRAWME;
         }
      }

      return D_O_K;
   }

   return ret;
}

int refresh_proc(int msg, DIALOG *d, int c)
{
   int ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE)
   {
      broadcast_dialog_message(MSG_REFRESH, 0);
      return D_O_K;
   }
      
   return ret;
}


void clear_obd_info()
{
   strcpy(obd_interface, "N/A");
   strcpy(obd_mfr, "N/A");
   strcpy(obd_protocol, "N/A");
   strcpy(obd_system, "N/A");
}


void format_id_string(char *str)
{
   if (strncmp(str, "ELM32", 4) == 0)
   {
      memmove(str + 7, str + 6, strlen(str) - 5);
      str[6] = ' ';
   }
}


// OBD info getter states
#define OBD_INFO_IDLE         0
#define OBD_INFO_START        1
#define OBD_INFO_TX_0100      2
#define OBD_INFO_WAIT_ATZ     3
#define OBD_INFO_ECU_TIMEOUT  4
#define OBD_INFO_WAIT_0100    5
#define OBD_INFO_WAIT_011C    6

#define OBD_INFO_MAX_RETRIES  3

int obd_info_getter_proc(int msg, DIALOG *d, int c)
{
   static int state = OBD_INFO_START;
   static int device = 0;
   static int retries = 0;
   static char response[256];
   char buf[128];
   int status;

   switch (msg)
   {
      case MSG_START:
      case MSG_REFRESH:
         state = OBD_INFO_START;
         break;
         
      case MSG_IDLE:
         switch (state)
         {
            case OBD_INFO_IDLE:
               break;

            case OBD_INFO_START:
               strcpy(obd_interface, "detecting...");
               strcpy(obd_mfr, empty_string);
               strcpy(obd_protocol, empty_string);
               strcpy(obd_system, empty_string);
               retries = 0;
               state = OBD_INFO_TX_0100;
               return D_REDRAW;

            case OBD_INFO_TX_0100:
               if (serial_timer_running)
               {
                  // wait until we either get a prompt or the serial timer times out
                  while ((read_comport(buf) != PROMPT) && !serial_time_out)
                     ;
               }
               
               send_command("ati"); // get chip ID
               start_serial_timer(AT_TIMEOUT);
               device = 0;
               response[0] = 0;

               while ((status = read_comport(buf)) != PROMPT && !serial_time_out)
               {
                  if(status == DATA) // if new data detected in com port buffer
                     strcat(response, buf); // append contents of buf to response
               }

               if (status == PROMPT)  // if we got the prompt
               {
                  stop_serial_timer();
                  strcat(response, buf);
                  status = process_response("ati", response);

                  if (status < INTERFACE_ID)
                  {
                     send_command("atz"); // reset chip
                     start_serial_timer(ATZ_TIMEOUT);
                     response[0] = 0;
                     state = OBD_INFO_WAIT_ATZ;
                  }
                  else
                  {
                     format_id_string(response);
                     strcpy(obd_interface, response);

                     if ((device = status) == INTERFACE_ELM327)
                     {
                        send_command("at@1"); // get mfr string
                        start_serial_timer(AT_TIMEOUT);
                        response[0] = 0;

                        while ((status = read_comport(buf)) != PROMPT && !serial_time_out)
                        {
                           if(status == DATA) // if new data detected in com port buffer
                              strcat(response, buf); // append contents of buf to response
                        }

                        if (status == PROMPT)  // if we got the prompt
                        {
                           stop_serial_timer();
                           strcat(response, buf);

                           response[strlen(response) - 2] = 0;
                           strcpy(obd_mfr, response);
                           
                           send_command("atsp0");
                           start_serial_timer(AT_TIMEOUT);

                           while ((status = read_comport(buf)) != PROMPT && !serial_time_out)
                              ;

                           if (status == PROMPT)
                           {
                              start_serial_timer(ECU_TIMEOUT);
                              strcpy(obd_protocol, "waiting for ECU timeout...");
                              state = OBD_INFO_ECU_TIMEOUT;
                              return D_REDRAW;
                           }
                           else  // if serial timeout
                           {
                              stop_serial_timer();
                              if (alert("Connection to interface was lost", NULL, NULL, "&Retry", "&Cancel", 'r', 'c') == 1)
                                 state = OBD_INFO_START;
                              else
                              {
                                 clear_obd_info();
                                 state = OBD_INFO_IDLE;
                                 return D_REDRAW;
                              }
                           }
                        }
                        else  // if serial timeout
                        {
                           stop_serial_timer();
                           if (alert("Connection to interface was lost", NULL, NULL, "&Retry", "&Cancel", 'r', 'c') == 1)
                              state = OBD_INFO_START;
                           else
                           {
                              clear_obd_info();
                              state = OBD_INFO_IDLE;
                              return D_REDRAW;
                           }
                        }
                     }
                     else
                        strcpy(obd_mfr, "N/A");
                        
                     send_command("0100");
                     start_serial_timer(OBD_REQUEST_TIMEOUT);

⌨️ 快捷键说明

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