I try to read data from a serial port. I use the following putty configuration:
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();
}
}
}
}