回覆列表
  • 1 # 使用者2458114238191884

    同一臺機只能有一個程式開啟串列埠,自發自收的串列埠資料,可以將串列埠的2、3針腳短接,然後再對串列埠寫資料,同時再讀串列埠即可,讀寫串列埠可參看MSDN,system.io.ports.serialport的C#程式碼

    publicstaticvoidMain()

    {

    stringname;

    stringmessage;

    StringComparerstringComparer=StringComparer.OrdinalIgnoreCase;

    ThreadreadThread=newThread(Read);

    //CreateanewSerialPortobjectwithdefaultsettings.

    _serialPort=newSerialPort();

    //Allowtheusertosettheappropriateproperties.

    _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);

    //Settheread/writetimeouts

    _serialPort.ReadTimeout=500;

    _serialPort.WriteTimeout=500;

    _serialPort.Open();

    _continue=true;

    readThread.Start();

    Console.Write("Name:");

    name=Console.ReadLine();

    Console.WriteLine("TypeQUITtoexit");

    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();

    }

    publicstaticvoidRead()

    {

    while(_continue)

    {

    try

    {

    stringmessage=_serialPort.ReadLine();

    Console.WriteLine(message);

    }

    catch(TimeoutException){}

    }

    }

  • 中秋節和大豐收的關聯?
  • 當女兵有什麼要求?