LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2014, 01:36 PM   #1
KFarm
LQ Newbie
 
Registered: Nov 2014
Posts: 5

Rep: Reputation: Disabled
Program compiles just fine but has a Syntax error when I try to run it


Code:
#include "currentLoop.h"
#include "stdio.h"
#define CHANNEL CHANNEL4

//Include FILE Variables
FILE* value_file;
FILE* voltage_file;
FILE* current_file;

void setup()
{
  //Inits the Serial for viewing data in the serial monitor.


  //Switch ON the 24V DC-DC converter
  sensorBoard.ON();

  delay(100);
  printf("Arduino 4-20 mA board switched ON...");
  printf("\n");
  
  //Open 3 Files
  value_file = fopen("Value.txt","w");
  voltage_file = fopen("Voltage.txt","w");
  current_file = fopen("Current.txt","w");
  delay(5);
  
  printf("All Files Opened..");
  printf("\n");
}

void loop()
{
  if (sensorBoard.isConnected(CHANNEL))
  {
    // Get the sensor value in int format (0-1023).
    int value = sensorBoard.readChannel(CHANNEL);
    printf("Int value read from channel 1 : %d",value);
    printf("\n");
	
	//log data to the Value File
	fprintf(value_file, "%d\n", value);
	fflush(value_file);

    // Get the sensor value as a voltage in Volts.
    float voltage = sensorBoard.readVoltage(CHANNEL);
    printf("Voltage value rad from channel 1 : %f",voltage);
    printf(" V");
    printf("\n");
	
	//log data to the Voltage File
	fprintf(voltage_file, "%f\n", voltage);
	fflush(voltage_file);

    // Get the sensor value as a curren in mA.
    float current = sensorBoard.readCurrent(CHANNEL);
    printf("Current value read from channel 1 : %f",current);
    printf(" mA");
    printf("\n");
	
	//log data to the Current File
	fprintf(current_file, "%f\n", current);
	fflush(current_file);

    printf("***************************************");
    printf("\n");

  }
  else {

    printf("The sensor is not connected...");
    printf("\n");

  }

  //Delay after reading.
  delay(2500);

}

int main (){
  setup();
  while(1){
    loop();
  }
  return (0);
}
This program compiles just fine but will not run. Syntax error: word unexpected (expecting ")") is the error I get when I run it.

The program worked just fine until it was modiefied to write to file. I am running the program in Linux on a Raspberry Pi. It is compiled on the Pi with g++.

Thanks

Last edited by KFarm; 11-21-2014 at 02:00 PM. Reason: adding information
 
Old 11-21-2014, 02:53 PM   #2
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Rep: Reputation: 15
Have you tried to write a dummy file test.txt with "test" as the data?
Trying writing a file without using your inputted data and not going through the loop.
 
Old 11-21-2014, 02:58 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
how exactly did you compile it, and what is the complete error message.
please post both commands & their respective output.
 
Old 11-21-2014, 03:14 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
The syntax error has nothing to do with this program. Use command 'set -vx' to debug the problematic script.
 
1 members found this post helpful.
Old 11-21-2014, 03:54 PM   #5
KFarm
LQ Newbie
 
Registered: Nov 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
set -vx

g++ -c RaspMaincodeNew5.cpp -o RaspMaincodeNew5
pi@raspberrypi ~/library $ chmod +x RaspMaincodeNew5
pi@raspberrypi ~/library $ sudo ./RaspMaincodeNew5
./RaspMaincodeNew5: 1: ./RaspMaincodeNew5: Syntax error: word unexpected (expecting ")")
pi@raspberrypi ~/library $ set -vx
pi@raspberrypi ~/library $ sudo ./RaspMaincodeNew5
sudo ./RaspMaincodeNew5
+ sudo ./RaspMaincodeNew5
./RaspMaincodeNew5: 1: ./RaspMaincodeNew5: Syntax error: word unexpected (expecting ")")

So I tried the above steps.
 
Old 11-21-2014, 04:25 PM   #6
KFarm
LQ Newbie
 
Registered: Nov 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
same error different program

Ok so I tried to run a program that used to work just fine and I got the same error. So I am reinstalling the OS on the raspberry Pi. I will se if that fixes it.
 
Old 11-21-2014, 07:27 PM   #7
KFarm
LQ Newbie
 
Registered: Nov 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
SOLVED works now

So I got it working...

pi@raspberrypi ~ $ cd ~/upload
pi@raspberrypi ~/upload $ g++ -c arduPi.cpp -o arduPi.o
pi@raspberrypi ~/upload $ g++ -c currentLoop.cpp -o currentLoop.o
pi@raspberrypi ~/upload $ g++ -lpthread -lrt
/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o: In function `_start':
(.text+0x34): undefined reference to `main'
collect2: ld returned 1 exit status
pi@raspberrypi ~/upload $ ls
arduPi.cpp arduPi.o currentLoop.h RaspMaincode.cpp
arduPi.h currentLoop.cpp currentLoop.o
pi@raspberrypi ~/upload $ g++ -lpthread -lrt RaspMaincode.cpp arduPi.o currentloop.o -o Raspmaincode
g++: error: currentloop.o: No such file or directory
pi@raspberrypi ~/upload $ ls
arduPi.cpp arduPi.o currentLoop.h RaspMaincode.cpp
arduPi.h currentLoop.cpp currentLoop.o
pi@raspberrypi ~/upload $ g++ -lpthread -lrt RaspMaincode.cpp arduPi.o currentLoop.o -o Raspmaincode
pi@raspberrypi ~/upload $ chmode +x Raspmaincode
bash: chmode: command not found
pi@raspberrypi ~/upload $ chmod +x Raspmaincode
pi@raspberrypi ~/upload $ sudo ./Raspmaincode
Arduino 4-20 mA board switched ON...
All Files Opened..
Int value read from channel 1 : 1073
Voltage value rad from channel 1 : 5
and so on...

I had to reinstall my Raspberry Pi Operating System Rasparian then the above procedure worked...
 
Old 11-22-2014, 08:55 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,851
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
Forget 'chmod': object modules aren't meant to be executed. Read some gcc-howto.
Also, you can create executable in one command:
Code:
g++ -o RaspMaincode arduPi.cpp RaspMaincode.cpp -lpthread -lrt

Last edited by NevemTeve; 11-22-2014 at 10:27 AM.
 
1 members found this post helpful.
Old 11-22-2014, 10:36 AM   #9
KFarm
LQ Newbie
 
Registered: Nov 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks

Thanks for the clarification.
 
  


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
Program compiles and runs on 12.0-32 compiles on 14.0-64 but won't run. stf92 Slackware 6 01-23-2014 06:43 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
icculus.org Duke Nukem 3d: compiles fine but won't run mr.gizm0 Linux - Games 1 04-23-2005 04:03 AM
G++ compiles fine but program does not work MaddDog696 Programming 3 04-15-2004 05:39 PM
compiles fine but a make error bosewicht Linux - Newbie 3 01-04-2004 06:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:09 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
Open Source Consulting | Domain Registration