Thread: Writing a Variable as a string input to a function

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    16

    Writing a Variable as a string input to a function

    Hi

    I am currently trying to write c++ for the arm 7t using Metrowerks Codewarrior.

    The issue im having is that the function to enable me to write via a COM port
    connected to the arm7t only accepts strings.

    for instance the function call would be

    Code:
     serial_print (COM0_USER, "you string here");
    where com0_user is to define the port being used. This would then display that message on say windows terminal running on a host pc.

    I need to use in in a similar way to say Cout. i.e being able to send a result from a calculation performed to the screen. Any ideas??

    This is the deffinition of serial print:-

    Code:
    void serial_print (unsigned port, char *s)
    
    {
    	while ( *s != 0 ) {
    		switch (port) {
    		case	COM0_USER:
    		while ( TX_READY(GET_STATUS(UART0_BASE))==0);
    		PUT_CHAR(UART0_BASE,*s++);
    		break;
    		case 	COM1_DEBUG:
    		while ( TX_READY(GET_STATUS(UART1_BASE))==0);
    	    PUT_CHAR(UART1_BASE,*s++);
    		break;
    		}
    	}	
    }
    Any suggestions or ideas would be welcomed, thanks all for your help

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can use a std::stringstream object to convert whatever you are trying to display into a string/const char* and then pass that to the serial_print function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    thankyou, i shall try that now;

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    16
    Ok, ive looked into that and it looks like a feasable solution but i would like to do it the following way if possible (again need help please)

    I want to make a new function called

    Code:
    void serial_print (unsigned port, x)
    where the x value could be either an iteger or a string.
    could anyone help me write this?

    i would only need it for COM0 so the code above can be simplified to

    Code:
    void serial_print (unsigned port, *x)
    
    {
    	while ( *x != 0 ) {
    		
    		while ( TX_READY(GET_STATUS(UART0_BASE))==0);
    		PUT_CHAR(UART0_BASE,*s++);
                            }
    }
    know i dont know how to implement it so it will write the full variable any help greatly appreciated.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can overload functions in C++ (same name, different arguments) so you could do:
    Code:
    void serial_print(unsigned port, int x)
    {
        // Convert int to string/const char* here using stringstream
    
        serial_port(port,converted_int_value_as_const_char_ptr);  // Call the other "string" version
    }
    
    void serial_print(unsigned port, const char* blah)
    {
        // Do your usual stuff to write data here
    }
    Then you could call either one depending on what type of data you have:
    Code:
    int value;
    ...
    serial_print(COM0_USER,value);  // Call int version
    serial_print(COM0_USER,"your string here");  // Call const char* version
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM