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!
comport.Read(Buffer, 0, bytes);it is essential to handle the return method fromReadcorrectly, btw.respBuffer?byte iHex = Convert.ToByte(i.ToString())should just bebyte iHex = (byte)i