473,385 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strange error

Hi,

Sorry, I couldn't find a better title for this post. Anyway, I got a piece
of C code which only works well if I put a fprintf in it. Here's the code :

unsigned int8 Port_getPortNum( char *port )
{
fprintf(DB9, "port = %s\r\n", port ); // Don't know why
but I need this fprintf?!
if ( port[1] == 'A' )
{
if ( port[2] == 'I' ) return port[3]-48; else // [3] is a
number 1..9
if ( port[2] == 'O' ) return port[3]-38;
}else
if ( port[1] == 'D' )
{
if ( port[2] == 'I' ) return port[3]-42; else
if ( port[2] == 'O' ) return port[3]-36;
}
fprintf(DB9, "ERROR [3] = %c\r\n", port[3] ); // It shouldn't come
this far
return 0;
} // Port_getPortName

The strange thing is, if I remove the first fprintf the result isn't correct
anymore. I test it with another method :

void PORT_EXPORT( char port[4], char *result, int16 *varlen, int16
*reslen )
{
char test;
test = Port_getPortNum(port);
fprintf( DB9, "PORTID= %i\r\n",test );
<...>
Yep, I need to use a char because I can't give up an integer as parameter.
Maybe the result is ok but the printf's or conversions are wrong... But if I
don't remove that fprintf the result seems to be OK. Sometimes I experience
this problem also in other languages (on fast computers with plenty of
RAM).... What's wrong?

For your information, DB9 in the fprintf is a stream towards a rs232, I use
HyperTerminal to read out the results as it comes from a microcontroller.
And the input port parameter is a string like this : "@AI3" which stands
for Analog Input number 3.

Greetings,
Rick
Nov 13 '05 #1
6 2082
Rick wrote:
if ( port[1] == 'A' )
{
if ( port[2] == 'I' ) return port[3]-48;


I would write return port[3]-'0';

Nov 13 '05 #2
In <3f*********************@news.xs4all.nl> "Rick" <as******@hotmail.com> writes:
Sorry, I couldn't find a better title for this post. Anyway, I got a piece
of C code which only works well if I put a fprintf in it. Here's the code :


Post a *complete*, but minimal program illustrating your problem,
explaining what is your function returning with and without the
"magic" fprintf call.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
In <br**********@news-rocq.inria.fr> Grumble <in*****@kma.eu.org> writes:
Rick wrote:
if ( port[1] == 'A' )
{
if ( port[2] == 'I' ) return port[3]-48;


I would write return port[3]-'0';


I would definitely not do that, considering the rest of the function.
48 is a magical constant, just like the other 3 such constants used in
the code.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4
Thanks for taking time!

Ok, in short : I got a microcontroller (so I can expect strange stuff with
ram, minimal functions etc.) attached with RS232 to HyperTerminal. In It's
EEPROM all kind of stuff is stored. With several commands in HP I can read
or write. That part works
for now but one function for the adressing not. Of course, before start
reading anything I'll have to give a start position.
The function I showed in the previous post is for calculating the start
position for Port data. With port I mean some in- outputs that are attached
to the microcontroller. With one command I can call all the data of such a
port. A port has more than 1 properties (ala object orientated programming)
so the end result has to be a long string with all the data like this
<name>|<ID>|<unit>|<enabled>|<and so on
You can forget about that part, the error lies in the "Port_getPortNum"
function which returns the index of the asked port object. That has to be a
number from 1..14. By looking at the given name it can be calculated.
Everything works... But, only when that fprintf is included. If I remove it
for some reason it won't work. Here's some code. I didn't have the time to
setup a
working program but this might clear up the situation.

const unsigned long PORT_OFFSET = 1024*8;// EEPROM offset
const unsigned long PORT_TYPE_SIZE = 338; // Size of 1 port 'object'
char gv_SR[15]; // Serial command buffer

// This should return the port number, port is given as a string,
// @AI<number> for analog input 1..6
// @DI<number> for analog input 1..4
// @AO<number> for analog output 1..2
// @DO<number> for analog output 1..2
// In total we got 14 ports, numbered 1..14
// If I remove the first fprintf the whole IF stuff get's skipped, meaning
no
// A or D was detected on port[1].
unsigned int8 Port_getPortNum( char *port )
{
fprintf(DB9, "port = %s\r\n", port ); // MAGICAL PRINTF
if ( port[1] == 'A' )
{
if ( port[2] == 'I' ) return port[3]-48; else
if ( port[2] == 'O' ) return port[3]-38;
}else
if ( port[1] == 'D' )
{
if ( port[2] == 'I' ) return port[3]-42; else
if ( port[2] == 'O' ) return port[3]-36;
}
fprintf(DB9, "ERROR [3] = %c\r\n", port[3] );
return 0; // If it reaches this point something is wrong
} // Port_getPortName

// Read data out of EEPROM and put it into one large string.
// But before we're going to read I need to know where to start
// reading. And that's we're the error occurs
// The port parameter is the port name, result is an output buffer
void PORT_EXPORT( char port[5], char *result )
{
unsigned long pos;
unsigned int8 x; // For testing
x = Port_getPortNum(port);

fprintf( DB9, "PORTID = %U\r\n",x ); // print to RS232 stream

pos = PORT_OFFSET + ( (x-1) * PORT_TYPE_SIZE );
< the export functions, read stuff at 'pos'
we just calculated >
}

void someTestFunction()
{
// GV_SR is used to store a serial command, types in
// by hyperterminal. When I hit enter this void gets
// started. Maybe... the port parameter in PORT_EXPORT is a 5
// long string, GV_SR is 15 long. If I type '@AI1' (+ an enter) it will
// take 5. Maybe I'll have to size the parameter up to 15 as well?
char result[200] = "";

PORT_EXPORT( GV_SR, result );
< clear GV_SR for next command >
< print the result >
}
Greetings,
Rick
Nov 13 '05 #5
Dan Pop wrote:
Grumble wrote:
I would write return port[3]-'0';


I would definitely not do that, considering the rest of the function.
48 is a magical constant, just like the other 3 such constants used in
the code.


Dan,

Nothing magical about the four constants.

-48 is -'0'
-42 is -'0'+6 (+6 analog input ports)
-38 is -'0'+6+4 (+4 digital input ports)
-36 ia -'0'+6+4+2 (+2 analog output ports)

Apparently, in a single step, the original poster wants to convert
the ASCII representation of a digit to that digit and add an offset
to compute a port number.

Thus, I would use digit-'0'+offset to make the intent clearer.

Nov 13 '05 #6
Rick wrote:

Sorry, I couldn't find a better title for this post. Anyway, I
got a piece of C code which only works well if I put a fprintf
in it. Here's the code :

unsigned int8 Port_getPortNum( char *port )
{
fprintf(DB9, "port = %s\r\n", port ); // Don't know why
but I need this fprintf?!
if ( port[1] == 'A' )
{
if ( port[2] == 'I' ) return port[3]-48; else // [3] is a
number 1..9
if ( port[2] == 'O' ) return port[3]-38;
}else
if ( port[1] == 'D' )
{
if ( port[2] == 'I' ) return port[3]-42; else
if ( port[2] == 'O' ) return port[3]-36;
}
fprintf(DB9, "ERROR [3] = %c\r\n", port[3] ); // It shouldn't come
this far
return 0;
} // Port_getPortName

The strange thing is, if I remove the first fprintf the result
isn't correct anymore. I test it with another method :


Start by formatting it so it can be read and using real comments:

unsigned int8 Port_getPortNum(char *port)
{
fprintf(DB9, "port = %s\r\n", port );
/* port[3] assumed between '1' and '9' */
if (port[1] == 'A') {
if (port[2] == 'I') return port[3]-'0';
else if (port[2] == 'O') return port[3]-'0' + 10;
}
else if (port[1] == 'D') {
if (port[2] == 'I') return port[3]-'0' + 6;
else if (port[2] == 'O') return port[3]-'0' + 12;
}
/* shouldn't get here */
fprintf(DB9, "ERROR [3] = %c\r\n", port[3] );
return 0;
} /* Port_getPortName */

Now there are some obvious overlaps in possible returned values.
You obviously need some defines and #includes in addition. The
range of port[3] is never tested.

The remainder of your post is off-topic, because it deals with
non-standard C.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
0
by: Kris Vanherck | last post by:
yesterday i started getting this strange error when i try to run my asp.net project: Compiler Error Message: CS0006: Metadata file 'c:\winnt\microsoft.net\framework\v1.1.4322\temporary asp.net...
6
by: Gary | last post by:
I have an application that has been working just fine for a couple of years. It queries a SQL database and returns some formatted data back to the client. I have a new client, who has a larger...
5
by: Nathan Sokalski | last post by:
When I view my index.aspx page any time after the first time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize)...
0
by: ivb | last post by:
Hi all, I am using DB2 8.1.11.1 on NT with ASP.NET 1.1 When application make connection to database (via ADO.NET), it set "Connection timeout" parameter to 30 seconds. After, when my webpage...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
1
by: JoReiners | last post by:
Hello, I have a really strange problem. I'm unable to figure it out on my own. I parse very simple xml documents, without any check for their form. These files look very similar and are encoded...
11
by: Mike C# | last post by:
Hi all, I keep getting a strange error and can't pin it down. The message is: This application has requested the Runtime to terminate it in an unusual way. Please contact the application's...
3
by: Shelly | last post by:
I am encountering two strange problems. First one: I get a "server misconfiguration error", but only sometimes. It occurs on the first screen that accesses the database on a submit. This error...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.