简体   繁体   中英

Compiler error in “Write” statement sending byte values to SerialPort in C#.NET

I believe that this is a straightforward question, but after researching the error code in various places, it makes little sense to me.

What I am trying to achieve:

  1. Write an array of List values to a serial port

What is happening:

The statement below is generating the error, "CS0443: Syntax error, value expected" The error is located inside the pointer brackets of serPortXmitBuffer object of the "Write" statement.

mySerialPort.Write (serPortXmitBuffer[], 0, serPortXmitBuffer.Count-1); mySerialPort.Write (serPortXmitBuffer[error here], 0, serPortXmitBuffer.Count-1);

The "Write" statement is being passed a valid name in serPointXmitBuffer and I have checked that the array is there and correct. For the life of me, I have no idea why this code snippet is generating an error for a value??? The contents of the array are 11 bytes as follows: 255, 255, 2 65, 73, 77, 16, 2 0, 0, 3

What I have tried:

  1. Remove the brackets from the first passed in name, serPortXmitBuffer. (This doesn't work as doing this forces an error of, "CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List' to 'char[]'"
  2. In an illogical attempt to provide a value inside the [], I entered a zero (0). (this doesn't work either as it too generates the same exact error message as above about converting byte to char

For completeness, the code that generates the array is shown below:

        int incArrPnter;
        Console.WriteLine("Writing passed in xmitBuffer within TXPACK routine");
        for (incArrPnter = 0; incArrPnter <= _array.Count-1; incArrPnter = incArrPnter + 1)
        {

            Console.WriteLine(_array[incArrPnter]);
        }

        // Declare our new List<byte> array, serPortXmitBuffer
        // Copy all bytes in "_array" over to serPortXmitBuffer
        List<byte> serPortXmitBuffer = new List<byte>();

        // Copy "_array" over to serPortXmitBuffer
        for (incArrPnter = 0; incArrPnter <= _array.Count - 1; incArrPnter = incArrPnter + 1)
        {
            serPortXmitBuffer.Add(_array[incArrPnter]);
        }
        // Try to write the contents of serPortXmitBuffer to the serial port.
        mySerialPort.Write (serPortXmitBuffer[0], 0, serPortXmitBuffer.Count-1);

A couple of things that I learned that are not necessarily obvious to me (maybe others might not be aware so I am posting this answer:

  1. List as a structure is NOT supported by the "Write" method when trying to send bytes out a serial port. It must be a traditional array of bytes like myArray[]

  2. So far, I have found no way to allow "myArray.Count-1" to be a valid element within the Write statement to represent the total number of bytes to be transmitted. It must be either a number (int) or a variable that is an int.

I would love to be wrong on the second point, but so far, nothing I have read or seen allows me to pass "myArray.Count" or "myArray.Count - 1" as neither of these expressions resolves to an int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM