locked
c# serialport data received event. RRS feed

  • Question

  • Hi,

    Am having a problem reading data from the serial port of a bill validator. I am currently receiving and sending data, the problem I have is checking whether the received bytes are equal to some headers for the bill validator and process the data respectively.

    for instance if the bytes I receive are equal to a hex value 80 or a hex value 8f I know that the bill validator is on and is polling and I should send a hex value of 02 to stop the polling.

    this is my data received event

    privatevoidport_DataReceived(objectsender, SerialDataReceivedEventArgse)

    {

    if(!comport.IsOpen) return;

    intbytes = comport.BytesToRead;

    byte[] data = newbyte[bytes];

    comport.Read(data, 0, bytes);

    Log(LogMsgType.Incoming, ByteArrayToHexString(data));

    processData(data);

    }

    this is my processData method

    publicvoidprocessData(byte[] value)

    {

    if(value == HexStringToByteArray("80"))

    {

    WriteData("02");

    }

    elseif(value == HexStringToByteArray("81"))

    {

    WriteData("02");

    }

    }

    what could be the problem? thank you in advance.

    Monday, November 20, 2017 9:00 AM

Answers