Console does not show the entire answar from serial port

纵然是瞬间 提交于 2021-01-29 11:17:04

问题


I am working with BC66 modem from Quectel. On my program when I type some AT commands to the modem it can not show the entire response from the modem, but instead the reply of the AT command. see figur below:

Instead using an terminal program and type the AT commands i got the status of my request. See figure below:

This is what I am expecting in my program, but unfortunately not happened.

The code snip:

static void Main(string[] args)
        {
            if (InitPort() == true)
            {
                Console.WriteLine("Port initialized");
                TestAT();
            }
        }

private static void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     byte[] data = new byte[serialPort.BytesToRead];
     serialPort.Read(data, 0, data.Length);
     Console.WriteLine(Encoding.ASCII.GetString(data));
     //Console.WriteLine(serialPort.ReadExisting());
}

private static void TestAT()
{
     for (int i = 0; i < 10; i++)
     {
        serialPort.Write("at");
        Thread.Sleep(2000);
        serialPort.Write("at+cereg");
        Thread.Sleep(2000);
     }
}

private static bool InitPort()
        {
            serialPort.PortName = "COM51";
            serialPort.BaudRate = 115200;
            serialPort.DataBits = 8;
            serialPort.Parity = Parity.None;
            serialPort.StopBits = StopBits.One;
            serialPort.DataReceived += SerialPort_DataReceived;

            try
            {
                serialPort.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }

Any suggestion on what is wrong?


回答1:


As you can see here on page 7 you need to finish your commands with <CR><LF>.

The “AT” or “at” prefix must be set at the beginning of each command line. Entering <CR> will terminate a command line.

in C# the equivalent is \r\n

As @Mong Zhu pointed out, you can achieve that by using serialPort.WriteLine("at");



来源:https://stackoverflow.com/questions/57427052/console-does-not-show-the-entire-answar-from-serial-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!