Linux Program Examples

This forum covers PCAN-Linux and Linux development issues concerning our products

Linux Program Examples

Postby peterm » Thu 31. Mar 2011, 22:48

I have a PCI Express CAN card and need to write some software to interface with some older code in C.

I wanted to know if anyone has any example programs for transmitting to the Bus in the C language. I have tried the transmit program located in the test directory and it worked, but would like to see how to write to the CANusing data in a program and not from an outside file without the use of the STD::list function as I cannot use this in my legacy software.

Any help would be greatly appreciated.
peterm
 
Posts: 1
Joined: Thu 31. Mar 2011, 22:42

Re: Linux Program Examples

Postby U.Wilhelm » Mon 4. Apr 2011, 08:52

Here the sample code that i send you also by E-Mail:
Code: Select all
/* pcantest.c Testapplication for PCAN and thread programming under Linux
(c) 2010_06_14 MM   */

// INCLUDES
#include <stdio.h>
#include <libpcan.h>
#include <fcntl.h>
#include <termios.h> // for getch()
#include <pthread.h> // Threads

// GLOBALS
HANDLE h;
int ret;
TPCANMsg msg1;
char txt[VERSIONSTRING_LEN];

// Thread for reading messages
void read_t1(HANDLE h1)
{
int i;
printf("Thread is activ!\n");
while(1)
  {
   ret=CAN_Read(h1, &msg1);
   if (ret)
    printf("Error: Read %x\n",ret);
   else
    {
     printf("msg-ID=%X, DLC=%d, MSGType=%X Data= ",msg1.ID,msg1.MSGTYPE,msg1.LEN);
     for (i=0;i<msg1.LEN;i++)
     printf(" %02X",msg1.DATA[i]);
     printf("\n");
    }
  } 
}

// get keyboard character without the need to hit return...
int getch()
{
static int ch = -1, fd = 0;
struct termios neu, alt;
fd = fileno(stdin);
tcgetattr(fd, &alt);
neu = alt;
neu.c_lflag &= ~(ICANON|ECHO);
tcsetattr(fd, TCSANOW, &neu);
ch = getchar();
tcsetattr(fd, TCSANOW, &alt);
return ch;
}

int main(int argc, char *argv[])
{
char c;
pthread_t r1;

printf("pcantest - a small test program which receives and prints CAN messages.\n");
h = LINUX_CAN_Open("/dev/pcan32", O_RDWR);
  if (!h)
  {
   printf("receivetest: Error: can't open device \n");
   return 0;
  }

  ret = CAN_VersionInfo(h, txt);
  if (!ret)
    printf("pcantest: driver version = %s\n", txt);
  else {
    printf("pcantest: Error: CAN_VersionInfo()");
    return 0;
    }

  // init to an user defined bit rate
  ret = CAN_Init(h, 0x001C, CAN_INIT_TYPE_EX);
   if (ret)
     {
      printf("pcantest: Error: CAN_Init()");
      return 0;
    }

  if(pthread_create(&r1, NULL, (void *)&read_t1, (HANDLE)h) != 0)
    {
      fprintf(stderr, "Fehler bei Thread 1......\n");
      return 0;
    }


while (1) //main loop
{
 
  switch(c=getch(stdin)) 
  {
   case 't':
     msg1.ID  = 0x100;
     msg1.MSGTYPE  = 0;
     msg1.LEN  = 1;
     msg1.DATA[0]  = 0xAE;
     ret = CAN_Write(h, &msg1);
    if (ret)
      {
       printf("CAN_Write() Error: %x \n",ret);
       return 0;
      }
     else     
      {
       printf("pcantest: CAN_Write() ok\n");
      }
     break;
   case 's':
     msg1.ID  = 0x111;
     msg1.MSGTYPE  = 0;
     msg1.LEN  = 1;
     msg1.DATA[0]  = 0xFF;
     ret = CAN_Write(h, &msg1);
     if (ret)
      {
       printf("CAN_Write() Error: %x \n",ret);
       return 0;
      }
     else     
      {
       printf("pcantest: CAN_Write() ok\n");
      }

     break;

   default:
     break;
  }
} //end main loop
}

--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------
U.Wilhelm
Sales & Support
Sales & Support
 
Posts: 377
Joined: Fri 10. Sep 2010, 19:34
Location: Darmstadt


Return to Linux