예제 #1
0
        /// <summary>
        /// Opens the com port and configures it with the required settings
        /// </summary>
        /// <returns>false if the port could not be opened</returns>
        protected bool Open
            (byte comNum, ushort portSpeed, ushort portParity, ushort portStopBits
            )
        {
            if (online)
            {
                return(false);
            }
            string comtxt = "COM" + comNum.ToString();

            hPort = RSWrapper.CreateFile
                    (
                comtxt, RSWrapper.GENERIC_READ | RSWrapper.GENERIC_WRITE, 0, IntPtr.Zero, RSWrapper.OPEN_EXISTING,
                0, IntPtr.Zero
                    );
            if (hPort == (IntPtr)RSWrapper.INVALID_HANDLE_VALUE)
            {
                return(false);
            }
            if (!SetComStat((RSWrapper.BAUD_RATE)portSpeed, (RSWrapper.ParityBit)portParity, (RSWrapper.StopBits)portStopBits, 8))
            {
                return(false);
            }
            if (!AfterOpen())
            {
                Close();
                return(false);
            }
            online = true;
            return(true);
        }
예제 #2
0
 /// <summary>
 /// Sends a protocol byte immediately ahead of any queued bytes.
 /// </summary>
 /// <param name="tosend">Byte to send</param>
 /// <returns>False if an immediate byte is already scheduled and not yet sent</returns>
 protected bool SendImmediate(byte tosend)
 {
     CheckOnline();
     if (RSWrapper.TransmitCommChar(hPort, tosend))
     {
         return(true);
     }
     return(false);
 }
예제 #3
0
 /// <summary>
 /// The ReadFile function reads data from a serial port,
 /// </summary>
 /// <param name="toRead">Pointer to the buffer that receives the data read from</param>
 /// <param name="maxDataLength">[in] Number of bytes to be read from the port</param>
 /// <param name="dataLength">Variable that receives the number of bytes read. Read sets this value to zero
 /// before doing any work or error checking. </param>
 /// <returns>If the function succeeds, the return value is true. </returns>
 protected bool Read(IntPtr toRead, uint maxDataLength, out uint dataLength)
 {
     if (RSWrapper.ReadFile(hPort, toRead, maxDataLength, out dataLength, ptrUWO))
     {
         incBytesTransferred(dataLength);
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #4
0
        /// <summary>
        /// Check if the port is online.
        /// </summary>
        /// <returns></returns>
        protected bool CheckOnline()
        {
            uint f;

            if (online)
            {
                if (RSWrapper.GetHandleInformation(hPort, out f))
                {
                    return(true);
                }
            }
            return(false);
        }//CheckOnline
예제 #5
0
        protected bool Send(Byte[] tosend)
        {
            uint   sent       = 0;
            ushort writeCount = (ushort)tosend.GetLength(0);

            CheckOnline();
            if ((RSWrapper.WriteFile(hPort, tosend, writeCount, out sent, ptrUWO)) && (writeCount == sent))
            {
                incBytesTransferred(writeCount);
                return(true);
            }
            return(false);
        }
예제 #6
0
 /// <summary>
 /// The GetCommState function retrieves the current control settings for a specified communications device.
 /// </summary>
 /// <param name="BaudRate">Baud rate at which the communications device operates.</param>
 /// <param name="Parity">If param is TRUE, parity checking is performed and errors are reported.</param>
 /// <param name="StopBits">Number of stop bits to be used.
 /// </param>
 /// <param name="ByteSize">Number of bits in the bytes transmitted and received.</param>
 /// <returns></returns>
 protected bool GetComStatSel
     (ref Int32 BaudRate, ref RSWrapper.ParityBit Parity, ref RSWrapper.StopBits StopBits, ref byte ByteSize)
 {
     RSWrapper.DCB PortDCB = new RSWrapper.DCB();
     if (!RSWrapper.GetCommState(hPort, ref PortDCB))
     {
         return(false);
     }
     BaudRate = PortDCB.BaudRate;
     ByteSize = PortDCB.ByteSize;
     Parity   = PortDCB.Parity;
     StopBits = PortDCB.stopBits;
     return(true);
 }
예제 #7
0
        /// <summary>
        /// Queues bytes for transmission.
        /// </summary>
        /// <param name="tosend">Array of bytes to be sent</param>
        /// <param name="dataLength">the length of data</param>
        protected bool Send(IntPtr tosend, ushort dataLength)
        {
            uint sent = 0;

            if (!CheckOnline())
            {
                return(false);
            }
            if ((RSWrapper.WriteFile(hPort, tosend, dataLength, out sent, ptrUWO)) && (dataLength == sent))
            {
                incBytesTransferred(dataLength);
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #8
0
 /// <summary>
 /// This function configures a communications device
 /// </summary>
 /// <param name="BaudRate">Specifies the baud rate at which the communication device operates.</param>
 /// <param name="Parity">Specifies the parity scheme to be used.</param>
 /// <param name="StopBits">Specifies the number of stop bits to be used.</param>
 /// <param name="ByteSize">Specifies the number of bits in the bytes transmitted and received.</param>
 /// <returns>true indicates success.</returns>
 protected bool SetComStat
     (RSWrapper.BAUD_RATE BaudRate, RSWrapper.ParityBit Parity, RSWrapper.StopBits StopBits, byte ByteSize)
 {
     RSWrapper.DCB PortDCB = new RSWrapper.DCB();
     if (!RSWrapper.GetCommState(hPort, ref PortDCB))
     {
         return(false);
     }
     PortDCB.BaudRate = (int)BaudRate;
     PortDCB.ByteSize = ByteSize;
     PortDCB.Parity   = Parity;
     PortDCB.stopBits = StopBits;
     if (!RSWrapper.SetCommState(hPort, ref PortDCB))
     {
         return(false);
     }
     return(true);
 }
예제 #9
0
        }//CheckOnline

        //The ClearCommError function retrieves information about a communications error and reports the current
        // status of a communications device.
        /// <summary>
        /// The ClearCommError function retrieves information about a communications error and reports the current
        /// status of a communications device.
        /// </summary>
        /// <param name="inRxQueue">Position in rx queue</param>
        /// <param name="inTxQueue">Position in tx queue</param>
        /// <returns></returns>
        protected bool ClearCommErrorSel(out UInt32 inRxQueue, out UInt32 inTxQueue)
        {
            RSWrapper.COMSTAT cs;
            uint er;

            inRxQueue = 0;
            inTxQueue = 0;
            if (!CheckOnline())
            {
                return(false);
            }
            if (!RSWrapper.ClearCommError(hPort, out er, out cs))
            {
                return(false);
            }
            inRxQueue = cs.cbInQue;
            inTxQueue = cs.cbOutQue;
            return(true);
        }
예제 #10
0
 private void InternalClose()
 {
     RSWrapper.CloseHandle(hPort);
     online = false;
 }