1

I'm new to serial communication and was trying to write a simple piece of code that writes and reads from a COM port. I have a wpf window that triggers the Connect and Disconnect functions, and when I start Connect the reader thread keeps timing out and doesn't get any data from the writer thread. Could anyone help me point out what's missing?

   public void Connect()
    {
        port = new SerialPort("COM1", 115200);
        port.ReadTimeout = 500;
        port.WriteTimeout = 500;
        port.Handshake = Handshake.None;
        port.Open();

        readThread = new Thread(Read);
        readRunning = true;
        readThread.Start();

        writeThread = new Thread(Write);
        writeRunning = true;
        writeThread.Start();

        System.Diagnostics.Debug.Print("connected");
    }

    public void Disconnect()
    {
        if (!readRunning && !writeRunning)
        {
            port.Close();
        }
        else
        {
            readRunning = false;
            writeRunning = false;
            readThread.Join();
            writeThread.Join();
            port.Close();
        }
        System.Diagnostics.Debug.Print("disconnected");
    }

    public void Read()
    {
        while (readRunning)
        {
            if (port.IsOpen)
            {
                try
                {
                    int byteData = port.ReadByte();
                    System.Diagnostics.Debug.Print("message: " + byteData.ToString());
                }
                catch (TimeoutException)
                {
                }
            }
        }
    }

    public void Write()
    {
        byte[] byteData = {1,2,3};
        while (writeRunning)
        {
            if (port.IsOpen)
            {
                try
                {
                    port.Write(byteData, 0, 3);
                }
                catch (TimeoutException)
                {
                }
            }
        }
    }
nemesis
  • 253
  • 1
  • 4
  • 11

1 Answers1

-1

You need to flush the buffer after write. Like this

try
{
    Stream s = port.BaseStream;
    s.Write(byteData, 0, 3);
    s.Flush();
    // etc.

You want to read your written bytes back. Are you using a loopback connector?

Sjips
  • 1,248
  • 2
  • 11
  • 22
  • no i'm just trying to test that the read and write actually work, without having something physical pluged in. I tried using the flush but it doesn't seem to have changed anything. – nemesis Sep 23 '14 at 20:06
  • To get data returned, you have to use a loopback connector. If you don't have one, you can make it yourself. Short-circuit pin 2 and pin 3 of the serial port with a paperclip, screwdriver, whatever is conducting. Do not touch other pins or the shielding. Don't be afraid of damaging the serial port by making short-circuit; you will only loose your bits and bytes. – Sjips Sep 23 '14 at 20:14
  • If you just want to test reading and writing threads, consider named pipes: `NamedPipeServerStream` for your read thread and `NamedPipeClientStream` for your write thread. They implement `Stream` interface, and you write and read bytes in the same way as from a serial port. In this way, you don't need to do something with the serial port. NamedPipeStreams are very fast and work also between processes, not just threads. – Sjips Sep 23 '14 at 20:20
  • I see your point. Got around the issue with a virtual port though with com0com. Thanks a lot for the help! – nemesis Sep 23 '14 at 21:27