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

📄 samplesource.c

📁 基于J1939的汽车CAN通讯测试,简单上位机软件源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
        }
      else if( iProtocol == 4 )  
        {
          strcpy( szProtocolName, "IESCAN" );
        }
      else if( iProtocol == 5 )  
        {
          strcpy( szProtocolName, "J1850" );
        }
      else
        {
        }
      
    } while( iProtocol < 1 || iProtocol > 5 );

  //
  // Have the user select the device number and set nUserDevice (make 150 the default)
  //

  printf("                                                                     \n");
  printf("                                                                      \n");
  printf("--------------------------------------------------------------------- \n");
  printf(" Select Device To Use                                                 \n");
  printf("--------------------------------------------------------------------- \n");
  printf("                                                                      \n");
  printf(" 150 =  DG DPA 4 or DPA 5  Using USB   (default)                      \n");
  printf(" 101 =  DG DPA II/II+/III+ Using COM1                                 \n");
  printf("                                                                      \n");
  printf(" These are the most common Dearborn Group devices.  Other device      \n");
  printf(" numbers exist in the DG121032.INI or DG1210B.INI files that can be   \n");
  printf(" found in the C:\\Windows directory.                                  \n");
  printf("                                                                      \n");
  printf("->  ");
  
  iUserDevice = atoi( gets( szTemp ) );
  
  if( iUserDevice == 0 )
    {
      iUserDevice = 150;
    }
  
  //
  // Ask user if they want to write to a disk file.
  //

  printf("                                                                      \n");
  printf("--------------------------------------------------------------------- \n");
  printf(" Recording of data to a disk file.                                    \n");
  printf("--------------------------------------------------------------------- \n");
  printf("                                                                      \n");
  printf(" Would you like to write the output of this program to a disk file?   \n");
  printf("                                                                      \n");
  printf("(y/n) or (1/0) ->  ");

  gets( szTemp );

  if( (toupper( szTemp[0] ) == 'Y') || (szTemp[0] == '1') )
    {
      printf("                                                  \n");
      printf(" Enter the name of the file to record/append to ->  ");
      
      gets( szTemp );

      if( NULL == (fpDiskFile = fopen( szTemp, "a" ) ) )
        {
          printf("Unable to open file [%s] for appending.  Exiting program.\n", szTemp );
          exit( 1 );
        }
    }
  else
    {
      fpDiskFile = NULL;
    }

  //
  // Load the DLL in szDLLName and set all function pointer variables (i.e. pRP1210_ClientConnect)
  //

  LoadRP1210LibraryAndFunctions(szDLLName);

  //
  // Get the current time to show we have connected or not connected successfully.
  //

  time( &Timer );

  //
  // Set nClientID by attempting to connect to nUserDevice using szProtocolName.
  //

  nClientID = pRP1210_ClientConnect(NULL_WINDOW, (short) iUserDevice, szProtocolName,0,0,0);
  
  if ( ( nClientID >= 0 ) && ( nClientID <= 127 ) )
    {
      fprintf(stdout,"\n");
      fprintf(stdout,"SUCCESS - RP1210_ClientConnect(NULL_WINDOW,UserDevice=[%d],ProtocolName=[%s],0,0,0);\n", iUserDevice, szProtocolName );
      fprintf(stdout,"Time Connected = %s", ctime( &Timer ) );
      fprintf(stdout,"\n");

      if( fpDiskFile != NULL )
        {
          fprintf(fpDiskFile,"\n");
          fprintf(fpDiskFile,"SUCCESS - RP1210_ClientConnect(NULL_WINDOW,UserDevice=[%d],ProtocolName=[%s],0,0,0);\n", iUserDevice, szProtocolName );
          fprintf(fpDiskFile,"Time Connected = %s", ctime( &Timer ) );
          fprintf(fpDiskFile,"\n");
        }
    }
  else
    {
      fprintf(stdout,"\n");
      fprintf(stdout,"FAILURE - RP1210_ClientConnect(NULL_WINDOW,UserDevice=[%d],ProtocolName=[%s],0,0,0);\n", iUserDevice, szProtocolName );
      fprintf(stdout,"Time Connect Attempted = %s", ctime( &Timer ) );
      fprintf(stdout,"\n");

      if( fpDiskFile != NULL )
        {
          fprintf(fpDiskFile,"\n");
          fprintf(fpDiskFile,"FAILURE - RP1210_ClientConnect(NULL_WINDOW,UserDevice=[%d],ProtocolName=[%s],0,0,0);\n", iUserDevice, szProtocolName );
          fprintf(fpDiskFile,"Time Connect Attempted = %s", ctime( &Timer ) );
          fprintf(fpDiskFile,"\n");
        }

      PrintRP1210Error( "RP1210_ClientConnect", nClientID, stdout, fpDiskFile );

      fclose( fpDiskFile );
        
      FreeLibrary( hRP1210DLL );

      exit(1);
    }
  
  //
  // Set all filter states to pass so that messages will flow.
  //

  SetAllFilterStatesToPass( nClientID, stdout, fpDiskFile );

  //
  // If we are on the J1939 network, we must claim our address before we can send.
  //

  if( 0 == strcmp( szProtocolName, "J1939" ) )
    {
      ClaimMyJ1939Address(stdout, fpDiskFile);
    }
  
  //
  // While the user does not press a key, send request messages every 5 seconds and print messages found
  // on the chosen data bus.
  //

  printf("---------------------------------------------------------------------\n");
  printf(" Ready to begin.  Press \"Enter\" to start, any key exits program.   \n");
  printf("            Press \"Control-S\" to freeze the screen.                \n");
  printf("---------------------------------------------------------------------\n");
  printf(">");
  gets(szTemp);

  //
  // Get the current time in tLastTimeRequestsSent.  Use this variable to help send 
  // request messages every 5 seconds.
  //

  time( &tLastTimeRequestsSent );
  
  while ( TRUE )
    {
      //
      // If user presses a key, break from the loop, cleanup and exit the program.
      //

      if( _kbhit() )
        break;

      //
      // Clear the transmit/receive message buffer of any previous information.
      //

      memset( ucTxRxBuffer, 0x00, sizeof( ucTxRxBuffer ) );
      
      //
      // Look and see if a message is on the data bus in NON_BLOCKING mode.  
      //     0 = No message on bus.
      //   < 0 = Error condition.
      //   > 0 = Message found on bus.
      //

      nRetVal = pRP1210_ReadMessage( nClientID, (char*) &ucTxRxBuffer[0], sizeof(ucTxRxBuffer), NON_BLOCKING_IO );
      
      if( nRetVal == 0 )
        {
          // No message found on bus, do nothing.
        }
      else if ( nRetVal > 0  ) 
        {
          if( 0 == strcmp( szProtocolName, "J1939" ) )
            {
              Process_J1939_Message( nClientID, stdout, fpDiskFile, ucTxRxBuffer, nRetVal, ECHO_OFF );
            }
          else if( 0 == strcmp( szProtocolName, "J1708" ) )
            {
              Process_J1708_Message( nClientID, stdout, fpDiskFile, ucTxRxBuffer, nRetVal, CONVERTED_MODE, ECHO_OFF );
            }
          else if( 0 == strcmp( szProtocolName, "CAN" ) )
            {
              Process_CAN_Message( nClientID, stdout, fpDiskFile, ucTxRxBuffer, nRetVal, ECHO_OFF );
            }
          else if( 0 == strcmp( szProtocolName, "IESCAN" ) )
            {
              Process_CAN_Message( nClientID, stdout, fpDiskFile, ucTxRxBuffer, nRetVal, ECHO_OFF );
            }
          else if( 0 == strcmp( szProtocolName, "J1850" ) )
            {
              Process_J1850_Message( nClientID, stdout, fpDiskFile, ucTxRxBuffer, nRetVal, ECHO_OFF );
            }
          else
            {
            }
        }
      else if ( nRetVal < 0 ) 
        {
          PrintRP1210Error( "RP1210_ReadMessage", (short) -nRetVal, stdout, fpDiskFile );
        }

      //
      // If the first time through, or if it has been more than 5 seconds, send out PGN or PID request messages.
      //

      time( &tCurrentTime );

      if(  (tCurrentTime > ( tLastTimeRequestsSent + 5 ) ) || ( bFirst == TRUE ) )
        {
          bFirst = FALSE;

          tLastTimeRequestsSent = tCurrentTime;

          if( 0 == strcmp( szProtocolName, "J1939" ) )
            {
              SendJ1939PeriodicMessages( nClientID, stdout, fpDiskFile );
            }
          else if( 0 == strcmp( szProtocolName, "J1708" ) )
            {
              SendJ1708PeriodicMessages( nClientID, stdout, fpDiskFile );
            }
          else if( 0 == strcmp( szProtocolName, "CAN" ) )
            {
              SendCANPeriodicMessages( nClientID, stdout, fpDiskFile );
            }
          else if( 0 == strcmp( szProtocolName, "IESCAN" ) )
            {
              SendCANPeriodicMessages( nClientID, stdout, fpDiskFile );
            }
          else if( 0 == strcmp( szProtocolName, "J1850" ) )
            {
              SendJ1850PeriodicMessages( nClientID, stdout, fpDiskFile );
            }
        }
      
      //
      // Always a good idea to yield the processor to other processes.
      // 
      Sleep(10);
    
    }// Main "while" loop.

⌨️ 快捷键说明

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