Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off I recommend you use the following constructor instead of the one you currently use:</p> <pre><code>new SerialPort("COM10", 115200, Parity.None, 8, StopBits.One); </code></pre> <p>Next, you really should remove this code:</p> <pre><code>// Wait 10 Seconds for data... for (int i = 0; i &lt; 1000; i++) { Thread.Sleep(10); Console.WriteLine(sp.Read(buf,0,bufSize)); //prints data directly to the Console } </code></pre> <p>And instead just loop until the user presses a key or something, like so:</p> <pre><code>namespace serialPortCollection { class Program { static void Main(string[] args) { SerialPort sp = new SerialPort("COM10", 115200); sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler sp.Open(); sp.WriteLine("$"); //Command to start Data Stream Console.ReadLine(); sp.WriteLine("!"); //Stop Data Stream Command sp.Close(); } // My Event Handler Method private static void port_OnReceiveDatazz(object sender, SerialDataReceivedEventArgs e) { SerialPort spL = (SerialPort) sender; byte[] buf = new byte[spL.BytesToRead]; Console.WriteLine("DATA RECEIVED!"); spL.Read(buf, 0, buf.Length); foreach (Byte b in buf) { Console.Write(b.ToString()); } Console.WriteLine(); } } } </code></pre> <p>Also, note the revisions to the data received event handler, it should actually print the buffer now.</p> <p><strong>UPDATE 1</strong></p> <hr> <p>I just ran the following code successfully on my machine (using a null modem cable between COM33 and COM34)</p> <pre><code>namespace TestApp { class Program { static void Main(string[] args) { Thread writeThread = new Thread(new ThreadStart(WriteThread)); SerialPort sp = new SerialPort("COM33", 115200, Parity.None, 8, StopBits.One); sp.DataReceived += port_OnReceiveDatazz; // Add DataReceived Event Handler sp.Open(); sp.WriteLine("$"); //Command to start Data Stream writeThread.Start(); Console.ReadLine(); sp.WriteLine("!"); //Stop Data Stream Command sp.Close(); } private static void port_OnReceiveDatazz(object sender, SerialDataReceivedEventArgs e) { SerialPort spL = (SerialPort) sender; byte[] buf = new byte[spL.BytesToRead]; Console.WriteLine("DATA RECEIVED!"); spL.Read(buf, 0, buf.Length); foreach (Byte b in buf) { Console.Write(b.ToString() + " "); } Console.WriteLine(); } private static void WriteThread() { SerialPort sp2 = new SerialPort("COM34", 115200, Parity.None, 8, StopBits.One); sp2.Open(); byte[] buf = new byte[100]; for (byte i = 0; i &lt; 100; i++) { buf[i] = i; } sp2.Write(buf, 0, buf.Length); sp2.Close(); } } } </code></pre> <p><strong>UPDATE 2</strong></p> <hr> <p>Given all of the traffic on this question recently. I'm beginning to suspect that either your serial port is not configured properly, or that the device is not responding.</p> <p>I highly recommend you attempt to communicate with the device using some other means (I use hyperterminal frequently). You can then play around with all of these settings (bitrate, parity, data bits, stop bits, flow control) until you find the set that works. The documentation for the device should also specify these settings. Once I figured those out, I would make sure my .NET SerialPort is configured properly to use those settings.</p> <p>Some tips on configuring the serial port:</p> <p>Note that when I said you should use the following constructor, I meant that use that function, not necessarily those parameters! You should fill in the parameters for your device, the settings below are common, but may be different for your device.</p> <pre><code>new SerialPort("COM10", 115200, Parity.None, 8, StopBits.One); </code></pre> <p>It is also important that you setup the .NET SerialPort to use the same flow control as your device (as other people have stated earlier). You can find more info here:</p> <p><a href="http://www.lammertbies.nl/comm/info/RS-232_flow_control.html" rel="noreferrer">http://www.lammertbies.nl/comm/info/RS-232_flow_control.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload