Brett
Brett

Reputation: 3

Need to run Socket Connection & Serial Port Connection in parallel

I have a Client-Server system in C# where I am constantly receiving data from the Client through the socket. I receive a set of 8 numbers from the Client to the Server, and then this data repeats again. Once I receive one set of data, I must send them all through a Serial Port, and once completed should work on the next set of data. The send-receive to Serial Port takes a lot of time, and in the meantime, the Socket Connection has already sent lots of data. The strange thing is, after the loop completes the first time, the program hangs, and does nothing, by which I mean that the program is not triggered anymore by the Socket Connection's incoming data. Could you please tell me why this could be happening? If I need to run the Socket Connection & Serial Port on two separate threads, how could I do that?

Thank You.

Upvotes: 0

Views: 987

Answers (1)

Tudor
Tudor

Reputation: 62439

You could implement a producer-consumer pattern in which one thread (the producer) receives data from the socket and puts it in a queue data structure, while another thread (the consumer) fetches data from the queue and writes sends it to the serial port.

Upvotes: 1

Related Questions