-1

I have to read serialport and store that response which is a sequence of six bytes in a string variable .I am trying to store the response in string variable by concatenate the response. But only the last byte from the response is getting stored.

        int response = port.ReadByte();
        string responseString = "";
        responseString  += response.ToString();
        Console.WriteLine(responseString);
Mishty
  • 123
  • 2
  • 13
  • The way you read from serialport seems not right. Try the method mentioned here https://stackoverflow.com/questions/13754694/what-is-the-correct-way-to-read-a-serial-port-using-net-framework – Ha Bom Dec 29 '18 at 09:10
  • Please post real [MCVE] so people can help you with your problem (i.e. currently posted code only reads one byte and always set `responseString` to empty for that single byte) . Otherwise link provided by @HaBom should be enough - maybe question should be closed as duplicate of it https://stackoverflow.com/questions/13754694/what-is-the-correct-way-to-read-a-serial-port-using-net-framework. – Alexei Levenkov Dec 29 '18 at 10:11
  • Is that code in the DataReceived() event? If so, move the `string responseString = "";` line out the the class/form level so that it doesn't get reset each time. – Idle_Mind Dec 29 '18 at 13:52

1 Answers1

0
Port.ReadByte()  

Synchronously reads one byte from the SerialPort input buffer.

So you probably want to use:

Port.ReadExisting()

Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object.

Flightdoc5242
  • 199
  • 1
  • 2
  • 12