Main Site Documentation

SerialPort.Write return value


#1

According to the MSDN site, SerialPort.Write is supposed to return the number of bytes written:

C#
[MethodImplAttribute(InternalCall)]
public int Write (
         byte[] buffer,
         int offset,
         int count
)
Parameters
buffer
The byte array that contains the data to write to the port.
offset
The zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.
count
The number of bytes to write.
Return Value
The number of bytes written.

This code works

       private static int bytesUploaded = 0;

        public static bool uploadBlock(byte[] bytesToSend)
        {
            consolePort.Write(bytesToSend, 0, bytesToSend.Length);
            consolePort.Flush();
            return true;
        }

but this code give a "Cannot implicitly convert type ‘void’ to ‘int’ " error

      private static int bytesUploaded = 0;

        public static bool uploadBlock(byte[] bytesToSend)
        {
            bytesUploaded = consolePort.Write(bytesToSend, 0, bytesToSend.Length);
            consolePort.Flush();
            return true;
        }
        

What dumb thing am I doing now?

Thanks


#2

@ Gene - There is nothing wrong with your code from what I see. The method is implemented as follows:


public int Write (
         byte[] buffer,
         int offset,
         int count
)

This should not be needed, but could you try:


#3

James

Thanks for the quick response.

        private static int bytesUploaded = 0;

        public static bool uploadBlock(byte[] bytesToSend)
        {
            bytesUploaded = (int)consolePort.Write(bytesToSend, 0, bytesToSend.Length);
            consolePort.Flush();
            return true;
        }

Gives the same error. I always assume that I’m doing something wrong and am not usually disappointed. But in this case the error is pretty clear that SerialPort.write is returning a void which seems to be at odds with the MSDN page. Maybe for once it isn’t my mistake.


#4

@ Gene - What hardware are you using as well as which Visual Studio?


#5

@ Gene - Actually, according to the actual implementation, MSDN is wrong. It does in fact return a void. A bug report should be submitted to either A) Return a value or B) Fix the documentation on MSDN.


#6

Some of the methods in NETMF are not exactly implemented the same wa as in win.net. To make it worse, the NETMF api docs are sometimes a copy of the win.net docs. Sometimes whole methods are missing, even the docs says it’s there. In NETMF it is often better to trust intelli sense


#7

Well it is nice to know it isn’t me for once although it is a little disappointing that the MSDN .Net Micro 4.2 documentation is wrong.

Can you tell me how to submit a bug report?

A little late at this point but I’m using a Spider board and VS2010.

Thanks again for the remarkably quick help.

Cheers - Gene


#8

@ Gene - go to netmf.codeplex.com. There you can report bugs to the Microsoft team.


#9

OK, I posted the issue. Hope they don’t bar me for life from MSDN.