none
TCP/UDP, NetworkStream and Serial Ports RRS feed

  • Question

  • Hello, I have a device that is currently using a serial port (USB) as its main form of communication between another piece of hardware. On the C# side of the code, it is using serial port read and serial port write in order to retrieve and send data asynchronously. My dilemma is would it be possible, knowing that the piece of hardware supports TCP/IP and UDP, to fix my C# without making major changes, maybe adding a class or something, making the changes from SerialPort read write, to network stream read/write or tcpclient read write.

    Thank you.

    Monday, April 7, 2014 10:08 PM

Answers

  • Yes, Yes, Yes.

    There should be very little differences between and serial port and TCP in reading and writing data.  The only differences is opening and closing the port/socket/client.  Both TCP and serial port classes has the same read and write methods.

    The async read methods for both the tcp and serialport have the same parameter list.  So the code is identical as long as you don't do any writing inside the async read.  You can actually do the writes if you pass an generic class object contain the write object (either serial port or client).  The BeginAccept can then pass an class object containing a TCP or SerialPort object and the lower code is identical. 


    jdweng

    Tuesday, April 8, 2014 1:56 AM