短信收发类SerialStream.cs的用例--SerialStreamReader.cs
发布日期:2021-06-30 19:06:59 浏览次数:2 分类:技术文章

本文共 1352 字,大约阅读时间需要 4 分钟。

using System;

using System.IO;
using System.Threading;

using LoMaN.IO;

namespace SerialStreamReader {

 class App {

  // The main serial stream

  static SerialStream ss;

  [STAThread]

  static void Main(string[] args) {

   // Create a serial port

   ss = new SerialStream();
   try {
    ss.Open("COM2");
   }
   catch (Exception e) {
    Console.WriteLine("Error: " + e.Message);
    return;
   }

   // Set port settings

   ss.SetPortSettings(9600);

   // Set timeout so read ends after 20ms of silence after a response

   ss.SetTimeouts(20, 0, 0, 0, 0);

   // Create the StreamWriter used to send commands

   StreamWriter sw = new StreamWriter(ss, System.Text.Encoding.ASCII);

   // Create the Thread used to read responses

   Thread responseReaderThread = new Thread(new ThreadStart(ReadResponseThread));
   responseReaderThread.Start();

   // Read all returned lines

   for (;;) {
    // Read command from console
    string command = Console.ReadLine();

    // Check for exit command

    if (command.Trim().ToLower() == "exit") {
     responseReaderThread.Abort();
     break;
    }

    // Write command to modem

    sw.WriteLine(command);
    sw.Flush();
   }
  }

  // Main loop for reading responses

  static void ReadResponseThread() {
   StreamReader sr = new StreamReader(ss, System.Text.Encoding.ASCII);
   try {
    for (;;) {
     // Read response from modem
     string response = sr.ReadLine();
     Console.WriteLine("Response: " + response);
    }
   }
   catch (ThreadAbortException) {
   }
  }
 }
}
 

转载地址:https://linuxstyle.blog.csdn.net/article/details/1536680 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:短信猫GSM Modem
下一篇:短信收发类无错版SerialStream.cs

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月18日 22时36分13秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章