Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > SerialPort_DataReceived to RichTextBox control in VB2005
 

SerialPort_DataReceived to RichTextBox control in VB2005

Hi

I have been having a difficult time finding the proper way to pass string data that arrived thru the serial portby way ofthe DataReceived event, to a RichTextBox control on the main form. Moving from VB6 to VB2005 without using the automatic conversion helpers, has been more difficult that i hoped.

I have examined lots of newsgroup posts, looked at posted example code and still I am having trouble.

Some examples work, but appear to be automatically converted VB6 apps with Definstance properties and other items. I would rather understand the proper way to implement this starting from scratch ,using the form designer and the SerialPort control dragged to a form.

I have tried BeginInvoke and Delegate's and used various combinations suggested in other posts but still problems exist.

The serial port functions work fine and the receivied data is properly contained in the string variable in the DataReceived handler. Getting this data to the textbox is the issue.

I have tried using InvokeRequired to determine if BeginInvoke should be used.

Obviously i just do not understand, the concepts even after reading all i can find in MSDN articles.

Currently all builds just fine, but debugger error when data arrivies at DataReceived handler states that BeginInvoke cannot be called on a control until window handle has been created ??????? ( im not sure how that can be, as initial text has already been placed in the richtextbox control on the main form window before the SerialPort is created,opened or data has arrived)

Soin the displaySUB i placed .IsHandleCreated and if i call this Sub from the main form, to display something in the richtextbox, it shows handle created ok, but if i call this same sub from the DataReceived handler it indicates that the handle is not created, and that is why the InvokeRequired always ends up False an i never get serial data to display.

Does anyone have a codesnippet of the proper way to use DataReceived and a richtextbox display starting from placing the Serial Port control on the form ,and by way of a thread safe technique ...

I have looked at the C# example but have not been able to properly convert it toVB2005.

thanks

Ken

kcvaio  Monday, February 20, 2006 4:31 PM

Try to read the description of our serial port program on the URL:

http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm

This describes everything (multithreading, events, delegates, Invoke/BeginInvoke etc.) in a language, which even beginners should understand. Any child, which has been to an amusement park, knows what a queue is, and they also know what a postman is ...Ups. Sorry ReneeC - postal delivery personel . By using the right words, I believe that even absolute beginners can understand and use multithreading. If there is anything you don't understand please let me know.

Don't feel stupid if you don't understand the VB help files. They are cryptical, unnecessary complicated, but worst of all - not even correct as it e.g. is the case with the description of BeginInvoke. How can intelligent people understand a wrong explanation? There are no bad students, just bad teachers.

Carsten Kanstrup  Saturday, February 17, 2007 9:24 AM

Hi,

I'm not sure I understand all of your needs. Do I understand that the port is taking in data at a high rate? and you have to sample continuously in another thread and can't access controls created by the original thread?

What is the problem with byte to unicode conversion classes?

ReneeC  Monday, February 20, 2006 4:36 PM

Hi

No,  there is no issue in obtaining data thru the serial port.

All the data is there in the DataReceived handler.

The problem is displaying the data on a richtextbox control on the main application form.  Since the DataReceived handler is a Shared Sub .. It is not just a simple task of

rtbDisplaytxt.Text = buffer

Im sure the same issue would happen if a timer event handler was trying to update information on a text control on a form ... I do not think this is Serial port specific.

Ken

[edited to add data from MSDN docs ]  this is where im having trouble

The DataReceived event is raised on a secondary thread when data is received from the SerialPort 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 Form or Control, post change requests back using Invoke , which will do the work on the proper thread.

kcvaio  Monday, February 20, 2006 4:52 PM

Ken,

Did you ever figure this out? I have been struggling mightily with the same problem. If you got it to work, pleas post your solution.

John

kalypsys_jw  Saturday, February 17, 2007 2:35 AM

Try to read the description of our serial port program on the URL:

http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm

This describes everything (multithreading, events, delegates, Invoke/BeginInvoke etc.) in a language, which even beginners should understand. Any child, which has been to an amusement park, knows what a queue is, and they also know what a postman is ...Ups. Sorry ReneeC - postal delivery personel . By using the right words, I believe that even absolute beginners can understand and use multithreading. If there is anything you don't understand please let me know.

Don't feel stupid if you don't understand the VB help files. They are cryptical, unnecessary complicated, but worst of all - not even correct as it e.g. is the case with the description of BeginInvoke. How can intelligent people understand a wrong explanation? There are no bad students, just bad teachers.

Carsten Kanstrup  Saturday, February 17, 2007 9:24 AM

You can use google to search for other answers

Custom Search

More Threads

• Unknown Stream Header Byte Sequence
• Best way to write video player
• Displaying System Time in Text Box
• New User Recommendation
• listbox and file deletion
• "The designer could not be shown for this file because none of the classes within it can be designed" . . . WTF?!
• Registration key
• [VB2005] Reading from serial port, pls help!
• How to create browsing history
• Creating New Items in Code