无法从Serialport类的ReadExisting()中读取数据 [英] Unable to Read Data from ReadExisting() of Serialport Class
问题描述
大家好,
我正在做一个项目,在这个项目中,我必须从通过USB调制解调器连接到PC的GSM手机发送短信.我正在使用serialport类从设备发送或接收数据.
但是,当我通过我的应用程序发送短信时.有时它发送有时不发送.在使用WriteLine()方法将AT命令写入串行端口后,当我尝试使用serialport.ReadExisting()方法获取所有现有数据时,则有时它会向我显示我在WriteLine()方法中传递的所有AT命令.它包含NULL.我不知道原因.可能是缓冲问题.
如果有人对此有任何想法,请帮助我.
谢谢
Hi All,
I am working on a project in which i have to send sms from a GSM phone connected to my PC via USB modem. I am using serialport class to send or recieve data from the device.
But when i send a sms via my application. sometimes it sends sometimes not. Means after writing the AT commands to serial port using WriteLine() method, when i try to get all existing data using method serialport.ReadExisting(), then sometimes it shows me all AT commands those i passed within WriteLine() method, but sometimes it contains NULL. I do not know the reason. perheps may be buffer problem.
Please help me out, if someone have any idea about that.
thanks
推荐答案
如果您使用的代码如下:
If you are using code like:
mySerialPort.WriteLine(myCommands);
string response = mySerialPort.ReadExsisting();
很可能是移动设备还没有时间做出完全反应-例如,它可能正在忙于与移动网络进行通话.
相反,请尝试(仅出于测试目的)在两个指令之间放置一个Thread.Sleep(500);
:您可能会获得更可靠的结果.
It may well be that the mobile has not had time to fully react yet - it may be busy talking to the mobile network for example.
Instead, try (just for testing) putting a Thread.Sleep(500);
between the two instructions: you may well get more reliable results.
这篇关于无法从Serialport类的ReadExisting()中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!