带有串口的线程队列 [英] Thread Queue with serial port

查看:80
本文介绍了带有串口的线程队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个短信网关,我想要一个基于短信的命令到串口。



这是个想法:



1.我的客户发送短信给电脑

2.电脑读短信并根据短信,对串口发出自定义USSD请求

3.计算机将结果从串口发送回客户端



以下是问题:计算机可以及时接收到许多客户端和许多短信,但只有一个串口来处理请求。



我需要制作一个创建短信队列的应用程序。

因此,当收到很多短信时,如果串口忙,它将首先进入队列。如果没有,串口可以立即处理短信。



短信将由FIFO订单处理。



我知道这将与线程有关但我不知道什么是完美的方法。

请给一个提示人。谢谢。

Hi all,

I have a sms gateway and i want to make an sms based command to serial port.

This is the idea :

1. my clients send sms to computer
2. The computer read the sms and based on the sms, make an custom USSD request to serial port
3. The computer send back the result from serial port to the clients

Here is the problem : many clients and many sms can be received by the computer in time but only one serial port to handle the request.

I need to make an application that make a "sms queue".
So when there are many sms received, it will go into the queue first, if the serial port is busy. If not, the serial port can right away process the sms.

The sms will be processed by FIFO order.

I know this will got to do with thread but i have no idea what is the perfect approach.
Please give a hint guys. Thank you.

推荐答案

看看创建一个SMSMessage类并创建一个静态Queue<SMSMessage> [ ^ ]实例。

当您收到TX的消息时,您将创建一个SMSMessage的新实例(包括响应客户端所需的信息)并添加它到队列。然后,您有一个后台线程,它从队列中检索消息并处理它。然后它使用提供的信息直接响应,或者将数据传递给一个单独的线程来处理响应。



您可能需要提供一些锁定才能使队列线程安全 - 特别是如果多个线程将添加到它。
Look at creating a SMSMessage class and creating a single, static Queue<SMSMessage>[^] instance.
When you receive a message for TX, you create a new instance of an SMSMessage (including the info you need to respond to the client) and add it to the queue. You then have a background thread which retrieves a message from the queue and deals with it. It then used the info supplied to either respond directly, or pass the data to a separate thread to handle responding.

You will probably need to provide some locking to make the Queue thread safe - particularly if more than one thread will be adding to it.


这篇关于带有串口的线程队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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