短信收发类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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月18日 22时36分13秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Pytorch(十一) —— 分布式(多GPU)训练
2019-04-30
Deeplab v3
2019-04-30
NLP 之 Perplexity困惑度
2019-04-30
tensor/矩阵/图片等更换通道,调整size
2019-04-30
本地和colab 中 改变tensorflow的版本
2019-04-30
Camera-ready ddl
2019-04-30
CUB-200鸟类数据集
2019-04-30
Python反射机制
2019-04-30
YAPF —— Python代码格式化工具
2019-04-30
UGC 用户产生内容
2019-04-30
ranger
2019-04-30
slurm
2019-04-30
xfce4
2019-04-30
xrdp
2019-04-30
Raft算法
2019-04-30
Python计算文本BLEU分数
2019-04-30
swap内存(linux)
2019-04-30
人脸au
2019-04-30
torch.distributed 分布式
2019-04-30