Terminal Emulator Problem

MTSkull

Centurion
Joined
Mar 25, 2003
Messages
151
Location
Boulder, Colorado
I wrote a Simple Hyper Terminal Emulator because my regular hyper terminal keeps locking up my computer. I have gotten the functionality pretty close to what I want with one exception. I am outputting the contents of the serial buffer to a rich text box that will also take key strokes and send them back to the device under test. The problem I am having is getting the richtext box to stay scrolled down so it displays only the latest received data.

Approaches I have tried...

richtext.scrolltocarat();
every time new serial data enters the text box the carat jumps back to the home position so scroll to carat does nothing.

SendKeys.Send("^{END}");
because I am reacting to keystroke events this sends the program into an infinite loop, I think.

Limiting the text box to 30 lines of the most recent data. Worked great when I was reading serial data in Line by Line, just push the results into a string collection. But the application needed to react to partial lines of incoming data.

Attached is the whole project if anyone would like to take a look. but here is the pertinent code.
C#:
        private void timer1_Tick(object sender, EventArgs e)
        {
            //function checks the input stream for buffered text, if it is there read it out to the rtxtbox

            if (serialPort1.IsOpen != true)
                return;

            //get input stream from port1
            // check buffer?
            if (serialPort1.BytesToRead > 0)
            {
                try
                {
                    rtxtStatus.Text = rtxtStatus.Text + serialPort1.ReadExisting();

                    //Does not work, need to force carat to end so it will subequently scroll to the carat.
                    // Carat returns to home and does not stay at the end of the control.
                    //rtxtStatus.Focus();
                    //rtxtStatus.ScrollToCaret();
                    
                    //Sending Cntrl End to move to the end of the rich text box
                    //send keys cause endless loop in conjunction with form keypress event.
                    //rtxtStatus.Focus();
                    //SendKeys.Send("^{END}");
                    //txtSendText.Focus();

                    //Trying to limit the lines of text to 30 keeping the most current info in view
                    //if (FromRadio.Count > 30)
                    //{
                    //    rtxtStatus.Text = "";
                    //    for(int x = (FromRadio.Count-31);x<FromRadio.Count;x++)
                    //        rtxtStatus.Text = rtxtStatus.Text + FromRadio[x];
                    //}
                    //else
                    //    rtxtStatus.Text = rtxtStatus.Text + FromRadio[FromRadio.Count-1];

                }
                catch
                {
                    //do nothing??
                    // if it errors I don't care just keep going... for now
                }
            }
            
            this.Update();
            Application.DoEvents();
        }
 

Attachments

Last edited by a moderator:
SelectionStart property

Try:

C#:
rtxtStatus.Text = rtxtStatus.Text + serialPort1.ReadExisting(); //Add text
rtxtStatus.SelectionLength = 0;
rtxtStatus.SelectionStart = rtxtStatus.Text.Length; //Move to end

Or you might have more success with:

C#:
rtxtStatus.SelectionLength = 0;
rtxtStatus.SelectionStart = rtxtStatus.Text.Length; //Move to end
rtxtStatus.SelectedText = serialPort1.ReadExisting(); //Add text

Good luck :)
 
Worked Kind of...
C#:
rtxtStatus.Text = rtxtStatus.Text + serialPort1.ReadExisting(); //Add text
rtxtStatus.SelectionLength = 0;
rtxtStatus.SelectionStart = rtxtStatus.Text.Length; //Move to end 

//Added
rtxtStatus.ScrollToCaret();

The selection did not scroll down just moved the carat to the end allowing the scroll to carat function to work.

Thanks Mr Paul you are awesome.
MT
 
Consider the second code block

I'm fairly sure the second block of code I posted will keep the textbox scrolled down by itself (without ScrollToCaret). This approach should also prevent something that may or may not be an issue for you - if the user has scrolled up to read previous text they might not like it if the textbox keeps scrolling down again, and I'm fairly sure the second block of code prevents this.

Good luck :)
 
The second block behaves better, but both still jump to the end when new text comes in. This is not a worry though since I am the intended user.

This project is meant to be a simpler Hyper Terminal, as well as letting me do experiments in the future. This also allows me to scroll through the entire session. In order to do that in Hyper Terminal you have to push the results out to a text file.

Thanks
MT
 
Back
Top