/// <summary> /// Initialize the communication port. /// </summary> /// <param name="communicationsSetting">The communication settings.</param> /// <exception cref="CommunicationException">Thrown if the error code returned from the call to the PTUDLL32.InitCommunication() method is /// not CommunicationError.Success.</exception> public virtual void InitCommunication(CommunicationSetting_t communicationsSetting) { Int32 error = -1; CommunicationError errorCode = CommunicationError.UnknownError; try { String args = ""; if (communicationsSetting.Protocol == Protocol.RS232) { m_CommDevice = new Serial(); args = "COM" + communicationsSetting.PortIdentifier + "," + ((Int32)communicationsSetting.SerialCommunicationParameters.BaudRate).ToString() + "," + communicationsSetting.SerialCommunicationParameters.Parity.ToString() + "," + ((Int32)communicationsSetting.SerialCommunicationParameters.BitsPerCharacter).ToString() + "," + ((Int32)communicationsSetting.SerialCommunicationParameters.StopBits).ToString(); } else if (communicationsSetting.Protocol == Protocol.TCPIP) { m_CommDevice = new TCP(); args = communicationsSetting.PortIdentifier; } if (m_CommDevice != null) { error = m_CommDevice.Open(args); } } catch (Exception) { errorCode = CommunicationError.SystemException; throw new CommunicationException(Resources.EMPortInitializationFailed, errorCode); } if (DebugMode.Enabled == true) { DebugMode.InitCommunication_t initCommunication = new DebugMode.InitCommunication_t(communicationsSetting.Protocol, communicationsSetting.PortIdentifier, communicationsSetting.SerialCommunicationParameters.BaudRate, communicationsSetting.SerialCommunicationParameters.BitsPerCharacter, communicationsSetting.SerialCommunicationParameters.Parity, communicationsSetting.SerialCommunicationParameters.StopBits, errorCode); DebugMode.Write(initCommunication.ToXML()); } if (error >= 0) { errorCode = CommunicationError.Success; m_WatchClockMarshal = new WatchClockMarshal(m_CommDevice); } if (errorCode != CommunicationError.Success) { throw new CommunicationException(Resources.EMPortInitializationFailed, errorCode); } }
/// <summary> /// Initialize the communication port. /// </summary> /// <param name="communicationsSetting">The communication settings.</param> /// <exception cref="CommunicationException">Thrown if the error code returned from the call to the PTUDLL32.InitCommunication() method is /// not CommunicationError.Success.</exception> public virtual void InitCommunication(CommunicationSetting_t communicationsSetting) { // Check that the function delegate has been initialized. Debug.Assert(m_InitCommunication != null, "CommunicationParent.InitCommunication() - [m_InitCommunication != null]"); CommunicationError errorCode = CommunicationError.UnknownError; try { errorCode = (CommunicationError)m_InitCommunication((short)communicationsSetting.Protocol, communicationsSetting.PortIdentifier, (int)communicationsSetting.SerialCommunicationParameters.BaudRate, (short)communicationsSetting.SerialCommunicationParameters.BitsPerCharacter, (short)communicationsSetting.SerialCommunicationParameters.Parity, (short)communicationsSetting.SerialCommunicationParameters.StopBits); } catch (Exception) { errorCode = CommunicationError.SystemException; throw new CommunicationException(Resources.EMPortInitializationFailed, errorCode); } if (DebugMode.Enabled == true) { DebugMode.InitCommunication_t initCommunication = new DebugMode.InitCommunication_t(communicationsSetting.Protocol, communicationsSetting.PortIdentifier, communicationsSetting.SerialCommunicationParameters.BaudRate, communicationsSetting.SerialCommunicationParameters.BitsPerCharacter, communicationsSetting.SerialCommunicationParameters.Parity, communicationsSetting.SerialCommunicationParameters.StopBits, errorCode); DebugMode.Write(initCommunication.ToXML()); } if (errorCode != CommunicationError.Success) { throw new CommunicationException(Resources.EMPortInitializationFailed, errorCode); } }