Error: invalid conversion from 'void*' to 'const char*' [-fpermissive]

I am trying to use an example and trying to add to it for a project I am working on and It involves the ATmega8 and OBD2 library with a MCP2515 module. I am getting an error that says

Arduino\libraries\OBD2\src\OBD2.cpp: In member function 'String OBD2Class::pidUnits(uint8_t)':
Arduino\libraries\OBD2\src\OBD2.cpp:432:26: error: invalid conversion from 'void*' to 'const char*' [-fpermissive]
const char* pgmUnits = pgm_read_ptr(&PID_UNIT_MAPPER[pid]);
                          ^
exit status 1

Compilation error: exit status 1

I was wondering if someone can help me figure out what the error means and what the solution.

I attached link of the code which i use in this project.

Example code

I attached link of the library file which i use in this project.

library file

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

const char* pgmUnits = (const char *) pgm_read_ptr(&PID_UNIT_MAPPER[pid]);

@Whandall I didn't get it what you are trying to say.

The pgm_read_ptr() function apparently returns a void* pointer. You need to cast

(const char *)

The void* to the type of result expected by const char* pgmUnits

I think my line would make your sketch compile, did you try it?

@Whandall Now It shows this error

c:/users/dell/appdata/local/arduino15/packages/dxcore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino4b/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld.exe: C:\Users\DELL\AppData\Local\Temp\arduino-sketch-BC6274E536839CA9049DFA7EA277D68E/OBD2_02_KeyStats.ino.elf section `.text' will not fit in region `text'
c:/users/dell/appdata/local/arduino15/packages/dxcore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino4b/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld.exe: region `text' overflowed by 8342 bytes
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

Too much code/data in the code you do not show.
Snippets are crap.

@Whandall I didn't get it what you are trying to say.

is this library is too large for my microcontroller?

So you don't understand a single line of code,
after that you don't understand a simple sentence, what is the problem?

The MCU is too small, or all the code and data is too big, pick your culprit.

i understand the code but not understand
this

You do not show the full code in code tags.

Some things, like the first error, don't need more than the error message.

It is not possible to guess what makes you exceed the 8k you have,
without seeing the full picture.

i already provide a link of code

but for your reference i again provide full code using code tags

// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <CAN.h> // the OBD2 library depends on the CAN library
#include <OBD2.h>

// array of PID's to print values of
const int PIDS[] = {
  CALCULATED_ENGINE_LOAD,
  ENGINE_COOLANT_TEMPERATURE,
  ENGINE_RPM,
  VEHICLE_SPEED,
  AIR_INTAKE_TEMPERATURE,
  MAF_AIR_FLOW_RATE,
  THROTTLE_POSITION,
  RUN_TIME_SINCE_ENGINE_START,
  FUEL_TANK_LEVEL_INPUT,
  ABSOLULTE_BAROMETRIC_PRESSURE,
  ABSOLUTE_LOAD_VALUE,
  RELATIVE_THROTTLE_POSITION
};

const int NUM_PIDS = sizeof(PIDS) / sizeof(PIDS[0]);

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println(F("OBD2 Key Stats"));

  while (true) {
    Serial.print(F("Attempting to connect to OBD2 CAN bus ... "));

    if (!OBD2.begin()) {
      Serial.println(F("failed!"));

      delay(1000);
    } else {
      Serial.println(F("success"));
      break;
    }
  }

  Serial.println();
}

void loop() {
  // loop through all the PID's in the array
  // 
  for (int i = 0; i < NUM_PIDS; i++) {
    int pid = PIDS[i];

    printPID(pid);
  }
  Serial.println();

  delay(1000);
}

void printPID(int pid) {
  // print PID name
  Serial.print(OBD2.pidName(pid));
  Serial.print(F(" = "));

  // read the PID value
  float pidValue = OBD2.pidRead(pid);

  if (isnan(pidValue)) {
    Serial.print("error");
  } else {
    // print value with units
    Serial.print(pidValue);
    Serial.print(F(" "));
    Serial.print(OBD2.pidUnits(pid));
  }

  Serial.println();
}

Posting a link is different to posting it in code tags here.

There is nothing that could be removed from your sketch, or shrunk
(to make a real difference, some bytes could be spared),
it uses more than double the available space, so you will need a bigger MCU.

@Whandall i change the code and it does not show any error. but it does not work. It work fine in ATmega328.

// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//
// This examples queries the engine RPM (OBD-II PID 0x0c) once a seconds and
// prints the value to the Serial monitor
//
#include <CAN.h>

// Most cars support 11-bit adddress, others (like Honda),
// require 29-bit (extended) addressing, set the next line
// to true to use extended addressing
const bool useStandardAddressing = false;

void setup() {
  Serial.begin(9600);

  //Serial.println("CAN OBD-II engine RPM");
  //CAN.setClockFrequency(8e6);
   
  // start the CAN bus at 500 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }

  // add filter to only receive the CAN bus ID's we care about
  if (useStandardAddressing) {
    CAN.filter(0x7e8);
  } else {
    CAN.filterExtended(0x18daf110);
  }
 }

void loop() {
  if (useStandardAddressing) {
    CAN.beginPacket(0x7df, 8);
  } else {
    CAN.beginExtendedPacket(0x98DB33F1, 8);
  }
  CAN.write(0x02); // number of additional bytes
  CAN.write(0x01); // show current data
  CAN.write(0x0d); // Vehicle Speed
  CAN.endPacket();

  // wait for response
  while (CAN.parsePacket() == 0 ||
         CAN.read() < 3 ||          // correct length
         CAN.read() != 0x41 ||      // correct mode
         CAN.read() != 0x0d);       // correct PID

  int Vss = (CAN.read() /* * 256.0)*/ + CAN.read())/* / 4.0*/;
  
  //Serial.print("Vehicle Speed = ");
  Serial.println(Vss);
  //tone(3, Vss);
  //delay(1000);
}

Then I would use one.

Good luck with your project.

@Whandall But it does not work with my microcontroller

The code you posted initially can not work on your microcontroller.

I'm not interested in squeezing code in too small microcontrollers, so good luck with your try.

it compile and upload very well but it does not work.

Sketch uses 7236 bytes (94%) of program storage space. Maximum is 7680 bytes.
Global variables use 649 bytes (63%) of dynamic memory, leaving 375 bytes for local variables. Maximum is 1024 bytes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.