Exemplo n.º 1
0
        static void Main(string[] args)
        {
            SerialPort sp = new SerialPort("COM4");

            if (sp.Open())
            {
                sp.SetBreakState();
                Thread.Sleep(50);
                sp.ClearBreakState();

                sp.SetDefaultTimeouts();

                SerialPortState cfg = sp.GetState();
                cfg.BaudRate = 115200;
                sp.SetState(cfg);

                for (int i = 0; i < 3; i++)
                {
                    string message = sp.ReadLine();

                    bool result = sp.WriteLine("cataca");
                }

                sp.Close();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets the state (config parameters) on the serial port.
        /// </summary>
        /// <param name="state">The configuation to be applied to the serial port.</param>
        public void SetState(SerialPortState state)
        {
            if (!IsOpen)
            {
                throw new InvalidOperationException("Port is not open.");
            }

            if (!Win32Api.SetCommState(m_fileHandle, ref state))
            {
                throw new Win32Exception(Win32Api.GetLastError(), "'SetCommState()' failed");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the current state (config parameters) set on the serial port.
        /// </summary>
        /// <returns>The current serial port state.</returns>
        public SerialPortState GetState()
        {
            if (!IsOpen)
            {
                throw new InvalidOperationException("Port is not open.");
            }

            SerialPortState dcb = new SerialPortState();

            if (!Win32Api.GetCommState(m_fileHandle, ref dcb))
            {
                throw new Win32Exception(Win32Api.GetLastError(), "'GetCommState()' failed");
            }

            return(dcb);
        }
Exemplo n.º 4
0
 public static extern bool SetCommState(IntPtr hFile, [In] ref SerialPortState lpDCB);
Exemplo n.º 5
0
 public static extern bool GetCommState(IntPtr hFile, ref SerialPortState lpDcb);
Exemplo n.º 6
0
        /// <summary>
        /// Sets the state (config parameters) on the serial port.
        /// </summary>
        /// <param name="state">The configuation to be applied to the serial port.</param>
        public void SetState(SerialPortState state)
        {
            if (!IsOpen) throw new InvalidOperationException("Port is not open.");

            if (!Win32Api.SetCommState(m_fileHandle, ref state))
            {
                throw new Win32Exception(Win32Api.GetLastError(), "'SetCommState()' failed");
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the current state (config parameters) set on the serial port.
        /// </summary>
        /// <returns>The current serial port state.</returns>
        public SerialPortState GetState()
        {
            if (!IsOpen) throw new InvalidOperationException("Port is not open.");

            SerialPortState dcb = new SerialPortState();
            if (!Win32Api.GetCommState(m_fileHandle, ref dcb))
            {
                throw new Win32Exception(Win32Api.GetLastError(), "'GetCommState()' failed");                
            }

            return dcb;
        }
Exemplo n.º 8
0
 public static extern bool GetCommState(IntPtr hFile, ref SerialPortState lpDcb);