Exemplo n.º 1
0
        public static ConnectionTag CreateNewSerialConnection(IWin32Window parent, SerialTerminalParam param)
        {
            bool       successful = false;
            FileStream strm       = null;

            try {
                string portstr = String.Format("\\\\.\\COM{0}", param.Port);
                IntPtr ptr     = Win32.CreateFile(portstr, Win32.GENERIC_READ | Win32.GENERIC_WRITE, 0, IntPtr.Zero, Win32.OPEN_EXISTING, Win32.FILE_ATTRIBUTE_NORMAL | Win32.FILE_FLAG_OVERLAPPED, IntPtr.Zero);
                if (ptr == Win32.INVALID_HANDLE_VALUE)
                {
                    string msg = GEnv.Strings.GetString("Message.CommunicationUtil.FailedToOpenSerial");
                    int    err = Win32.GetLastError();
                    if (err == 2)
                    {
                        msg += GEnv.Strings.GetString("Message.CommunicationUtil.NoSuchDevice");
                    }
                    else if (err == 5)
                    {
                        msg += GEnv.Strings.GetString("Message.CommunicationUtil.DeviceIsBusy");
                    }
                    else
                    {
                        msg += "\nGetLastError=" + Win32.GetLastError();
                    }
                    throw new Exception(msg);
                }
                //strm = new FileStream(ptr, FileAccess.Write, true, 8, true);
                Win32.DCB dcb = new Win32.DCB();
                FillDCB(ptr, ref dcb);
                UpdateDCB(ref dcb, param);

                if (!Win32.SetCommState(ptr, ref dcb))
                {
                    throw new Exception(GEnv.Strings.GetString("Message.CommunicationUtil.FailedToConfigSerial"));
                }
                Win32.COMMTIMEOUTS timeouts = new Win32.COMMTIMEOUTS();
                Win32.GetCommTimeouts(ptr, ref timeouts);
                timeouts.ReadIntervalTimeout         = 0xFFFFFFFF;
                timeouts.ReadTotalTimeoutConstant    = 0;
                timeouts.ReadTotalTimeoutMultiplier  = 0;
                timeouts.WriteTotalTimeoutConstant   = 100;
                timeouts.WriteTotalTimeoutMultiplier = 100;
                Win32.SetCommTimeouts(ptr, ref timeouts);
                successful = true;
                System.Drawing.Size      sz = GEnv.Frame.TerminalSizeForNextConnection;
                SerialTerminalConnection r  = new SerialTerminalConnection(param, ptr, sz.Width, sz.Height);
                r.SetServerInfo("COM" + param.Port, null);
                return(new ConnectionTag(r));
            }
            catch (Exception ex) {
                GUtil.Warning(parent, ex.Message);
                return(null);
            }
            finally {
                if (!successful && strm != null)
                {
                    strm.Close();
                }
            }
        }
        public CommandResult SerialConfig()
        {
            SerialTerminalConnection stc = _connection as SerialTerminalConnection;

            if (stc == null)
            {
                return(CommandResult.Failed);
            }

            try {
                SerialConfigForm f = new SerialConfigForm();
                f.ApplyParam(stc);
                if (GCUtil.ShowModalDialog(GApp.Frame, f) == DialogResult.OK)
                {
                    return(CommandResult.Success);
                }
                else
                {
                    return(CommandResult.Cancelled);
                }
            }
            catch (NotSupportedException) {
                GUtil.Warning(GApp.Frame, GApp.Strings.GetString("Message.SerialConfigIsSerialOnly"));
                return(CommandResult.Failed);
            }
        }