LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-14-2012, 05:27 AM   #1
akaur16
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
Sementation Fault


Hi Guys,

Im trying to run a program by calling a function from another .cc file
the code compiles but when I try to run it, I get a segmentation fault.

Im trying to control an electronic piece of equipment. The drivers are all installed.

Here is my main program.

testprogram.cc


//Program to run the HV Supply.

#include "caen.hh"


int main(int nargs, char ** args)
{
CAEN hv;
hv.setHV(0,100); //Set channel 0 to 100 volts
return 0;
}

My CAEN program
caen.cc


#include "caen.hh"
//#include "MaxCamSerial.hh"

#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>

using namespace std;

int channel_number = 0; //or whatever
double value = 100.5; //or whatever

CAEN::CAEN(const char * dev, int board)
{
//make a copy of the device string.
device = strdup (dev);
bd = board;
}

CAEN::~CAEN()
{
free(device);
}


int CAEN::rawCaenCmd(const char * cmd, char ** response, double pause)
{

CAEN c("/dev/ttyUSB0",0);

int baudRate = 9600;
int dataBits = 8;
int stopBits = 1;
int xonxoff=1;
int rtscts = 0;
char parity = 'n';

return 0;
}


int CAEN::setHV(int channel_number,double value)
{

CAEN c("/dev/ttyUSB0",0);
char cmd[128];
sprintf(cmd,"$BD:0,CMD:SET,CH:%d,PAR:VSET,VAL:%06.1f", writeID, setValue);
rawCaenCmd(cmd,NULL,0.1);

return 0;

}

double CAEN::readHV(int channel_number)
{

CAEN c("/dev/ttyUSB0",0);
char * cmd ="$BD:0,CMD:SET,CH:0,PAR:VSET,VAL:1000.0";
char * resp;
c.rawCaenCmd(cmd,&resp,0.5);
cout << resp << endl;

return 0;
}

my header file

caen.hh


#ifndef CAEN_HH
#define CAEN_HH

class CAEN
{

public: //these method are available to call by the user of this class

CAEN(const char * device = "/dev/ttyUSB0", int board = 0);

~CAEN();

// set the value of channel channel_number to value . Returns 0 if it worked, some other value if it didn't.
int setHV(int channel_number, double value);

// reads the value currently for the channel channel_number
double readHV(int channel_number);


int rawCaenCmd(const char * cmd, char ** response, double pause = 0.1);


/* Getters */
const char * getDevice() { return device; }
int getBoard() { return bd; }

private:

int readID;
int writeID;
int setValue;

char * device;
int bd;


};

#endif


my makefile:

#when you type make in this directory, it reads this file to figure out how to compile

#define some variables

CC=g++

OBJS= caen.cc
CFLAGS = -g
LDFLAGS = -o testprogram

libCaen.so: $(OBJS)
$(CC) -shared -o $@ $^ $(LDFLAGS)

%.o: %.cc $ caen.hh
$(CC) $(CFLAGS) -c $?

clean:
rm *.o *.so

testprogram: testprogram.cc caen.o
$(CC) $(CFLAGS)-o $@ $^ $(LDFLAGS)


All I get is segmentation fault
Any idea?

Any help would be appreciated!!
Thanks

Last edited by akaur16; 02-14-2012 at 05:31 AM.
 
Old 02-14-2012, 08:37 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Have you tried running with gdb to see what is going on when the seg fault occurs?
 
Old 02-14-2012, 08:40 AM   #3
akaur16
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yeah....all that comes up is

Program recieved signal SIGSEGV, Segnebtation fault
0x00000001 in ?? ()
 
Old 02-14-2012, 09:20 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
I don't normally use makefiles, some I'm not an expert on what is right in a makefile, but some of what you have in the makefile is wrong enough to be evident to a non expert:

Quote:
Originally Posted by akaur16 View Post
my makefile:

#when you type make in this directory, it reads this file to figure out how to compile

#define some variables

CC=g++

OBJS= caen.cc
CFLAGS = -g
LDFLAGS = -o testprogram

libCaen.so: $(OBJS)
$(CC) -shared -o $@ $^ $(LDFLAGS)

%.o: %.cc $ caen.hh
$(CC) $(CFLAGS) -c $?

clean:
rm *.o *.so

testprogram: testprogram.cc caen.o
$(CC) $(CFLAGS)-o $@ $^ $(LDFLAGS)
I think there is more wrong with that makefile than what I marked in red. You also seem to be compiling caen.cc two different ways and I'm not sure that works.

But the interaction between the two things I marked in red seems to be the most obviously wrong bit.

Last edited by johnsfine; 02-14-2012 at 09:22 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
seg fault / mem fault - in cron, but works in shell? kauaikat Linux - Software 1 04-29-2008 09:21 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 10:34 AM
sementation fault slackware 9.0 on 486 syrtsardo Slackware 2 09-28-2003 06:41 AM
sementation fault khutze Programming 1 06-06-2003 04:26 AM


All times are GMT -5. The time now is 10:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration