I am trying to retrieve device information such as IMEI, Serial Number, and other details from a device connected to my system via USB (COM6 port) in C#. The device communicates over a serial interface, and I am using the SerialPort class to send AT commands to the device and receive the response.
I am using the following code to interact with the device:
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace DEVICE_INFO
{
public partial class Form1 : Form
{
SerialPort serialPort;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string portName = "COM6"; // Specify your COM port (change if needed)
int baudRate = 115200; // Try different baud rates (9600, 38400, 115200)
serialPort = new SerialPort(portName, baudRate)
{
ReadTimeout = 5000, // Timeout for reading data (5 seconds)
WriteTimeout = 3000, // Timeout for writing data (3 seconds)
DtrEnable = true, // Some devices require this setting
RtsEnable = true, // Some devices require this setting
Encoding = System.Text.Encoding.ASCII
};
serialPort.DataReceived += SerialPort_DataReceived;
serialPort.Open(); // Open the serial port
// Send AT commands to request SIM card and device information
serialPort.WriteLine("AT\r\n");
MessageBox.Show("Reading data from device...", "Device Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Device Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Event handler for receiving data
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string rawData = serialPort.ReadExisting(); // Read incoming data
// Display the response in the RichTextBox in the UI thread
this.Invoke((MethodInvoker)delegate
{
richTextBox1.AppendText(rawData + "\n"); // Append response to the RichTextBox
});
}
catch (Exception ex)
{
MessageBox.Show("Read Error: " + ex.Message, "Device Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
I am getting error
[L610_cldsd_load.c] -DR_RecvData_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_load.c] -DR_RecvData
_Analysis- Line=1292:<ERR>, the
iRet=-5006
[L610_cldsd_uart.c]
-DR_Uart0_Handle- Line=88:DR_Rec
vData_Analysis iRet=-5006
This error message seems to be coming from the device firmware. I tried various approaches to send AT commands, but the device does not respond as expected.
Troubleshooting steps I have tried:
- Checking the COM port connection.
- Changing the baud rate (tried 9600, 38400, and 115200).
- Sending different AT commands like “AT+GMR” for firmware version and “AT+CGSN” for IMEI.
- Ensuring the device is awake by sending a simple “AT” ping.
Question -
- How can I correctly retrieve the IMEI, Serial Number, and other device details from a device connected via USB COM port in C#?
- Are there any special configuration requirements for the device or the serial communication that I might be missing?
I would appreciate any help or suggestions. Thanks!