Search code examples
c#winformsc#-4.0serial-portcdc

Send AT command to a CDC reader and get response


I have this CDC contactless reader that is connected with USB that appears as a virtual serial port. I'm able to find the port and send AT commands with the following code:

string strPort = GetPortName();

SerialPort srPort = new SerialPort(strPort, 9600);

srPort.Open();

if (srPort.IsOpen)
{
    //device info
    srPort.WriteLine(@"ATI\r" + (char)(13));
    System.Threading.Thread.Sleep(200);

    MessageBox.Show("Device: " + srPort.ReadExisting());

    //card UID
    srPort.WriteLine(@"AT+i\r" + (char)(13));
    System.Threading.Thread.Sleep(200);

    string strRes = srPort.ReadExisting();

    MessageBox.Show("UID: " + strRes);

    srPort.Close();
}

I am sending the commands according to the device documentation. First command goes OK, but the second, which is supposed to give me the card UID is always returning ERROR.

Am I sending the command string to the port in a right way? Because the second command has a + sign in between and I am thinking maybe it is the cause of the error?

I tried to contact their support, but no feedback. So I hope to get some help here.


Solution

  • So after testing reading and testing the commands from documentation, I realized that my reader was in Scan mode, which means it ignores all the commands sent to device. I turned the Scan mode off and was able to get the UID of the card.

    I also replaced WriteLine with `Write' and used only ASCII byte array instead of a string command.