控制台不显示来自串口的整个 answar [英] Console does not show the entire answar from serial port
问题描述
我正在使用移远通信的 BC66 调制解调器.在我的程序中,当我向调制解调器键入一些 AT 命令时,它无法显示来自调制解调器的整个响应,而是显示 AT 命令的回复.见下图:
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:
而是使用终端程序并键入 AT 命令,我获得了请求的状态.见下图:
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.
代码截图:
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;
}
}
对错误有什么建议吗?
推荐答案
如您所见 此处 在第 7 页上,您需要使用 <CR><LF>
来完成您的命令.
As you can see here on page 7 you need to finish your commands with <CR><LF>
.
必须在每个命令的开头设置AT"或at"前缀线.输入
将终止命令行.
The "AT" or "at" prefix must be set at the beginning of each command line. Entering
<CR>
will terminate a command line.
在 C# 中等价物是 \r\n
in C# the equivalent is \r\n
正如@Mong Zhu 指出的那样,您可以通过使用serialPort.WriteLine("at");
As @Mong Zhu pointed out, you can achieve that by using
serialPort.WriteLine("at");
这篇关于控制台不显示来自串口的整个 answar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!