Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm trying to direct the SerialPort received data to the terminal control.
can you direct me how to achieve it..?
i tried the following:
C#
SerialTerminalParam stp = new SerialTerminalParam();
stp.BaudRate = int.Parse(cmbBaudRate.Text);
stp.FlowControl = FlowControl.None;
stp.Parity = Poderosa.ConnectionParam.Parity.NOPARITY;
stp.Port = 1;
stp.StopBits = Poderosa.ConnectionParam.StopBits.ONESTOPBIT;
stp.TerminalType = TerminalType.XTerm;

      
           
IntPtr pt = new IntPtr();
SerialTerminalConnection con = new SerialTerminalConnection(stp,  pt,624, 249);
ConnectionTag ct = new ConnectionTag(con);
TerminalDataReceiver tdr = new TerminalDataReceiver(ct);


but i'm getting an error saying: Object reference not set to an instance of an object.
at the following line (@ TerminalControl\Terminal\connections.cs):
GEnv.Connections.KeepAlive.SetTimerToConnectionTag(this);

do you know what are the required steps to open a serial connection? instead SSH connection you gave as an example...
Posted
Updated 1-Mar-18 7:47am
v2
Comments
Richard MacCutchan 11-Oct-10 9:58am    
The line with your error message is not present in your code snippet; did you post the correct portion of your program?

Also, what example are you referring to?
cholegm 15-May-12 3:54am    
Does any know why I got error "The following error occurred when connecting to COM2. WaitCommEvent failed 6" when I try to open/connect to the serial port (COM2)?

1 solution

C#
int Port = 2;
int BaudRate = 115200;
ConnectionMethod = ConnectionMethod.Serial;
 
public void Connect()
        {
            // Save old log info in case this is a reconnect
            Poderosa.ConnectionParam.LogType logType = Poderosa.ConnectionParam.LogType.Default;
            string file = null;
            if (this.TerminalPane.Connection != null)
            {
                logType = this.TerminalPane.Connection.LogType;
                file = this.TerminalPane.Connection.LogPath;
                //GApp.GetConnectionCommandTarget().Close();
                this.TerminalPane.Connection.Close();
                this.TerminalPane.Detach();
            }
 
            try
            {
                if ((Poderosa.ConnectionParam.ConnectionMethod)this.Method == Poderosa.ConnectionParam.ConnectionMethod.Telnet)
                {
                    TelnetTerminalParam tel = new TelnetTerminalParam(this.Host);
                    tel.Port = Port;
                    tel.TerminalType = TerminalType.VT100;
                    tel.RenderProfile = new RenderProfile();
                    tel.Encoding = EncodingType.ISO8859_1;
 
                    CommunicationUtil.SilentClient s = new CommunicationUtil.SilentClient();
                    Size sz = this.Size;
 
                    SocketWithTimeout swt;
                    swt = new TelnetConnector((Poderosa.ConnectionParam.TelnetTerminalParam)tel, sz);
                    swt.AsyncConnect(s, tel.Host, tel.Port);
                    ConnectionTag ct = s.Wait(swt);
 
                    this.TerminalPane.FakeVisible = true;
                    this.TerminalPane.Attach(ct);
                    ct.Receiver.Listen();
 
                    if (file != null)
                        this.SetLog((LogType)logType, file, true);
                    this.TerminalPane.ConnectionTag.RenderProfile = new RenderProfile();
                    this.SetPaneColors(Color.White, Color.Black);
 
                }
                else if ((Poderosa.ConnectionParam.ConnectionMethod)this.Method == Poderosa.ConnectionParam.ConnectionMethod.SSH2)
                {
                    //------------------------------------------------------------------------
                    SSHTerminalParam sshp = new SSHTerminalParam((Poderosa.ConnectionParam.ConnectionMethod)this.Method, this.Host, this.UserName, this.Password);
                    sshp.AuthType = this.AuthType;
                    sshp.IdentityFile = this.IdentifyFile;
                    sshp.Encoding = EncodingType.ISO8859_1;
                    sshp.Port = Port;
                    sshp.RenderProfile = new RenderProfile();
                    sshp.TerminalType = TerminalType.XTerm;
 
                    CommunicationUtil.SilentClient s = new CommunicationUtil.SilentClient();
                    Size sz = this.Size;
 
                    SocketWithTimeout swt;
                    swt = new SSHConnector((Poderosa.ConnectionParam.SSHTerminalParam)sshp, sz, sshp.Passphrase, (HostKeyCheckCallback)null);
                    swt.AsyncConnect(s, sshp.Host, sshp.Port);
                    ConnectionTag ct = s.Wait(swt);
 
                    this.TerminalPane.FakeVisible = true;
 
                    this.TerminalPane.Attach(ct);
                    ct.Receiver.Listen();
                    //-------------------------------------------------------------
                    if (file != null)
                        this.SetLog((LogType)logType, file, true);
                    this.TerminalPane.ConnectionTag.RenderProfile = new RenderProfile();
                    this.SetPaneColors(Color.White,Color.Black);
                }
                else if ((Poderosa.ConnectionParam.ConnectionMethod)this.Method == Poderosa.ConnectionParam.ConnectionMethod.Serial)
                {
                    SerialTerminalParam stp = new SerialTerminalParam();
                    stp.BaudRate = BaudRate;
                    stp.FlowControl = FlowControl.None;
                    stp.Parity = Poderosa.ConnectionParam.Parity.NOPARITY;
                    stp.Port = Port;
                    stp.StopBits = Poderosa.ConnectionParam.StopBits.ONESTOPBIT;
                    stp.TerminalType = TerminalType.XTerm;
 
                    IntPtr pt = new IntPtr();
                    SerialTerminalConnection stcon = new SerialTerminalConnection(stp, pt, this.TerminalPane.Width, this.TerminalPane.Height);
                    ConnectionTag ct = new ConnectionTag(stcon);
 
                    this.TerminalPane.FakeVisible = true;
 
                    this.TerminalPane.Attach(ct);
                    ct.Receiver.Listen();
                    //-------------------------------------------------------------
                    if (file != null)
                        this.SetLog((LogType)logType, file, true);
                    this.TerminalPane.ConnectionTag.RenderProfile = new RenderProfile();
                    this.SetPaneColors(Color.White, Color.Black);
                }
                else
                {
                    return;
                }
            }
            catch
            {
                //MessageBox.Show(e.Message, "Connection Error");
                return;
            }
        }



When I try to open/connect to the serial port (COM2) I got error "The following error occurred when connecting to COM2. WaitCommEvent failed 6".
 
Share this answer
 
Comments
cholegm 15-May-12 3:53am    
Does any know why I got error "The following error occurred when connecting to COM2. WaitCommEvent failed 6" when I try to open/connect to the serial port (COM2)?
Member 13140892 8-May-17 10:28am    
Did this issue ever get solved? I'd like to use this solution, but I'm getting the same error on trying to open the port.
ElectroMonster 1-Mar-18 21:00pm    
Anybody has a solution for this code. Some reason it is complaining for port is busy.
ajeda 25-May-18 6:32am    
Problem with this soultion is that it creates an empty handle for the serial port (IntPtr pt = new IntPtr) instead of obtaining a correct one. You can try to create one manually but I've found it easier to use the CommunicationUtil.CreateNewSerialConnection(IWin32Window parent, SerialTerminalParam stp) function which returns ConnectionTag object. So instead of creating ConnectionTag by hand like in the example above, use this function and pass it a SerialTerminalParam instance with desired configuration. It should work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900