2

I try to read data from a serial port. I use the following putty configuration: enter image description here

And i get this output: enter image description here

Currently i do not care about the details of the output, it's a magic low-level protocol. Every 2 seconds I receive some bytes.

On the other side: If a use a C#-program, I do not get any ouptput:

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ComTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort sp = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One);
            //sp.Handshake = Handshake.None;
            sp.Handshake = Handshake.XOnXOff;
            sp.Open();
            sp.DataReceived += (s, e) => Console.WriteLine("Data received.");
            sp.ErrorReceived += (s, e) => Console.WriteLine("Data received.");
            try
            {
                byte[] buffer = new byte[2];
                while (true)
                {
                    string buf = sp.ReadExisting();
                    if (buf.Length > 0)
                    {
                        Console.WriteLine(buf.Length);
                        Console.Write(buf);
                    }
                    Thread.Sleep(20);
                }
            }
            finally
            {
                sp.Close();
            }
        }
    }
}

I also tried to use Java, but there was also no output visible. What could be the difference? I played around with the parameters, but i never get any output (in C# and also Java).

Thank you very much

-edit-

I just played around with a virtual linux. The C# code just does nothing like on windows, but a bash-script works. It changes the serial-port configuration to:

XXX@ubuntu:~/tmp/vol$ sudo stty -F /dev/ttyUSB0 -a
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 5;
-parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke
XXX@ubuntu:~/tmp/vol$ 

Unfortunately i dont know how i have to change the C# code to get the same configuration.

-edit-

Hans Passant solved my problem (see comments): It is just required to set CtrEnable=true and RtsEnable=true.

Working code:

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ComTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort sp = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
            //sp.Handshake = Handshake.None;
            sp.Handshake = Handshake.RequestToSend;
            sp.RtsEnable = true;
            sp.DtrEnable = true;
            sp.Open();
            sp.DataReceived += (s, e) => Console.WriteLine("Data received.");
            sp.ErrorReceived += (s, e) => Console.WriteLine("Data received.");
            try
            {
                byte[] buffer = new byte[2];
                while (true)
                {
                    string buf = sp.ReadExisting();
                    if (buf.Length > 0)
                    {
                        Console.WriteLine(buf.Length);
                        Console.Write(buf);
                    }
                    Thread.Sleep(20);
                }
            }
            finally
            {
                sp.Close();
            }
        }
    }
}
Kevin Meier
  • 2,339
  • 3
  • 25
  • 52
  • What happens if you set breakpoint in the `Console.WriteLine(...)` line? – Dmitry Nov 07 '16 at 19:45
  • Are you getting an error or is it just doing nothing? – Brandon Nov 07 '16 at 19:45
  • @Dmitry: It never reaches the breakpoint, because `buf.Length` is always equal to 0. @Brandon: It just loops forever, there is no error. – Kevin Meier Nov 07 '16 at 19:46
  • Do you know if flow control is actually enabled on the other side? Did you try Handshake = Handshake.None instead of Xon/Xoff? – Levesque Nov 07 '16 at 19:47
  • @Levesque: I also tried Handshake.None, but it had the same effect. – Kevin Meier Nov 07 '16 at 19:48
  • 2
    Have you tried to subscribe to the [`SerialPort.DataReceived`](https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx) event? – Dmitry Nov 07 '16 at 19:50
  • @Dmitry: I tried it just now (see the code changes), but the event was not fired. – Kevin Meier Nov 07 '16 at 19:52
  • 1
    Have you tried the c# example on msdn? https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx should give you an idea whether theres a code problem or something more fundamental! – Chris Watts Nov 07 '16 at 19:56
  • @ChrisWatts: I tried it, but it does not seem to work. Unfortunately i do not know what else it could be. I tried every USB port (USB 2.0 and also USB 3.0), but the effect was always the same. – Kevin Meier Nov 07 '16 at 20:02
  • I don't believe you can directly read from a USB port this way, not to the best of my knowledge at least. Your example is using old school serial port tech (i.e. a 9 pin serial port). You could check this out for accessing USB (I've never tried it): https://msdn.microsoft.com/en-ca/library/windows/hardware/ff540174(v=vs.85).aspx.r there are adapters available if you really want to interface with a serial device over USB: http://www.staples.com/Staples-USB-to-Serial-Adapter/product_837560 – Levesque Nov 07 '16 at 20:22
  • @Levesque: I already use such an adapter. Sorry for the bad description. – Kevin Meier Nov 07 '16 at 21:52
  • 4
    When you use Handshake.None or Handshake.XonXoff then the SerialPort class assumes that it should not use the handshake signals. Which is *not* often correct, almost any serial device relies on them. Set the DtrEnable and RtsEnable properties to *true* in your code and it is likely that it will now work. Which then gives you a very strong hint that it actually should be Handshake.RequestToSend, the 98% correct choice unless you use a wonky low-cost device like an Arduino. – Hans Passant Nov 07 '16 at 23:21
  • @HansPassant: Thank you very much! After set DtrEnable and RtsEnable to true it just worked:)! – Kevin Meier Nov 08 '16 at 17:59

0 Answers0