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();
            }
        }
Exemple #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");
            }
        }
Exemple #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);
        }
 public static extern bool SetCommState(IntPtr hFile, [In] ref SerialPortState lpDCB);
 public static extern bool GetCommState(IntPtr hFile, ref SerialPortState lpDcb);
        /// <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");
            }
        }
        /// <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;
        }
 public static extern bool GetCommState(IntPtr hFile, ref SerialPortState lpDcb);