Anything better then SerialPort Write

I have been reading that C# is more or less a like C but with enhancements.
Coming from C, i used printf quite a bit.
So i am seeing that i have to use the SerialPort object.
What is blowing my mind is how can C# be better when before in C and wanted to print say “Hello World” all i had to do was

printf(“Hello World\r\n”);

but now i have to do all this:

string counter_string = “Hello World\r\n”;
byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
UART.Write(buffer, 0, buffer.Length);

so how is this better, more efficient ? Seems to me the guys to made C# just like to type more.

Every language has its advantages and disadvantages. C# is not C.
There are many good books on that.
If you ever look at the code for printf, you’ll see all the stuff that happens there.
You may want to change the way you do things. As a rough example, make an object to handle the “small” stuff behind the scenes.

   
   class DbgPrint
   {
      static SerialPort UART = new SerialPort("COM1", 115200);

      public static void init()
      {
         // Open UART before printing
         UART.Open();
      }    

      public static void myPrint(string myString)
      {
         // convert the string to bytes
         byte[] myBuff = Encoding.UTF8.GetBytes(myString + "\r\n");
         // send the bytes on the serial port
         UART.Write(myBuff, 0, myBuff.Length);
      }
. . .
   }

Then just call the procedures from your other code, i.e.

         
   OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

   // Open COM1 as debugging terminal
   DbgPrint.init();
   .
   .
   DbgPrint.myPrint("Start LCD_Init");

I see. Thank you for the GREAT example!

Now how do you guys handle formatting then ?

printf(“X=%04u\r\n”,x);

It is easier with C#, search the web for info on strings on C#.

int x = 5;

print("value of x is " + x.ToString());

or better

print("value of x is " + x);

I have a few of books coming on C#.

In that example though it does not take care of the formatting.
If x = 2, the output will be “value of x is 2”

It will not print “value of x is 0002”

I will do some searching…

print("value of x is " + x.ToString(“0000”));

or better…

print(String.Format(“value of x is {0:0000}”, x);

Sweet, thank you much.

print(String.Format("value of x is {0:0000}", x);

There is no String.Format in Micro Framework so that wont work.

So then how do you guys get that result ?
Say i want to print something like temp on an LCD.
temp = 123.45

when the temp goes down to “1.23” i want the lcd to show “001.23”
are you telling me that i have to create code to watch for the length and have a routine to add the “0”'s in place ?

If this is the case, that sounds very lame at best. Why would C# drop the wonderful and powerful options of printf.

http://netmfcommonext.codeplex.com/documentation

Thanks Architect for that link! But what reference do i need to be able to use StringUtility.Format ?

StringUtility.Format is an extension to MF. You have to download the extensions from CodePlex…

Download the extension assembly from:

http://netmfcommonext.codeplex.com/releases/view/54949

then reference the NetMf.CommonExtensions.dll

I bet by you guys wish i was by you so you could slap me upside the back of my head huh ?

I was reading that and did just that before your post. Now for an even more naive question. where do i put those files so i can add the reference ?

Doesn’t really matter. VS will copy them to your bin directory during compile. Personally, I keep a folder called “3rd Party Components” to keep all this stuff in.

I got it working now. Thank to all for the help.

let me ask you guys this.
I have been looking here
http://msdn.microsoft.com/en-US/library/67ef8sbd(v=VS.80).aspx
but its clear to me that this is not where i should be at for learning the micro framework.
Yet i cannot find a page like this for micro framework.

The reason i ask is when i was looking here; for string formatting it shows how to use it yet the MF does not have it. So what gives ?

Where can i find information like these links that just deal with the MF, so know what i have available to me to use, and not try to use something that is not available to me.