예제 #1
0
 private static extern int BuildCommDCB(string lpDef, ref SerialPort.DCB lpDCB);
예제 #2
0
 private static extern int SetCommState(int hFile, ref SerialPort.DCB lpDCB);
예제 #3
0
        public bool Open(string comsettings, int portnum)
        {
            SerialPort.DCB          dCB          = default(SerialPort.DCB);
            SerialPort.COMMTIMEOUTS cOMMTIMEOUTS = default(SerialPort.COMMTIMEOUTS);
            this.Opened = true;
            this.hComm  = SerialPort.CreateFile("\\\\.\\COM" + portnum.ToString(), 3221225472u, 0, 0, 3, 0, 0);
            bool result;

            if (this.hComm == -1)
            {
                SerialPort.CloseHandle(this.hComm);
                this.Opened = false;
                result      = false;
            }
            else
            {
                this.retval = SerialPort.GetCommTimeouts(this.hComm, ref cOMMTIMEOUTS);
                if (this.retval == -1)
                {
                    SerialPort.CloseHandle(this.hComm);
                    this.Opened = false;
                    result      = false;
                }
                else
                {
                    cOMMTIMEOUTS.ReadTotalTimeoutConstant    = this.ReadTimeout;
                    cOMMTIMEOUTS.ReadTotalTimeoutMultiplier  = 0;
                    cOMMTIMEOUTS.WriteTotalTimeoutMultiplier = 0;
                    cOMMTIMEOUTS.WriteTotalTimeoutConstant   = 0;
                    this.retval = SerialPort.SetCommTimeouts(this.hComm, ref cOMMTIMEOUTS);
                    if (this.retval == -1)
                    {
                        SerialPort.CloseHandle(this.hComm);
                        this.Opened = false;
                        result      = false;
                    }
                    else
                    {
                        this.retval = SerialPort.GetCommState(this.hComm, ref dCB);
                        if (this.retval == -1)
                        {
                            SerialPort.CloseHandle(this.hComm);
                            this.Opened = false;
                            result      = false;
                        }
                        else
                        {
                            this.retval = SerialPort.BuildCommDCB(comsettings, ref dCB);
                            if (this.retval == -1)
                            {
                                SerialPort.CloseHandle(this.hComm);
                                this.Opened = false;
                                result      = false;
                            }
                            else
                            {
                                this.retval = SerialPort.SetCommState(this.hComm, ref dCB);
                                if (this.retval == -1)
                                {
                                    SerialPort.CloseHandle(this.hComm);
                                    this.Opened = false;
                                    result      = false;
                                }
                                else
                                {
                                    this.PortNum     = portnum;
                                    this.ComSettings = comsettings;
                                    result           = this.Opened;
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }