473,785 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_getPortNam e

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 2110
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******@hotma il.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**********@n ews-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>|<un it>|<enabled>|< and so on
You can forget about that part, the error lies in the "Port_getPortNu m"
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_getPortNam e

// 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 someTestFunctio n()
{
// 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_getPortNam e

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_getPortNam e */

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********@yah oo.com) (cb********@wor ldnet.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
8927
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 onclick="javaScript:alert('document.forms(0)='+document.forms(0)); parent.myFunc(document.forms(0));" type="button" value="Open" name="Button" ID="Button"> The strange part is that the debug alert says that the document.forms(0) is an object så all seem to be well. But...
25
3744
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 machines. The code opens MS Word through Automation and then opens a particular Word doc. It's still working fine on most machines; but on one or two of them, the user is getting an Automation Error. The code used is as follows: Dim objWord As...
0
328
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 files\spsweb\0e3514bf\cb1844e7\assembly\dl2\3b163f 16\00452d31_84e5c301\infragistics.webui.ultrawebgrid.v3.dll' could not be found
6
1700
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 database than any of our previous customers. For example, the query to build the datatable now takes about 2 minutes instead of one minute or less. This is a third party database we are integrating with. He is getting very strange results. For...
5
1737
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) +313 System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState) +201 System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState) +263
0
3574
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 requests database, and query execution time exceeds 30 seconds, the following error reported: ===
11
2598
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 temperatures. T = 20 degrees at all times.... The 2D T-array looks like this:
1
1557
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 in UTF-8. Now minidom is always able to parse these files with minidom.parse("file") . Now when fetching I use this expression: xmldoc.getElementsByTagName('DocNumb').firstChild.data.encode('latin1')
11
2501
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 support team for more information. However I'm not purposely requesting that the Runtime terminate in an "unusual way." The line that is causing me headaches is:
3
1837
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 is intermittent -- sometimes it happens and sometimes not from the same screen with the same data. Here is the error:
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.