2

Currently im working on an app that receives data from RFID card reader throu serial port. The data, afterwards the data should be sent by TCP/IP to a server.

I already have both thing working, but not at the same time. I can read ID from card, and i can also send some text to server. But i would like to ask you for suggestion, what would be the best solution for those two things working together.

At the moment i have TCP/IP usage and Serial usage in two classes. From main class im creating objects of both classes and starting a thread that reads data from serial port.

Should i try something like this?

In class i would create only serial usage object and call a thread that handles reading. Inside of that reading thread, i would call a new thread that would open TCP/IP connection and sent data that it has recieved from serial port.

Another somehow simlar approach would open both connections from main class, but it would start TCP/IP sending thread (throu already open connection) from serial port recieveing thread.

I dont have too much experience with threading, so thats why i ask for your help fellow software developers. Thanks! (if that would help, i could paste some parts of code here).

UPDATED:

Thats for the tip, i'll look closer into that what you suggested. I guess that is good idea, but as i said i dont have too much experience with threading. Would it work, if i would create separate threads in those classes, and they would both use same object, lets say object of class Message, i would create new object in a COM handling thread, and TCP/IP handling thread would use that object to send the data from it throu TCP/IP? Can i make a queue of objects of class Message just like i can do with List? Also, how do i pass that queue of objects created in one thread in class toanotherthread

4

1 回答 1

3

将两种不同的通信方法抽象为单独的类是很好的设计,所以我会坚持下去。

与其在主类中处理线程,不如让每个通信类处理自己的线程管理并通过共享线程安全队列在它们之间进行通信。

然后,您的串行通信对象可以处理与串行端口的通信、读取数据并将该数据放入队列中。TCP comms 对象可以处理连接、重新连接、断开连接等......并使用队列中的数据并将其发送到套接字并在网络上发送出去。

您的主类可以只创建队列和传递同一队列实例的两个通信对象。

于 2013-10-02T14:25:37.730 回答