VS 串口通信SerialPort,官方demo

轉自:https://blog.csdn.net/feixiangsmile/article/details/78210798 
 

網上有很多解決方案:

主流大概三種:

1、使用mscomm控件,是微軟以前提供的控件,現在已經放棄了。當然在vs中可以手動集成這個控件,也能使用,但是需要註冊。

2、自己編程實現串口通信(這個以後有時間寫個博客)

3、使用.net framework 中的SerialPort類。api地址爲:

https://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport(v=vs.110).aspx

本文介紹這個裏面demo導入vs控制檯應用後的錯誤修復問題。

在上面的那個網址中,官方的C++代碼爲:

#using <System.dll>
 
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
 
public ref class PortChat
{
private:
    static bool _continue;
    static SerialPort^ _serialPort;
 
public:
    static void Main()
    {
        String^ name;
        String^ message;
        StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase;
        Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read));
 
        // Create a new SerialPort object with default settings.
        _serialPort = gcnew 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();
    }
 
    static void Read()
    {
        while (_continue)
        {
            try
            {
                String^ message = _serialPort->ReadLine();
                Console::WriteLine(message);
            }
            catch (TimeoutException ^) { }
        }
    }
 
    static String^ SetPortName(String^ defaultPortName)
    {
        String^ portName;
 
        Console::WriteLine("Available Ports:");
        for each (String^ s in SerialPort::GetPortNames())
        {
            Console::WriteLine("   {0}", s);
        }
 
        Console::Write("Enter COM port value (Default: {0}): ", defaultPortName);
        portName = Console::ReadLine();
 
        if (portName == "")
        {
            portName = defaultPortName;
        }
        return portName;
    }
 
    static Int32 SetPortBaudRate(Int32 defaultPortBaudRate)
    {
        String^ baudRate;
 
        Console::Write("Baud Rate(default:{0}): ", defaultPortBaudRate);
        baudRate = Console::ReadLine();
 
        if (baudRate == "")
        {
            baudRate = defaultPortBaudRate.ToString();
        }
 
        return Int32::Parse(baudRate);
    }
 
    static Parity SetPortParity(Parity defaultPortParity)
    {
        String^ parity;
 
        Console::WriteLine("Available Parity options:");
        for each (String^ s in Enum::GetNames(Parity::typeid))
        {
            Console::WriteLine("   {0}", s);
        }
 
        Console::Write("Enter Parity value (Default: {0}):", defaultPortParity.ToString());
        parity = Console::ReadLine();
 
        if (parity == "")
        {
            parity = defaultPortParity.ToString();
        }
 
        return (Parity)Enum::Parse(Parity::typeid, parity);
    }
 
    static Int32 SetPortDataBits(Int32 defaultPortDataBits)
    {
        String^ dataBits;
 
        Console::Write("Enter DataBits value (Default: {0}): ", defaultPortDataBits);
        dataBits = Console::ReadLine();
 
        if (dataBits == "")
        {
            dataBits = defaultPortDataBits.ToString();
        }
 
        return Int32::Parse(dataBits);
    }
 
    static StopBits SetPortStopBits(StopBits defaultPortStopBits)
    {
        String^ stopBits;
 
        Console::WriteLine("Available Stop Bits options:");
        for each (String^ s in Enum::GetNames(StopBits::typeid))
        {
            Console::WriteLine("   {0}", s);
        }
 
        Console::Write("Enter StopBits value (None is not supported and \n" +
            "raises an ArgumentOutOfRangeException. \n (Default: {0}):", defaultPortStopBits.ToString());
        stopBits = Console::ReadLine();
 
        if (stopBits == "")
        {
            stopBits = defaultPortStopBits.ToString();
        }
 
        return (StopBits)Enum::Parse(StopBits::typeid, stopBits);
    }
 
    static Handshake SetPortHandshake(Handshake defaultPortHandshake)
    {
        String^ handshake;
 
        Console::WriteLine("Available Handshake options:");
        for each (String^ s in Enum::GetNames(Handshake::typeid))
        {
            Console::WriteLine("   {0}", s);
        }
 
        Console::Write("Enter Handshake value (Default: {0}):", defaultPortHandshake.ToString());
        handshake = Console::ReadLine();
 
        if (handshake == "")
        {
            handshake = defaultPortHandshake.ToString();
        }
 
        return (Handshake)Enum::Parse(Handshake::typeid, handshake);
    }
};
 
int main()
{
    PortChat::Main();
}


在vs(我使用的是vs2012)建立控制檯應用後,把上述代碼複製到項目中,會提示有錯誤:

解決方案:

在項目上右擊,點擊屬性中公共語言運行時支持下拉菜單中選擇公共語言運行時支持(/clr),如下圖

然後就可以運行了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章