The documentation for the device should also specify these settings. 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. I highly recommend you attempt to communicate with the device using some other means (I use hyperterminal frequently).
#Serial port datareceived event serial#
I'm beginning to suspect that either your serial port is not configured properly, or that the device is not responding. Given all of the traffic on this question recently. SerialPort sp2 = new SerialPort("COM34", 115200, Parity.None, 8, StopBits.One) SerialPort sp = new SerialPort("COM33", 115200, Parity.None, 8, StopBits.One) Thread writeThread = new Thread(new ThreadStart(WriteThread))
#Serial port datareceived event code#
I just ran the following code successfully on my machine (using a null modem cable between COM33 and COM34) namespace TestApp Next, you really should remove this code: // Wait 10 Seconds for data.Ĭonsole.WriteLine(sp.Read(buf,0,bufSize)) //prints data directly to the ConsoleĪnd instead just loop until the user presses a key or something, like so: namespace serialPortCollectionĪlso, note the revisions to the data received event handler, it should actually print the buffer now. Private static void port_OnReceiveDatazz(object sender,Ĭonsole.WriteLine(spL.Read(buf, 0, bufSize)) įirst off I recommend you use the following constructor instead of the one you currently use: new SerialPort("COM10", 115200, Parity.None, 8, StopBits.One) Sp.WriteLine("!") //Stop Data Stream Command Wait for data or user input to continue. Sp.WriteLine("$") //Command to start Data Stream Sp.DataReceived += port_OnReceiveDatazz // Add DataReceived Event Handler SerialPort sp = new SerialPort("COM10", 115200) I can live with just reading the port in a thread and streaming the data straight to file.īyte buf = new Byte //To store the received data. Perhaps it has something to do with the device. I am stumped and can't seem to get the event handler to work. Thanks to Down, and for all the suggestions. Can someone please help enlighten me as to why the event handler port_OnReceiveDatazz is not being called even though I know that there is data being sent to the port by the hardware? I rewrote the console application so that all the code is in Main and is pasted below.
After reading the Top 5 SerialPort Tips, I still can't seem to get this to work and don't know what I am missing. I would like to convert this application to use the dataReceived event to stream instead. I have successfully created a small C# console object and application that opens the port and streams data to a file for a fixed period of time using a for-loop. Private Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)ĭim rcv As String = _serialport.ReadExisting()Ĭonsole.WriteLine(buffer.Substring(0, x).I am attempting to create a small application to collect data received from an external sensor attached to COM10. DataReceived, AddressOf DataReceivedHandler Is the readline method just handling the buffer for me? Does the data still come in chunks regardless of the method used?ĭim message As String = _serialPort.ReadLine()Ĭatch generatedExceptionName As TimeoutExceptionĪddHandler. Using this method, however, has the NewLine variable to set the line ending character for the port. If I use SerialPort.ReadLine() method, the data comes in 1 line sentences.
The problem is the data is coming in chunks and not 1 line sentences. In VB.NET, what is the difference between using the SerialPort.ReadLine() method versus using the DataReceived event handler? Currently, I'm using the data received event handler and detecting the line endings.