SerialPort Read()返回使用Write()写入的内容 [英] SerialPort Read() Returns What Was Written Using Write()

查看:908
本文介绍了SerialPort Read()返回使用Write()写入的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MSDN上列出的SerialPort.Write()示例( http://msdn.microsoft.com /en-us/library/y2sxhat8.aspx ),每当调用Read()时,它仅返回最初写入设备的内容. (即,我发送"AT",它响应"AT",等等.)使用终端程序(例如PuTTY),设备功能正常,返回应有的状态.知道为什么会这样吗?
使用的代码:

Using the SerialPort.Write() example listed on MSDN (http://msdn.microsoft.com/en-us/library/y2sxhat8.aspx), whenever Read() is called, it only returns whatever was originally written to the device. (ie. I send "AT", it responds "AT", etc.) Using a terminal program, such as PuTTY, the device functions fine, returning what it should. Any idea why this would be happening?
Code used:

public static void Main()
{
    string name;
    string message;
    StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
    Thread readThread = new Thread(Read);
    // Create a new SerialPort object with default settings.
    _serialPort = new SerialPort();
    // Allow the user to set the appropriate properties.
    _serialPort.PortName = SetPortName(_serialPort.PortName);
    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
    // Set the read/write timeouts
    _serialPort.ReadTimeout = 500;
    _serialPort.WriteTimeout = 500;
    _serialPort.Open();
    _continue = true;
    readThread.Start();
    Console.Write("Name: ");
    name = Console.ReadLine();
    Console.WriteLine("Type QUIT to exit");
    while (_continue)
    {
        message = Console.ReadLine();
        if (stringComparer.Equals("quit", message))
        {
            _continue = false;
        }
        else
        {
            _serialPort.WriteLine(
                String.Format("<{0}>: {1}", name, message) );
        }
    }
    readThread.Join();
    _serialPort.Close();
}
public static void Read()
{
    while (_continue)
    {
        try
        {
            string message = _serialPort.ReadLine();
            Console.WriteLine(message);
        }
        catch (TimeoutException) { }
    }
}

推荐答案

AFAIK GSM调制解调器正在镜像发送的内容. Putty会将字符代码发送到设备,并且不会在屏幕上打印(除非您也强行将其打印).设备会反映您在终端中输入的内容作为反馈.

因此,在您的程序中,您会看到一个来自键盘的响应和一个来自设备的响应.

这只是一个提示-我没有仔细检查您的代码:)
AFAIK GSM modems are mirroring what was sent. Putty sends characters codes to the device and doesn''t print it on the screen (unless you force it too). Device mirrors what have you typed in in the terminal as a feedback.

So in your program you see one response from keyboard and one from the device.

This is only a clue - I didn''t examine your code carefully :)


这篇关于SerialPort Read()返回使用Write()写入的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆