USB/CAN FD initialization

This forum covers issues concerning multiple software products.

USB/CAN FD initialization

Postby Elthes » Thu 25. May 2017, 22:56

I've been working through some code and have been running into similar issues with initialization and opening a channel.

Or trying to initialize when the PCAN-View software has the channel versus the code that I've been working on.

It seems to me that when I place some of the functions in a helper function to clean up the code that is in main problems start up.

Also is there some way to scan which USBBUS the program needs to initialize to?

I'm sure I can come up with a few more questions but I'll leave my questions here.

Thanks!

Code: Select all
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
#include <stdio.h>

#include "windows.h"
#include "PCANBasic.h"
#include "main.h"

//#define _WIN32_WINNT 0x0500

using namespace std;

//int ProcessMessage(TPCANMsg msg) {
//   return 0;
//}
//
//int HandleReadError(TPCANStatus result) {
//   return 0;
//
//}

int main()
{
   int test = 0;
   int count = 0;
while(test != 1) /* Loop forever - Test code */
{
TPCANMsg msgList[50]; //TPCANMsg msg; // Fill the list "msgList" with messages as you need
TPCANMsg msgRX;
TPCANMsg msgTX;
TPCANStatus result = 0;
char strMsg[256];
char line[256];
TPCANTimestamp timestamp;
FILE *fp = NULL;
unsigned char messageType = NULL;
bool xmtFileFormat = false;

   
/* The Plug & Play Channel (PCAN-USB) is initialized                     */
result = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K);                     
if (result != PCAN_ERROR_OK)
{
   // An error occurred, get a text describing the error and show it
   CAN_GetErrorText(result, 0, strMsg);
   std::wcout<<(strMsg)<<endl;
}
else
   std::wcout<<("PCAN-USB (Ch-1) was initialized")<<endl;
/***************************END INITIALIZATION***********************************/

   /*
   The msgTX format listed is specific to the SFP200 protocol,
   the following is the format that is expected.
   Need method to change the requested data on the fly?
   Method to change query would need to alter the redister requested.
   */
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x20; // register on register map Table 3

   ///* this is just to satisfy the initialization condition - error C4700 */
   msgRX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgRX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgRX.LEN      = 1;
   msgRX.DATA[0]   = 0x20; // register on register map Table 3

   /**/
   
   CAN_Reset(PCAN_USBBUS1);
   messageConfig(msgTX,msgRX);
   

/**************************************Start Write*******************************/
   sendMessage(msgTX);
/*
result = CAN_Write(PCAN_USBBUS1, &msgTX);
if(result != PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    CAN_GetErrorText(result, 0, strMsg);
    wcout<<(strMsg)<<endl;
}
else
    wcout<<("Message sent successfully")<<endl;
*/
/***************************************END WRITE********************************/
   
/*******************************Start Read***************************************/
   readMessage(msgRX,msgTX);

   /*
      If we are reading coulombs we need to read twice.
      Need some control to enter read twice.
   */

   //do
   //{
//     // Check the receive queue for new messages   //
   //  // A CAN message is configured            //
   //  // Will loop until receive queue is empty      //
//     result = CAN_Read(PCAN_USBBUS1, &msgRX, &timestamp);
//     if (result != PCAN_ERROR_QRCVEMPTY) //0x00020U -- #32 // if result not equal 32 // Receive queue is empty
//     {
//        // Process the received message
   //     if(CAN_Initialize(PCAN_USBBUS1,PCAN_BAUD_500K) == PCAN_ERROR_OK)
   //      {
   //         for(int i=0; i<50;i++)
   //         {
   //            msgRX = msgList[i];
   //            CAN_Read(PCAN_USBBUS1,&msgRX,&timestamp);
   //         }
   //         Sleep(100);// Sleep to be sure all messages have been sent. - Stellan
   //         CAN_Uninitialize(PCAN_USBBUS1);
   //      }
//        ProcessMessage(msgRX);
   //    wcout<< count <<endl<< ("A message was received\n") <<msgRX.DATA[1]<<" "<<msgRX.DATA[2]<<" "<<msgRX.DATA[3]<<" "<<msgRX.DATA[4]<<" "<< endl <<msgRX.ID<< endl;
//       
   //    sendyneCurrentDataFormat( msgTX, msgRX);
   //    //sendyneCurrentDataFormat(fp,msgTX.DATA[0],msgRX.DATA[1],msgRX.DATA[2],msgRX.DATA[3],msgRX.DATA[4]); // pass the data read to be translated and read to "outfile.txt"
   //    count++;
//     }// end if
//     else
//     {
//        /* An error occurred, get a text describing the error and show it
//           and handle the error
//        */
//        CAN_GetErrorText(result, 0, strMsg);
//        wcout << (strMsg) << endl;
//        /* Here can be decided if the loop has to be  terminated (eg. the bus
//        // status is  bus-off)
//        */
//        HandleReadError(result);
//     }// end else
//     // Try to read a message from the receive queue of the PCAN-USB, Channel 1,
//     // until the queue is empty
   //  //CAN_Uninitialize(PCAN_USBBUS1); // uncomment to enter inner if statement and for loop
   //} while ((result & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY); //end do while

/*******************************END Read*****************************************/ 
}// end while
    return 0;
}


Code: Select all
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
#include <stdio.h>

#include "windows.h"
#include "PCANBasic.h"
#include "main.h"
//#define _WIN32_WINNT 0x0500
using namespace std;
/************Funtions For File Handling************/
/**/FILE * open_input_file (void)// — Opens "infile.dat" for reading.
{
FILE *handle = NULL;
   handle = fopen ("sequences.txt", "r");
   if (handle == NULL) // if NULL then file was not opened
   {
      printf ("ERROR 123: Could not open file successfully!\n");
   }
   if (handle != NULL)
   {
      printf ("Could open SEQUENCES file successfully!\n");
   }
   return handle;
}
/**/FILE * open_outfile (void) // - Opens "mutations.txt" for reading.
{
   FILE *handle = NULL;
   handle = fopen ("outfile.txt", "a");
   if (handle == NULL) // if NULL then file was not opened
   {
      printf ("ERROR 123: Could not open file successfully!\n");
   }
   if (handle != NULL)
   {
      printf ("Could open OUTFILE file successfully!\n");
   }
   return handle;
}
/**/FILE * open_random (void) // - Opens "mutations.txt" for reading.
{
   FILE *handle = NULL;
   handle = fopen ("random.txt", "a");
   if (handle == NULL) // if NULL then file was not opened
   {
      printf ("ERROR 123: Could not open file successfully!\n");
   }
   if (handle != NULL)
   {
      printf ("Could open RANDOM file successfully!\n");
   }
   return handle;
}
/**/void close_outfile (FILE * outfile)
{
   fclose(outfile);
      printf("\nMUTATIONS Closed!\n");
}
/**/void close_infile (FILE * infile)
{
   fclose(infile);
      printf("\nSEQUENCES Closed!\n");
}
/**/void close_random (FILE * random)
{
   fclose(random);
      printf("\nRANDOM Closed!\n");
}
/***************************************************/

int ProcessMessage(TPCANMsg msg) {
   return 0;
}

int HandleReadError(TPCANStatus result) {
   return 0;

}
void initialize()
{
       // The Plug & Play Channel (PCAN-USB) is initialized   //
   TPCANStatus result = 0;
   char strMsg[256];

   result = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K);
   if (result != PCAN_ERROR_OK)
   {
      // An error occurred, get a text describing the error and show it
      //
      CAN_GetErrorText(result, 0, strMsg);
      wcout<<(strMsg)<<endl;
   }
   else
      wcout<<("PCAN-USB (Ch-1) was initialized")<<endl;

}
void initialize(TPCANStatus result,char strMsg)
{
    // The Plug & Play Channel (PCAN-USB) is initialized
   //
   result = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K);
   if (result != PCAN_ERROR_OK)
   {
      // An error occurred, get a text describing the error and show it
      //
      CAN_GetErrorText(result, 0, &strMsg);
      wcout<<(strMsg)<<endl;
   }
   else
      wcout<<("PCAN-USB (Ch-1) was initialized")<<endl;
}

void sendMessage(TPCANMsg msgTX)
{
char strMsg[256];                     /* Only used to display predefined error messages      */
TPCANStatus result = 0;                  /* Also used to hold code for message to be displayed   */
result = CAN_Write(PCAN_USBBUS1, &msgTX);
if(result != PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    CAN_GetErrorText(result, 0, strMsg);
    std::wcout<<(strMsg)<<endl;
}
else
    std::wcout<<("Message sent successfully")<<endl;

}

void readMessage(TPCANMsg msgRX, TPCANMsg msgTX)
{

TPCANStatus result = 0;
char strMsg[256];
char line[256];
TPCANTimestamp timestamp;
FILE *fp = NULL;
unsigned char messageType = NULL;
bool xmtFileFormat = false;
int count = 0;
do
   {
      // Check the receive queue for new messages
      //
         // A CAN message is configured //
                                    //msgRX.ID = 0x0A100200;
                                    //msgRX.MSGTYPE = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
                                    //msgRX.LEN = 5;
                                    //msgRX.DATA[0] = 0x60;
                                 //sendDataOverCAN();

      result = CAN_Read(PCAN_USBBUS1, &msgRX, &timestamp);
      if (result != PCAN_ERROR_QRCVEMPTY) //0x00020U -- #32 // if result not equal 32 // Receive queue is empty
      {
         // Process the received message
         //
         
         ProcessMessage(msgRX);
       std::wcout<< count <<endl<< ("A message was received\n") <<msgRX.DATA<< endl <<msgRX.ID<< endl <<msgRX.LEN<< endl <<msgRX.MSGTYPE<< endl;
         sendyneCurrentDataFormat(msgTX,msgRX);
       count++;
      }
      else
      {
         // An error occurred, get a text describing the error and show it
         // and handle the error
         //
         CAN_GetErrorText(result, 0, strMsg);
         std::wcout << (strMsg) << endl;
         // Here can be decided if the loop has to be  terminated (eg. the bus
         // status is  bus-off)
         //
         HandleReadError(result);
      }
      // Try to read a message from the receive queue of the PCAN-USB, Channel 1,
      // until the queue is empty
   } while ((result & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY);

}
/*
void status()
{
   TPCANStatus result;
   StringBuilder^ strMsg;

   strMsg = gcnew StringBuilder(256);

   // Check the status of the PCI Channel
   //
   result = PCANBasic::GetStatus(PCANBasic::PCAN_PCIBUS1);
   switch (result)
   {
      case TPCANStatus::PCAN_ERROR_BUSLIGHT:
         MessageBox::Show("PCAN-PCI (Ch-1): Handling a BUS-LIGHT status...");
         break;
      case TPCANStatus::PCAN_ERROR_BUSHEAVY:
         MessageBox::Show("PCAN-PCI (Ch-1): Handling a BUS-HEAVY status...");
         break;
      case TPCANStatus::PCAN_ERROR_BUSOFF:
         MessageBox::Show("PCAN-PCI (Ch-1): Handling a BUS-OFF status...");
         break;
      case TPCANStatus::PCAN_ERROR_OK:
         MessageBox::Show("PCAN-PCI (Ch-1): Status is OK");
         break;
      default:
         // An error occurred, get a text describing the error and show it
         //
         PCANBasic::GetErrorText(result, 0, strMsg);
         MessageBox::Show(strMsg->ToString());
         break;
   }
}
*/
void messageConfig( TPCANMsg &msgTX, TPCANMsg &msgRX)
{
      // A CAN message is configured //
   int coulombStageCount = 0;
   int choice = 0;
   int query = 0;

   printf(   "Choice\n"
         "1 for coulomb counting      \n"
         "2 for current sensing      \n"
         "3 for voltage0 sensing      \n"
         "4 for voltage1 sensing      \n"
         "5 for voltage2 sensing      \n"
         "6 for temperature sensing   \n");
   scanf("%d",&choice);

   /*
      When querying for coulomb count this is control structure
      to ensure the latch that is stated in the SFP200 Data sheet
      for the coulomb registers is maintained.
      When querying for coulombs this will be the only method that
      can be ran with this given implementation.
   */
   COULOMB:
   switch (choice)
   {
   case 1:
   query = 0x40;
   choice --;
   break;
   
   case 0:
   query = 0x41;
   choice ++;   
   break;

   case 2:
   query = 0x20;
   break;

   case 3:
   query = 0x60;
   break;

   case 4:
   query = 0x61;
   break;

   case 5:
   query = 0x62;
   break;

   case 6:
   query = 0x80;
   break;

   default:
      break;
   }

   switch(query)//
   {
   case 0x20: // Current
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x20;

   /* Possibly put the write and read portions of code in main here?   */
   /* Do the same for all of the different cases?                  */
   
   
   CAN_Reset(PCAN_USBBUS1);
   initialize();
   sendMessage(msgTX);
   readMessage(msgRX,msgTX);
   goto COULOMB;
   break;
   
   case 0x40: // Coulomb Count Low
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x40;

   initialize();
   sendMessage(msgTX);
   readMessage(msgRX,msgTX);

   goto COULOMB;

   break;

   case 0x41: // Coulomb Count High
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x41;

   initialize();
   sendMessage(msgTX);
   readMessage(msgRX,msgTX);

   goto COULOMB;

   break;

   case 0x60: // Voltage 0
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x60;

   //CAN_Reset(PCAN_USBBUS1);
   initialize();
   sendMessage(msgTX);
   readMessage(msgRX,msgTX);

   goto COULOMB;
   break;
   
   case 0x61: // Voltage 1
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x61;

   initialize();
   sendMessage(msgTX);
   readMessage(msgRX,msgTX);

   goto COULOMB;
   break;

   case 0x62: // Voltage 2
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x62;

   initialize();
   sendMessage(msgTX);
   readMessage(msgRX,msgTX);

   goto COULOMB;
   break;
   
   case 0x80: // Temperature (C)
   msgTX.ID      = 0x0A100201; //SFP200 listens for this ID
   msgTX.MSGTYPE   = PCAN_MESSAGE_EXTENDED; // changed from PCAN_MESSAGE_STANDARD
   msgTX.LEN      = 1;
   msgTX.DATA[0]   = 0x80;   

   initialize();
   sendMessage(msgTX);
   readMessage(msgRX,msgTX);

   goto COULOMB;
   break;

   default:
      break;
   }
}

/*
Debugging text that is in the PEAK help file.
So far the filter status has come back closed for all test cases that
I've ran, Speaking to Michael the filter should be open upon initialization.
This is something that may need to be looked into further.

On a similar note, a method to retrieve the active USBBUS should be looked at.
*/
void checkFilter(TPCANStatus result,char strMsg)
{
int iBuffer;
// The status of the message filter of the PCAN-USB Channel 1 is asked//
result = CAN_GetValue(PCAN_USBBUS1, PCAN_MESSAGE_FILTER, &iBuffer, sizeof(iBuffer));
if(result != PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    CAN_GetErrorText(result, 0, &strMsg);
    cout<<(strMsg);
}
else
{
    // A text is shown giving information about the current status of the filter
    //
    switch(result != PCAN_ERROR_OK)
    {
        case PCAN_FILTER_OPEN:
            cout<<("The message filter for the PCAN-USB, channel 1, is completely opened.")<<endl;
            break;
        case PCAN_FILTER_CLOSE:
            cout<<("The message filter for the PCAN-USB, channel 1, is closed.")<<endl;
            break;
        case PCAN_FILTER_CUSTOM:
            cout<<("The message filter for the PCAN-USB, channel 1, is custom configured.")<<endl;
            break;
    }
}
};

/*
Possibly not passing the values correctly i.e. by value/reference
Seems that when the helper functions are used bugs are introduced
although not consistently.
*/
void sendDataOverCAN(TPCANMsg msg,TPCANMsg msgList[50])
{


//
// Fill the list "msgList" with messages as you need
//

//
// Connect the channel to use and iterate to send the messages
//
if(CAN_Initialize(PCAN_USBBUS1,PCAN_BAUD_500K) == PCAN_ERROR_OK)
{
    for(int i=0; i<50;i++)
    {
        msg = msgList[i];
        CAN_Write(PCAN_USBBUS1,&msg);
    }
    CAN_Uninitialize(PCAN_USBBUS1);
}

}

/*Test Code From Sendyne*/
//void sendyneCurrentDataFormat(FILE *fp,TPCANMsg msgTX,TPCANMsg msgRX)
void sendyneCurrentDataFormat(TPCANMsg msgTX,TPCANMsg msgRX)
{
   int querry = msgTX.DATA[0];
   switch (querry)
   {
   case 0x20: // Current
      {   
         ofstream outfile ("current.txt",std::ofstream::app);
         unsigned int reassembled_data = 0;
         reassembled_data |= msgRX.DATA[1] << 24;
         reassembled_data |= msgRX.DATA[2] << 16;
         reassembled_data |= msgRX.DATA[3] << 8;
         reassembled_data |= msgRX.DATA[4] << 0;
         // Converting to volts
         float voltage = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts         
         //output to file
         outfile << "Current μA: "<<voltage<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
      break;
   
   case 0x40: // Coulomb Count Low
      {   ofstream outfile ("CCL.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= msgRX.DATA[1] << 24;
      reassembled_data |= msgRX.DATA[2] << 16;
      reassembled_data |= msgRX.DATA[3] << 8;
      reassembled_data |= msgRX.DATA[4] << 0;
      // Converting to volts
      float CCL = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      //output to file
      outfile << "CCL μC: "<<CCL<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
      break;

   case 0x41: // Coulomb Count High
      {   ofstream outfile ("CCH.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= msgRX.DATA[1] << 24;
      reassembled_data |= msgRX.DATA[2] << 16;
      reassembled_data |= msgRX.DATA[3] << 8;
      reassembled_data |= msgRX.DATA[4] << 0;
      // Converting to volts
      float CCH = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      //output to file
      outfile << "CCH μC*2^32: "<<CCH<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
   
   case 0x60: // Voltage 0
      {   
         ofstream outfile ("voltage0.txt",std::ofstream::app);
         unsigned int reassembled_data = 0;
         reassembled_data |= msgRX.DATA[1] << 24;
         reassembled_data |= msgRX.DATA[2] << 16;
         reassembled_data |= msgRX.DATA[3] << 8;
         reassembled_data |= msgRX.DATA[4] << 0;
         // Converting to volts
         float voltage0 = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
         //output to file
         outfile << "Voltage0 μV: "<<voltage0<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
      break;
   /*
   case 0x61: // Voltage 1
      {   ofstream outfile ("voltage1.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= msgRX.DATA[1] << 24;
      reassembled_data |= msgRX.DATA[2] << 16;
      reassembled_data |= msgRX.DATA[3] << 8;
      reassembled_data |= msgRX.DATA[4] << 0;
      // Converting to volts
      float voltage1 = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      //output to file
      outfile << "Voltage1 μV: "<<voltage1<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
      break;
   case 0x62: // Voltage 2
      {   ofstream outfile ("voltage2.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= msgRX.DATA[1] << 24;
      reassembled_data |= msgRX.DATA[2] << 16;
      reassembled_data |= msgRX.DATA[3] << 8;
      reassembled_data |= msgRX.DATA[4] << 0;
      // Converting to volts
      float voltage2 = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      //output to file
      outfile << "Voltage2 μV: "<<voltage2<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
      break;
   */
   case 0x80: // Temperature (C)
      {   
         ofstream outfile ("temperature.txt",std::ofstream::app);
         unsigned int reassembled_data = 0;
         reassembled_data |= msgRX.DATA[1] << 24;
         reassembled_data |= msgRX.DATA[2] << 16;
         reassembled_data |= msgRX.DATA[3] << 8;
         reassembled_data |= msgRX.DATA[4] << 0;
         // Converting to volts
         float tempInC = (int)(reassembled_data) / 1000;   //
         //output to file
         outfile << "Temperature in m°C: "<<tempInC<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
      break;

   default:
      break;
   }
}

void sendyneCurrentDataFormat(FILE *fp,unsigned char queryType,unsigned char byte1,unsigned char byte2,unsigned char byte3,unsigned char byte4)
{

   switch (queryType)
   {
   case 0x20: // Current
      {   ofstream outfile ("current.txt",std::ofstream::app);
         unsigned int reassembled_data = 0;
         reassembled_data |= byte1 << 24;
         reassembled_data |= byte2 << 16;
         reassembled_data |= byte3 << 8;
         reassembled_data |= byte4 << 0;
         // Converting to volts
         float voltage = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
         
         //output to file
         outfile << "Current: "<<voltage<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
   /*
   case 0x40: // Coulomb Count Low
      {   ofstream outfile ("CCL.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= byte1 << 24;
      reassembled_data |= byte2 << 16;
      reassembled_data |= byte3 << 8;
      reassembled_data |= byte4 << 0;
      // Converting to volts
      float voltage = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      
      //output to file
      outfile << "CCL: "<<voltage<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
   case 0x41: // Coulomb Count High
      {   ofstream outfile ("current.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= byte1 << 24;
      reassembled_data |= byte2 << 16;
      reassembled_data |= byte3 << 8;
      reassembled_data |= byte4 << 0;
      // Converting to volts
      float voltage = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      
      //output to file
      outfile << "Current: "<<voltage<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
   */
   case 0x60: // Voltage 0
      {   ofstream outfile ("voltage0.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= byte1 << 24;
      reassembled_data |= byte2 << 16;
      reassembled_data |= byte3 << 8;
      reassembled_data |= byte4 << 0;
      // Converting to volts
      float voltage = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts

      //output to file
      outfile << "Voltage0: "<<voltage<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
   /*
   case 0x61: // Voltage 1
      {   ofstream outfile ("voltage1.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= byte1 << 24;
      reassembled_data |= byte2 << 16;
      reassembled_data |= byte3 << 8;
      reassembled_data |= byte4 << 0;
      // Converting to volts
      float voltage = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      float tempInC = (int)(reassembled_data) / 1000;   //

      //output to file
      outfile << "Voltage1: "<<voltage<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
   case 0x62: // Voltage 2
      {   ofstream outfile ("voltage2.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= byte1 << 24;
      reassembled_data |= byte2 << 16;
      reassembled_data |= byte3 << 8;
      reassembled_data |= byte4 << 0;
      // Converting to volts
      float voltage = (int)(reassembled_data) / 1000000.0; // Calculated value is - 12.213964 Volts
      float tempInC = (int)(reassembled_data) / 1000;   //

      //output to file
      outfile << "Voltage2: "<<voltage<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"
   */
   case 0x80: // Temperature (C)
      {   ofstream outfile ("temperature.txt",std::ofstream::app);
      unsigned int reassembled_data = 0;
      reassembled_data |= byte1 << 24;
      reassembled_data |= byte2 << 16;
      reassembled_data |= byte3 << 8;
      reassembled_data |= byte4 << 0;
      float tempInC = (int)(reassembled_data) / 1000;   //
      //output to file
      outfile << "Temperature in C: "<<tempInC<<endl;
      }// adding {} "fixes transfer of control bypasses initialization of:"

   default:
      break;
   }
}

Elthes
 
Posts: 3
Joined: Fri 19. May 2017, 20:23

Re: USB/CAN FD initialization

Postby U.Wilhelm » Fri 26. May 2017, 07:58

Find available USB CAN Interfaces could be very easy done by try and see result of the CAN_GetValue(..) with the Parameter PCAN_CHANNEL_CONDITION

PCAN_CHANNEL_CONDITION

Access:

Description: This parameter is used to check and detect available PCAN hardware on a computer, even before trying to connect any of them. This is useful when an application wants the user to select which hardware should be using in a communication session.

Possible values: This parameter can have one of these values: PCAN_CHANNEL_UNAVAILABLE, PCAN_CHANNEL_AVAILABLE, PCAN_CHANNEL_OCCUPIED, PCAN_CHANNEL_PCANVIEW.

Default value: N/A.

PCAN-Device: All PCAN devices (excluding PCAN_NONEBUS channel).



See online Help File - there you find all info about it.

We do not understand you other infore - what exact is your question - we do not understand your request?
--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------
User avatar
U.Wilhelm
Sales & Support
Sales & Support
 
Posts: 1039
Joined: Fri 10. Sep 2010, 18:34
Location: Darmstadt

Re: USB/CAN FD initialization

Postby Elthes » Fri 26. May 2017, 10:26

For example if I put this code into a function " void initialize()" there seems to be some issues with actually initializing the channel. When I take the encapsulated code out of the function and put it back into main then the channel is initialized.

Find available USB CAN Interfaces could be very easy done by try and see result of the CAN_GetValue(..) with the Parameter PCAN_CHANNEL_CONDITION


By doing so I'll be able to see what the USB/CAN FD device is assigned to i.e. USBBUS(some number)?
Essentially it seems to me that I'll need some method of dynamically initializing the correct USBBUS.


Code: Select all
result = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K);                     
if (result != PCAN_ERROR_OK)
{
   // An error occurred, get a text describing the error and show it
   CAN_GetErrorText(result, 0, strMsg);
   std::wcout<<(strMsg)<<endl;
}
else
   std::wcout<<("PCAN-USB (Ch-1) was initialized")<<endl;
Elthes
 
Posts: 3
Joined: Fri 19. May 2017, 20:23

Re: USB/CAN FD initialization

Postby U.Wilhelm » Fri 26. May 2017, 14:46

For example if I put this code into a function " void initialize()" there seems to be some issues with actually initializing the channel. When I take the encapsulated code out of the function and put it back into main then the channel is initialized.

This seems a application / environment related problem and no PEAK-System API related problem. We do not know your environment.

By doing so I'll be able to see what the USB/CAN FD device is assigned to i.e. USBBUS(some number)?
Essentially it seems to me that I'll need some method of dynamically initializing the correct USBBUS.

The Device Numbers are also well explained in the Online Help - please read the Help (PCAN Handle Definitions) and study the sample code.

To FIX a USB-CAN Interface to a defined connector , please see here (link)
--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------
User avatar
U.Wilhelm
Sales & Support
Sales & Support
 
Posts: 1039
Joined: Fri 10. Sep 2010, 18:34
Location: Darmstadt

Re: USB/CAN FD initialization

Postby Elthes » Fri 26. May 2017, 18:55

The Device Numbers are also well explained in the Online Help - please read the Help (PCAN Handle Definitions) and study the sample code.

To FIX a USB-CAN Interface to a defined connector , please see here (link)


Thank you this should get me going in the right direction again.
You're help is very much appreciated!
Elthes
 
Posts: 3
Joined: Fri 19. May 2017, 20:23


Return to Software



This website uses cookies for analytics, for logins to the online shop and the forum, and for the handling of an order. By browsing this website you agree to the use of cookies. Detailed information on their use can be found in our privacy policy.

OKPrivacy Policy