mardi 17 octobre 2017

working with external device, .net application

i am writting a driver for card reader to integrated card reader device with my application. i would like to know is there any approach or design pattern which would suit me? reader is a com port one and has a few commands:

  1. checkReader
  2. doPrePayment
  3. doPostPayment

in code it looks like that :

lock(locketObject)
{
  Write(0x05);
  Read(0x06);
  a certain command
  Read(0x06);
  Write(0x04);
}

after performing a command , reader responds as an array of bytes :

serialPort.DataReceived += SerialPortOnDataReceived 
private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
        byte[] buffer = new byte[serialPort.BytesToRead];
        serialPort.Read(buffer, 0, buffer.Length);
        string data = Encoding.ASCII.GetString(buffer);

        // NEED TO PARSE READER DATA

        if(IsCheckReaderResponse(data)){
            ProcessCheckReaderResponse();
        }
        else if(IsdoPrePaymentResponse(data)){
            ProcessdoPrePaymentResponse();
        }
        else if(IsdoPostPaymentResponse(data)){
            ProcessdoPostPaymentResponse();
        }
        else{
            ProcessUnknownResponse();
        }
}

i have faced a few problems:

  1. when two commands performed at the same time , reader start processing two command and send messy response containd answer for both commands.
  2. i need to prevent the call of another command while the current on has not yet recieved reply from reader.

Aucun commentaire:

Enregistrer un commentaire