コード例 #1
0
        public static SerialTerminalConnection CreateNewSerialConnection(IPoderosaMainWindow window, SerialTerminalParam param, SerialTerminalSettings settings)
        {
            bool       successful = false;
            FileStream strm       = null;

            try {
                StringResource sr = SerialPortPlugin.Instance.Strings;
                //Debug.WriteLine("OPENING COM"+param.Port);
                string portstr = String.Format("\\\\.\\{0}", param.PortName);
                IntPtr ptr     = Win32Serial.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 = sr.GetString("Message.FailedToOpenSerial");
                    int    err = Marshal.GetLastWin32Error();
                    if (err == 2)
                    {
                        msg += sr.GetString("Message.NoSuchDevice");
                    }
                    else if (err == 5)
                    {
                        msg += sr.GetString("Message.DeviceIsBusy");
                    }
                    else
                    {
                        msg += "\nGetLastError=" + Marshal.GetLastWin32Error();
                    }
                    throw new Exception(msg);
                }
                //strm = new FileStream(ptr, FileAccess.Write, true, 8, true);
                Win32Serial.DCB dcb = new Win32Serial.DCB();
                FillDCB(ptr, ref dcb);
                UpdateDCB(ref dcb, settings);

                if (!Win32Serial.SetCommState(ptr, ref dcb))
                {
                    Win32.CloseHandle(ptr);
                    throw new Exception(sr.GetString("Message.FailedToConfigSerial"));
                }
                Win32Serial.COMMTIMEOUTS timeouts = new Win32Serial.COMMTIMEOUTS();
                Win32Serial.GetCommTimeouts(ptr, ref timeouts);
                timeouts.ReadIntervalTimeout         = 0xFFFFFFFF;
                timeouts.ReadTotalTimeoutConstant    = 0;
                timeouts.ReadTotalTimeoutMultiplier  = 0;
                timeouts.WriteTotalTimeoutConstant   = 100;
                timeouts.WriteTotalTimeoutMultiplier = 100;
                Win32Serial.SetCommTimeouts(ptr, ref timeouts);
                successful = true;
                SerialTerminalConnection r = new SerialTerminalConnection(param, settings, ptr);
                return(r);
            }
            catch (Exception ex) {
                RuntimeUtil.SilentReportException(ex);
                if (window != null)
                {
                    window.Warning(ex.Message);
                }
                else
                {
                    GUtil.Warning(Form.ActiveForm, ex.Message); //TODO 苦しい逃げ。IPoderosaFormを実装したベースクラスをCoreにでも持っていたほうがいいのか
                }
                return(null);
            }
            finally {
                if (!successful && strm != null)
                {
                    strm.Close();
                }
            }
        }
コード例 #2
0
ファイル: SerialConnection.cs プロジェクト: FNKGino/poderosa
        public static SerialTerminalConnection CreateNewSerialConnection(IPoderosaMainWindow window, SerialTerminalParam param, SerialTerminalSettings settings)
        {
            bool successful = false;
            FileStream strm = null;
            try {
                StringResource sr = SerialPortPlugin.Instance.Strings;
                //Debug.WriteLine("OPENING COM"+param.Port);
                string portstr = String.Format("\\\\.\\{0}", param.PortName);
                IntPtr ptr = Win32Serial.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 = sr.GetString("Message.FailedToOpenSerial");
                    int err = Marshal.GetLastWin32Error();
                    if (err == 2)
                        msg += sr.GetString("Message.NoSuchDevice");
                    else if (err == 5)
                        msg += sr.GetString("Message.DeviceIsBusy");
                    else
                        msg += "\nGetLastError=" + Marshal.GetLastWin32Error();
                    throw new Exception(msg);
                }
                //strm = new FileStream(ptr, FileAccess.Write, true, 8, true);
                Win32Serial.DCB dcb = new Win32Serial.DCB();
                FillDCB(ptr, ref dcb);
                UpdateDCB(ref dcb, settings);

                if (!Win32Serial.SetCommState(ptr, ref dcb)) {
                    Win32.CloseHandle(ptr);
                    throw new Exception(sr.GetString("Message.FailedToConfigSerial"));
                }
                Win32Serial.COMMTIMEOUTS timeouts = new Win32Serial.COMMTIMEOUTS();
                Win32Serial.GetCommTimeouts(ptr, ref timeouts);
                timeouts.ReadIntervalTimeout = 0xFFFFFFFF;
                timeouts.ReadTotalTimeoutConstant = 0;
                timeouts.ReadTotalTimeoutMultiplier = 0;
                timeouts.WriteTotalTimeoutConstant = 100;
                timeouts.WriteTotalTimeoutMultiplier = 100;
                Win32Serial.SetCommTimeouts(ptr, ref timeouts);
                successful = true;
                SerialTerminalConnection r = new SerialTerminalConnection(param, settings, ptr);
                return r;
            }
            catch (Exception ex) {
                RuntimeUtil.SilentReportException(ex);
                if (window != null)
                    window.Warning(ex.Message);
                else
                    GUtil.Warning(Form.ActiveForm, ex.Message); //TODO 苦しい逃げ。IPoderosaFormを実装したベースクラスをCoreにでも持っていたほうがいいのか
                return null;
            }
            finally {
                if (!successful && strm != null)
                    strm.Close();
            }
        }