ameyer75
Posts: 2
Joined: Thu Mar 28, 2019 8:54 pm

SPI CAN frames

Thu Mar 28, 2019 9:59 pm

Hello,
I have been working to create a GUI for a custom made formula car for a club I am in. I have the GUI built and compiled in a GTK program that is written in C along with functions that are intended to get the sensor data. We are using a CAN network and we have purchased and implemented the PI CAN 2 shield and are successfully capturing CAN frames with all needed data. The part that I am unsure about is how to get that data from the SPI connection that the shield uses to communicate with the pi and into my C program to be used. Below is my code written in C and an image of the CAN frames the PI is receiving. I think my problem is trying to use the scanf function to parse through the frames but I just don't really know.

Thanks in advance

Code: Select all

#include <glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "CAN.h"
#define FREQUENCY 500

static void destroy (GtkWidget *widget, gpointer data);
static gboolean update_text (GtkWidget *label);

long findRPM() {

    char device[5];
    char filter[10];
    char zeros[4];
    char random[3];
    char rpm[3];
    char TPS[3];
    char fuelOpen[3];
    char igAngle[3];
    short done = 0;
    
    while(done == 0){

    scanf("%s, %s, %s, %s, %s, %s, %s, %s", device, filter, zeros, random, rpm, TPS, fuelOpen, igAngle);

    if(strcmp(filter, "0CFFF048")){
      long tpsNumber = strtol(TPS, NULL, 16);
      return tpsNumber;
      printf("%s",TPS);
      done = 1;
  }
}
}

long findCoolantTemp() {
    char device[5];
    char filter[10];
    char zeros[4];
    char random[3];
    char batt[3];
    char air[3];
    char coolant[3];
    char type[2];
    short done = 0;
    
    while(done == 0){

    scanf("%s, %s, %s, %s, %s, %s, %s, %s", device, filter, zeros, random, batt, air, coolant, type);

    if(strcmp(filter, "0CFFF548")){
      long cool = strtol(coolant, NULL, 16);
      return cool;
      printf("%s",cool);
      done = 1;
  }
}
}
long findGear() {
    char device[5];
    char filter[10];
    char zeros[4];
    char random[3];
    char batt[3];
    char air[3];
    char coolant[3];
    char type[2];
    short done = 0;
    
    while(done == 0){

    scanf("%s, %s, %s, %s, %s, %s, %s, %s", device, filter, zeros, random, batt, air, coolant, type);

    if(strcmp(filter, "0CFFF548")){
      long cool = strtol(coolant, NULL, 16);
      return cool;
      printf("%s",cool);
      done = 1;
  }
}
}

int main (int argc, char **argv) 
{
GtkWidget *window;
GtkWidget *label;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(window), "Cyclone Racing");
gtk_window_set_default_size (GTK_WINDOW (window), 800, 480);
g_signal_connect (G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);

label = gtk_label_new ("Lets Go Racing!");
gtk_container_add (GTK_CONTAINER (window), label);

g_timeout_add (1000/FREQUENCY, (GSourceFunc)update_text, label);	
gtk_widget_show_all (window);

gtk_main ();

return 0;
}

static gboolean
update_text (GtkWidget *label)
{
gchar *text;
static int i = 0;

/* A prettier solution would proably be to remove the timeout when
* the window is closed */
if (GTK_IS_LABEL(label))
{
text = g_strdup_printf ("%d %s\n%d %s\n%s", findRPM(),"RPM",findCoolantTemp(),"F","Gear");
gtk_label_set_text (GTK_LABEL(label), text);
g_free (text);
return TRUE;
}
/* Returning FALSE removes the timeout from the glib main loop */
else
return FALSE;

}

static void
destroy (GtkWidget *widget, gpointer data)
{
gtk_main_quit [img][/img]();
}
Image

bertr2d2
Posts: 98
Joined: Wed Aug 08, 2012 10:12 pm

Re: SPI CAN frames

Fri Mar 29, 2019 2:02 pm

Please have a look at:
https://en.wikipedia.org/wiki/SocketCAN

It's simply network programming like UDP.

Regards

Gerd
Easy to build CAN-Bus interface:
http://lnxpps.de/rpie

ameyer75
Posts: 2
Joined: Thu Mar 28, 2019 8:54 pm

Re: SPI CAN frames

Sat Mar 30, 2019 4:42 pm

Thank you Gerd for your response.

I have read through the page and have zeroed in on a function called log2long which initially seems like it will translate the data from hex to a meaningful format for the user. While this is great I am trying to find an example of code or documentation of the function that will allow me to use that data in a C function that I have written in order to display that data in my GUI rather than make the driver of my car have to read raw can frames or even translated ones. I will keep looking and struggling through but any advice or code examples in C would be appreciated.

Thanks Again

Return to “Interfacing (DSI, CSI, I2C, etc.)”