Go to Post I find it quite amazing that this thread has been continuously active for more than two years. - Stephanie [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 28-01-2008, 20:22
Mr. Freeman Mr. Freeman is offline
Put better title here.
AKA: Kellan
no team (CSM Robotics)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Colorado
Posts: 198
Mr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to behold
Problem with printf()

I'm getting the weirdest syntax error ever. My code is as follows (This is in the user_routines.c file)
Code:
void Process_Data_From_Master_uP(void)
{
printf("lkjhjasfdkjhashddsfkjlkasdhfdlkjkljsadhfklkjasjdhf");

// for debugging
static int counter = 0;
When I remove the printf statement, the program compiles but the printf's that are farther down don't get printed. With the printf() statement in, the compiler returns a syntax error on the following line:
static int counter = 0;

The error returned is:
Code:
C:\2008_code_svn\08_bot_working\user_routines.c:199:Error: syntax error
Halting build on first failure as requested.
BUILD FAILED: Mon Jan 28 18:17:43 2008
I can't find anything wrong with this line or any above it. I don't know why I'm getting a syntax error. Any help would be much appreciated.
__________________
"Time is an illusion, lunchtime doubly so."
-Douglas Adams
“Why is it that I can walk into Wal-Mart and buy boxes of bullets and black powder, but I can’t buy potassium perchlorate to do science because it can also be used to make explosives?”
-Theodore Gray
  #2   Spotlight this post!  
Unread 28-01-2008, 20:28
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: Problem with printf()

Quote:
Originally Posted by Mr. Freeman View Post
I can't find anything wrong with this line or any above it. I don't know why I'm getting a syntax error. Any help would be much appreciated.
In ANSI C, you have to declare all variables before executing any other code in a block. I can't be certain if this is your problem, but try moving the "static int counter = 0;" line above the printf and see if it compiles.

If that is indeed the problem, I would have hoped that C18 would have given a better error message. Sadly, C18 seems to do a poor job with errors in general from what I have seen.
  #3   Spotlight this post!  
Unread 28-01-2008, 20:29
psy_wombats's Avatar
psy_wombats psy_wombats is offline
Registered User
AKA: A. King
FRC #0467 (Duct Tape Bandits)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Shrewsbury MA
Posts: 95
psy_wombats has a spectacular aura aboutpsy_wombats has a spectacular aura aboutpsy_wombats has a spectacular aura about
Re: Problem with printf()

The problem is that you are declaring a variable after you've begun to give commands. C doesn't like this, but isn't very informative when it spits up 'syntax error.' (At least I'm pretty sure.) Anyway, I don't know why the printfs further on aren't printing, but try using your printf after the counter is declared.
  #4   Spotlight this post!  
Unread 29-01-2008, 16:54
Mr. Freeman Mr. Freeman is offline
Put better title here.
AKA: Kellan
no team (CSM Robotics)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Colorado
Posts: 198
Mr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to behold
Re: Problem with printf()

Thanks for all of your help, but I'm still having trouble getting this working. I've moved the printf statement to below all the variable declarations. The code compiles fine with no errors or warnings, but the printf still doesn't work.

Our software mentor should be showing up in a few hours, so hopefully he'll be able to help if I can't get it working by then, but I'd like to solve this as soon as possible because it's holding up the testing of our robot.

Here is the code as it looks now. I'm pretty sure there isn't anything wrong with it, but I can't explain the fact that it doesn't work. I'd like to post more relevant sections, but I'm getting 0 errors and 0 warnings.

Code:
void Process_Data_From_Master_uP(void)
{

// for debugging
static int counter = 0;

	unsigned char byte_count;
	unsigned char data;
	unsigned char j;

printf("akjsdfusdhafkjsa");

	Getdata(&rxdata);
__________________
"Time is an illusion, lunchtime doubly so."
-Douglas Adams
“Why is it that I can walk into Wal-Mart and buy boxes of bullets and black powder, but I can’t buy potassium perchlorate to do science because it can also be used to make explosives?”
-Theodore Gray
  #5   Spotlight this post!  
Unread 29-01-2008, 17:38
B_Dubbs B_Dubbs is offline
Registered User
AKA: Brian
FRC #1153
Team Role: Programmer
 
Join Date: Mar 2007
Rookie Year: 2006
Location: Walpole, MA
Posts: 29
B_Dubbs is an unknown quantity at this point
Re: Problem with printf()

Make sure that you have included stdio.h at the begining of the program like so
Code:
#include <stdio.h>
If that doesn't work then try adding
Code:
-mL -nw=2066 -D_FRC_BOARD
to the build options menu MPLabC18 tab, then click use alternate settings and add that to the end so it looks like this:
Code:
-Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa- -mL -nw=2066 -D_FRC_BOARD
Hopefully this will help you with your problem.
  #6   Spotlight this post!  
Unread 29-01-2008, 18:02
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Cañada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Cañada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: Problem with printf()

Quote:
Originally Posted by Mr. Freeman View Post
Thanks for all of your help, but I'm still having trouble getting this working. I've moved the printf statement to below all the variable declarations. The code compiles fine with no errors or warnings, but the printf still doesn't work.

Our software mentor should be showing up in a few hours, so hopefully he'll be able to help if I can't get it working by then, but I'd like to solve this as soon as possible because it's holding up the testing of our robot.

Here is the code as it looks now. I'm pretty sure there isn't anything wrong with it, but I can't explain the fact that it doesn't work. I'd like to post more relevant sections, but I'm getting 0 errors and 0 warnings.

Code:
void Process_Data_From_Master_uP(void)
{
 
// for debugging
static int counter = 0;
 
    unsigned char byte_count;
    unsigned char data;
    unsigned char j;
 
printf("akjsdfusdhafkjsa");
 
    Getdata(&rxdata);
There is some weirdness with Microchip's implementation of the stdio library that necessitates the use of the return and new line characters at the end of a string. Try this:

printf("akjsdfusdhafkjsa\r\n");

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #7   Spotlight this post!  
Unread 29-01-2008, 18:28
Roger Roger is offline
Registered User
FRC #1153
 
Join Date: Jan 2006
Rookie Year: 1900
Location: Walpole MA
Posts: 688
Roger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond reputeRoger has a reputation beyond repute
Re: Problem with printf()

That seems curious, Kevin. We've done printf's without "\r\n" at all, and it still works.
  #8   Spotlight this post!  
Unread 29-01-2008, 20:39
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Cañada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Cañada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: Problem with printf()

Quote:
Originally Posted by Roger View Post
That seems curious, Kevin. We've done printf's without "\r\n" at all, and it still works.
I'm not sure if printf() always needs to see \r\n, but I've experienced this problem in the past with the 2.4 compiler (I'm not sure about 3.1). I just threw up my hands and always use \r\n in my code.

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #9   Spotlight this post!  
Unread 29-01-2008, 23:33
Chaos in a Can Chaos in a Can is offline
Joel Spadin
FRC #1736 (Robot Casserole)
Team Role: Programmer
 
Join Date: Mar 2007
Rookie Year: 2007
Location: Peoria, IL
Posts: 58
Chaos in a Can is an unknown quantity at this point
Send a message via MSN to Chaos in a Can
Re: Problem with printf()

I've had some odd problems with printf myself, though I've never had a problem with \r\n.

When I used a printf with about seven %d's, something that should have shown up as a 1 or 0 was printing about 150.
It wasn't until I tried using separate printf's for each value that I found that it had either swapped or skipped some of the values and was printing an input from the joystick where it should have been printing a digital input.
__________________
2006 Xerox Creativity Award
2007 General Motors Industrial Design Award
2008 Judges' Award
2009 Chrysler Team Spirit Award

  #10   Spotlight this post!  
Unread 30-01-2008, 00:12
Mr. Freeman Mr. Freeman is offline
Put better title here.
AKA: Kellan
no team (CSM Robotics)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Colorado
Posts: 198
Mr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to beholdMr. Freeman is a splendid one to behold
Re: Problem with printf()

I'd like to thank everyone again for their help.

Here's how we solved it:
There was a lot of code not working in addition to the printf(). However, I discovered some printf()s that were actually working, in a different section of the code.
It turns out that we had to have the operator interface plugged in for that code to work. We scratched our heads for awhile about this and came to the conclusion (one of us finally remembered) that without a connection to the OI, the watchdog processor must set all PWMs & Relays to 127 & 0, respectively.

Upon further investigation and testing, we think that with no connection to the OI, the process_data_from_master_up() function never gets called from main.c
However, once a connection with the OI has been established and then lost, the RC keeps printing whatever it should have been printing before the connection to the OI was lost.

The reason we took so long to hook the OI up was that we were under the assumption that we had had this code working and printing before without an OI connection in the past. We now believe that perhaps we did have a connection to the OI at the time we were testing, but simply didn't remember that we hooked it up.

This thread has still yielded some good advice. I was not aware that putting commands before all vars were declared was incorrect, and I now have a couple things to try when my printf()s stop working in the future.

Again, thank you everyone for your help.
__________________
"Time is an illusion, lunchtime doubly so."
-Douglas Adams
“Why is it that I can walk into Wal-Mart and buy boxes of bullets and black powder, but I can’t buy potassium perchlorate to do science because it can also be used to make explosives?”
-Theodore Gray
  #11   Spotlight this post!  
Unread 30-01-2008, 07:54
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Problem with printf()

Quote:
Originally Posted by Chaos in a Can View Post
When I used a printf with about seven %d's, something that should have shown up as a 1 or 0 was printing about 150.
It wasn't until I tried using separate printf's for each value that I found that it had either swapped or skipped some of the values and was printing an input from the joystick where it should have been printing a digital input.
This is a symptom of not matching the format information with the size of the variable to be printed. %d wants an int. If you instead give it a char, it will also grab the next byte of data to go with it, and everything will be misaligned from that point on. It's a good idea to get in the habit of always explicitly casting your %d-printed variables as (int).
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
printf problem miketwalker Programming 7 31-01-2005 13:19
printf problem cbolin Programming 4 09-11-2004 02:44
Anyone with a replacement Printf? Larry Barello Programming 5 19-03-2004 02:20
printf isn't printf-ing. Help! Meandmyself Programming 14 15-02-2004 16:27


All times are GMT -5. The time now is 19:11.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi