Serial received events can be caused by any of the items in the enumeration. Because the operating system determines whether to raise this event or not, not all parity errors may be reported., DataReceived, and events may be called out of order, and there may be a slight delay between when the underlying stream reports the error and when the event handler is executed. Only one event handler can execute at a time. The DataReceived event is not guaranteed to be raised for every byte received. Use the property to determine how much data is left to be read in the buffer.
The DataReceived event is raised on a secondary thread when data is received from the object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception.
If it is necessary to modify elements in the main or, post change requests back using, which will do the work on the proper thread. For more information about handling events, see. Imports System Imports System.IO.Ports Class PortDataReceived Public Shared Sub Main Dim mySerialPort As New SerialPort( 'COM1') mySerialPort.BaudRate = 9600 mySerialPort.Parity = Parity.None mySerialPort.StopBits = StopBits.One mySerialPort.DataBits = 8 mySerialPort.Handshake = Handshake.None AddHandler mySerialPort.DataReceived, AddressOf DataReceviedHandler mySerialPort.Open Console.WriteLine( 'Press any key to continue.' ) Console.WriteLine Console.ReadKey mySerialPort.Close End Sub Private Shared Sub DataReceviedHandler(sender As Objecte As SerialDataReceivedEventArgs) Dim sp As SerialPort = CType(sender, SerialPort) Dim indata As String = sp.ReadExisting Console.WriteLine( 'Data Received:') Console.Write(indata) End Sub End Class.
I was perhaps too quick to think that VB's built-in event would be be easy to use. As you can see from my previous post I am building a program which sends and receives a few bytes from serial bus. The bus is actually a usb serial adapter, there are no real serial buses in my machine. Anyway that SerialPort1.DataReceived does not fire.
It is from visual studio/vb ide so syntax should work? Here is my start up code. There are many comments, because I tried various combinations. These settings do not help. If Not serialPort.IsOpen Then If Not arvo = ' Then serialPort.DtrEnable = False serialPort.Parity = Parity.None serialPort.BaudRate = '9600' serialPort.PortName = arvo serialPort.Handshake = Handshake.None ' SerialPort1.Handshake = IO.Ports.Handshake.None serialPort.RtsEnable = False serialPort.Encoding = System.Text.Encoding.Default serialPort.StopBits = StopBits.One ' serialPort.ReadTimeout = 1000 ' serialPort.WriteTimeout = 500 serialPort.Open What is missing? Especially as there are no hand shaking with USB adapters I am using. Hi, it does look to me like you have placed one serialport component on your form, but use another one in your code.
If you want to receive the events for your serialPort then you should AddHandler to it. Your event like it is now just Handles SerialPort1.DataReceived. Add a Handler to it like: AddHandler serialPort.DataReceived, AddressOf SerialPort1DataReceived and remove the Handles SerialPort1.DataReceived from this method. Spanish fly crimes of agony. And then you also need to call RemoveHandler before you are closing your application. If you have got questions about this, just ask. Mark the thread as answered if the answer helps you. This helps others who have the same problem!
![]()
C# to VB.NET. Heslacher is right, you have one port that you created in code, but the event handler is watching the events from a completely different object. It's like any other control-you don't put one on the form, and then instantiate a new one in your code and expect them to be the same object.
You need to decide on whether you want a Serialport on your form at design time, and then attach to its events, or if you want to instantiate a serial port completely in code, and then add event handlers to it. The simplest way is the first one. Don't declare a new serial port in your code, just change the settings and etc on the one on your form. I would create them in code in a case where I might have several at once, and they will change-say being added or removed from the computer-and I want to catch that and add/remove them in my program on the fly.
![]()
Otherwise, if the program will be used on a computer with a fixed COM port, I probably would add a fixed Serialport control to my form. The code shortened. I left off error handling and so on. Hi, it does look to me like you have placed one serialport component on your form, but use another one in your code.
If you want to receive the events for your serialPort then you should AddHandler to it. Your event like it is now just Handles SerialPort1.DataReceived.
Add a Handler to it like: AddHandler serialPort.DataReceived, AddressOf SerialPort1DataReceived and remove the Handles SerialPort1.DataReceived from this method. And then you also need to call RemoveHandler before you are closing your application.
If you have got questions about this, just ask. Mark the thread as answered if the answer helps you. This helps others who have the same problem! C# to VB.NET.
Heslacher is right, you have one port that you created in code, but the event handler is watching the events from a completely different object. It's like any other control-you don't put one on the form, and then instantiate a new one in your code and expect them to be the same object. You need to decide on whether you want a Serialport on your form at design time, and then attach to its events, or if you want to instantiate a serial port completely in code, and then add event handlers to it. The simplest way is the first one. Don't declare a new serial port in your code, just change the settings and etc on the one on your form. I would create them in code in a case where I might have several at once, and they will change-say being added or removed from the computer-and I want to catch that and add/remove them in my program on the fly.
Otherwise, if the program will be used on a computer with a fixed COM port, I probably would add a fixed Serialport control to my form. I removed extra declarations, but still no events. I can see that mcu is probably receiving something, but its response to the data is wrong and its reply is not received. Code now, all of it.
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. The problem is the data is coming in chunks and not 1 line sentences. If I use SerialPort.ReadLine method, the data comes in 1 line sentences. Using this method, however, has the NewLine variable to set the line ending character for the port. Is the readline method just handling the buffer for me?
Does the data still come in chunks regardless of the method used? When working with the.NET SerialPort implementation, you should NEVER try to read from the serial port using the DataReceived event and any other method. ReadExisting and ReadLine both use the same underlying MemoryStream. You will run into situations where you are pulling data off the MemoryStream from the ReadLine when the event interrupts causing you to attempt to read data that has already been removed from the stream. Use one method or the other.
Do no use both. Hans Passant is correct about the ReadLine blocking your UI thread. You have two methods around that: use the DataReceived event; or place your code that handles the SerialPort on a separate thread. Using the DataReceived event is usually preferable as.NET will run that on a worker thread automatically. You lose the freebie of the ReadLine though. And you have to manually buffer the input if you need to perform read aheads (to give you something like the ReadLine functionality). Your ReadLine call is synchronous and blocks your code, potentially hanging your program.
![]() Serialport Datareceived Separate Thread
DataReceived is asynchronous and doesn't. Which makes it harder to use, there is no free lunch. But can be important, serial ports can be slow enough to make your entire program unresponsive to user input. Not usually an issue in a console mode program or when you use a separate thread. You can call ReadLine in your DataReceived event handler as well, that's desert.
Serialport Datareceived Event
That can technically cause deadlock, you should use the ReadTimeout property. But since you use the console, there's probably little reason to use DataReceived.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |