Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Howdi,

here is short summary what i want to do with my programm:
- i want to receive data over com1 (rs232) in a seperate thread
- i dont want to use the datareceived event (from the serialport class)
- a thread should read the comport and send the data to a pipeserverThread via Pipe
- Pipeserverthread should write the comdata in a richtextbox as Hex

Here are my problems:
1. I want to read the comport receive buffer bit by bit and send those data to the pipe until the buffer is empty

2. I want to read the pipe bit by bit and have an indicator that at this point the combuffer was empty :/ (absolutly no clue how to do it)

I also have some general questions:
is it possible to crate a pipe to the datareceiveevent of the serialport?
is it possible to loose data when the datareceiveevent occours and i spend too much time in the eventhandle funktion?

Hope you have some imput for me.
Posted

1 solution

This is all possible. For the port, use the class System.IO.Ports.SerialPort, see:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^].

In your case, you should do all the port I/O operation is a separate thread and just read from it. The call is blocking: you thread will be in a wait state (using zero CPU time) until data arrives and wake it up. Of course you can also always check up if the buffer is empty or not, using the property System.IO.Ports.SerialPort.BytesToRead.

However, your requirement "until the buffer is empty" is not really correct. More exactly, it all depends on what happens on the other end of RS-232 cable. This moment the buffer is empty, but in a minute, that end starts to transmit data again. In general case, your code should listen for the data all the time, during full run time of the application. I think you did understand it.

For pipes, see this CodeProject article:
Inter-Process Communication in .NET Using Named Pipes, Part 1[^].

You can also use pipes indirectly, through the IPC channel in classical remoting or WCF. Such channels use named pipes for the implementation in a way transparent to the other aspects of the communication.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900