1

I am working on an RS232 communication effort but have been running into issues with the some of the arrays I am handling.

In the example below I am sending out a "Command" with the intent to read and store the first 4 bytes of the command in a new array called "FirstFour". For every loop execution that is run I also want to convert the integer "i" to a Hex value. I then intend to combine the "FirstFour" and "iHex" arrays into a new array noted as "ComboByte". Below is my code so far but it doesn't seem to be working.

private void ReadStoreCreateByteArray() { byte[] Command = { 0x01, 0x02, 0x05, 0x04, 0x05, 0x06, 0x07, 0x08}; for (int i = 0; i < 10; i++) { //Send Command comport.Write(Command, 0, Command.Length); //Read response and store in buffer int bytes = comport.BytesToRead; byte[] Buffer = new byte[bytes]; comport.Read(Buffer, 0, bytes); //Create 4 byte array to hold first 4 bytes out of Command var FirstFour = Buffer.Take(4).ToArray(); //Convert i to a Hex value byte iHex = Convert.ToByte(i.ToString()); //Combine "FirstFour" and "iHex" into a new array byte [] ComboByte = {iHex, FirstFour[1], FirstFour[2], FirstFour[3], First Four[4]}; comport.Write(ComboByte, 0, ComboByte.Length); } } 

Any help would be appreciated. Thanks!

4
  • comport.Read(Buffer, 0, bytes); it is essential to handle the return method from Read correctly, btw. Commented Jan 24, 2014 at 13:53
  • What is respBuffer? Commented Jan 24, 2014 at 13:55
  • Is the ComboByte initialization correct? Shouldn't FirstFour be indexed from 0-3 and not 1-4? Commented Jan 24, 2014 at 13:56
  • byte iHex = Convert.ToByte(i.ToString()) should just be byte iHex = (byte)i Commented Jan 24, 2014 at 14:11

2 Answers 2

1

Arrays are zero based, so...

byte [] ComboByte = {iHex, FirstFour[0], FirstFour[1], FirstFour[2], First Four[3]}; 

...should give you the first 4 elements of FirstFour.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for catching that
1

Firstly, you need to handle the return value from Read operations. The following pattern should work fine for a range of APIs, including SerialPort, Stream, etc:

static void ReadExact(SerialPort port, byte[] buffer, int offset, int count) { int read; while(count > 0 && (read = port.Read(buffer, offset, count)) > 0) { count -= read; offset += read; } if (count != 0) throw new EndOfStreamException(); } 

So: you have a method that can read a reliable number of bytes - you should then be able to re-use a single buffer and populate it sequentially:

byte[] buffer = new byte[5]; for (int i = 0; i < 10; i++) { //... buffer[0] = (byte)i; ReadExact(port, buffer, 1, 4); } 

The BytesToRead property is largely useless except for deciding whether to read synchronously or asynchronously, as it doesn't tell you whether more data is imminent. With your existing code there is no guarantee you will have at least 4 bytes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.