////////////////////////////////////////////////////////////////////////////////////// //SERIAL METHODS ////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Opens the serial Port /// </summary> /// <param name="port">Port Name i.e. "COM1:"</param> /// <param name="readTimeOutConst">How long is the serial port wait for an answer</param> /// <returns></returns> public bool Open(string port, int readTimeOutConst) //port syntax: "COM#:" { //Configure/Prepare to Open Close(); //Close port if already open DCB dcbCommPort = new DCB(); //Call DCB struct COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); //Call COMMTIMEOUTS struct COMMPROP commprop = new COMMPROP(); //Call COMMPROP struct //Open the COM port hComm = CreateFile( port, //Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, //Access (read-write) mode 0, //Share mode 0, //Pointer to the security attribute OPEN_EXISTING, //How to open the serial port 0, //Port attributes 0); //Handle to port with attribute to copy // If Port Can't be Opened, Return if (this.hComm == INVALID_HANDLE_VALUE) { return(false); //Return, update user } if (this.hComm == INVALID_HANDLE_VALUE) { throw(new ApplicationException("Comm Port Can Not Be Opened")); } // Set the COMM Port Type commprop.dwProvSubType = 0x00000001; // Set the COMM Timeouts GetCommTimeouts(hComm, ref ctoCommPort); ctoCommPort.ReadTotalTimeoutConstant = readTimeOutConst; ctoCommPort.ReadTotalTimeoutMultiplier = 0; ctoCommPort.WriteTotalTimeoutMultiplier = 0; ctoCommPort.WriteTotalTimeoutConstant = 0; SetCommTimeouts(hComm, ref ctoCommPort); // Set BAUD RATE, PARITY, WORD SIZE, AND STOP BITS. (From Above Values) GetCommState(hComm, ref dcbCommPort); dcbCommPort.BaudRate = BaudRate; dcbCommPort.flags = 0; dcbCommPort.flags |= 1; if (Parity > 0) { dcbCommPort.flags |= 2; } dcbCommPort.Parity = Parity; dcbCommPort.ByteSize = ByteSize; dcbCommPort.StopBits = StopBits; if (!SetCommState(hComm, ref dcbCommPort)) //Throw failure exception if needed { uint ErrorNum = GetLastError(); } Opened = true; //Port open return(true); //Return bool }
public unsafe void GetCommProperties() { using (SafeFileHandle handle = CommunicationsMethods.CreateComFileHandle(@"\\.\COM4")) { COMMPROP properties = CommunicationsMethods.GetCommProperties(handle); } }
internal static extern bool GetCommProperties( SafeFileHandle hFile, // handle to comm device ref COMMPROP lpCommProp // communications properties );
public static extern Boolean GetCommProperties(IntPtr hFile, out COMMPROP cp);
internal static extern bool GetCommProperties(SafeFileHandle hFile, ref COMMPROP lpCommProp);
internal static extern Boolean GetCommProperties(IntPtr hFile, out COMMPROP cp);
/// <summary> /// Default constructor. Creates the comm properties structure. /// </summary> internal Win32Props() { this.cp = new COMMPROP(); return; }
static extern bool GetCommProperties(IntPtr hFile, ref COMMPROP lpCommProp);
public void reloadSettings() { COMMPROP commProp = new COMMPROP(); IntPtr hFile = CreateFile(@"\\.\" + MainWindow.instance.serialPort.PortName, 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero); GetCommProperties(hFile, ref commProp); }
public static extern bool GetCommProperties( SafeFileHandle hFile, out COMMPROP lpCommProp);
internal static extern bool GetCommProperties(IntPtr hFile, out COMMPROP cp);
internal static extern bool GetCommProperties( IntPtr hFile, ref COMMPROP lpCommProp );