Exemple #1
0
        public static bool ExecuteSerialCommand(IntPtr serialCommRef, uint portNumber, string Command, out string bufferData, int timeOut, int delayBeforeReadingResponse)
        {
            bool   portConnected = false;
            string error         = "";
            int    retVal;
            string bufferDataFromCamera = "";

            try
            {
                //this.Text = string.Format("Please wait while detecting the connected cameras...");
                // this.Refresh();

                retVal = ClserWrapper.clSerialInit((uint)portNumber, ref serialCommRef);

                if (retVal != ClserWrapper.CL_ERR_NO_ERR)
                {
                    error         = string.Format("Cannot initialize serial port : {0}", portNumber);
                    portConnected = false;
                }

                retVal = ClserWrapper.clSetBaudRate(serialCommRef, ClserWrapper.CL_BAUDRATE_9600);
                if (retVal != ClserWrapper.CL_ERR_NO_ERR)
                {
                    error         = string.Format("Cannot set baud rate 9600 for serial port {0}", portNumber);
                    portConnected = false;
                }

                // Check whether camera is a "e2v" camera

                string command = Command.Contains(System.Environment.NewLine) ? Command : Command + System.Environment.NewLine;

                if (GetResponseFromSerialPort(serialCommRef,
                                              (uint)portNumber,
                                              command,
                                              timeOut,
                                              out bufferDataFromCamera,
                                              delayBeforeReadingResponse, out error))
                {
                    portConnected = true;
                }
                else
                {
                    portConnected = false;
                    throw new Exception(error);
                }
            }
            catch (Exception ex)
            {
                portConnected        = false;
                bufferDataFromCamera = "";
                throw new Exception(error);
            }


            ClserWrapper.clSerialClose(serialCommRef);
            bufferData = bufferDataFromCamera;
            return(portConnected);
        }
Exemple #2
0
        /// <summary>
        /// Gets the response from e2v camera. This function send the serial commands to the camera and read
        /// the response from the camera. A maximum of three attempts are made to read the response from camera
        /// in case of failures (such as internal error).
        /// </summary>
        /// <param name="serialComRef">The serial communication port reference.</param>
        /// <param name="portNumber">The serial port number.</param>
        /// <param name="command">The command.</param>
        /// <param name="bufferData">The buffer to receive the response from camera.</param>
        /// <returns></returns>
        public static bool GetResponseFromSerialPort(IntPtr serialComRef,
                                                     uint portNumber,
                                                     string command,
                                                     int timeOut,
                                                     out string bufferData,
                                                     int delayBeforeReadingResponse, out string err)
        {
            char   cont = 'y';
            bool   commandProcessingSuccess = true;
            uint   numberOfTrials           = 0;
            string error = "";

            do
            {
                int retVal = ClserWrapper.clFlushPort(serialComRef);
                if (retVal != ClserWrapper.CL_ERR_NO_ERR)
                {
                    error      = string.Format("Error in flushing serial port {0}. Error code : {1}", portNumber, retVal);
                    cont       = 'y';
                    bufferData = "";
                }

                byte[] writeBuffer     = System.Text.ASCIIEncoding.ASCII.GetBytes(command);
                uint   writeBufferSize = (uint)writeBuffer.Length;

                retVal = ClserWrapper.clSerialWrite(serialComRef, writeBuffer, ref writeBufferSize,
                                                    (uint)timeOut);

                if (retVal != ClserWrapper.CL_ERR_NO_ERR)
                {
                    error = string.Format("Error in writing command to serial port {0}. Error Code : {1}. Commad written : {2}", portNumber, retVal, command);
                    cont  = 'y';
                    commandProcessingSuccess = false;
                    bufferData = "";
                }
                else
                {
                    System.Threading.Thread.Sleep(delayBeforeReadingResponse);
                    uint   numBytes     = 1024;
                    byte[] readBuffer   = new byte[1024];
                    IntPtr ipReadBuffer = Marshal.AllocHGlobal(readBuffer.Length);
                    retVal = ClserWrapper.clSerialRead(serialComRef, ipReadBuffer, ref numBytes,
                                                       (uint)timeOut);
                    if (retVal == ClserWrapper.CL_ERR_NO_ERR)
                    {
                        Marshal.Copy(ipReadBuffer, readBuffer, 0, (int)numBytes);
                        readBuffer = readBuffer.Take((int)numBytes).ToArray();
                        string readBufferContents = System.Text.ASCIIEncoding.ASCII.GetString(readBuffer);
                        if (readBufferContents.IndexOf(">Ok\r") >= 0)
                        {
                            bufferData = readBufferContents;
                            commandProcessingSuccess = true;
                            cont = 'n';
                        }
                        else if (readBufferContents.IndexOf(">3\r") >= 0 || readBufferContents.IndexOf(">16\r") >= 0 ||
                                 readBufferContents.IndexOf(">21\r") >= 0 || readBufferContents.IndexOf(">33\r") >= 0 ||
                                 readBufferContents.IndexOf(">34\r") >= 0 || readBufferContents.IndexOf(">7\r") >= 0)
                        {
                            cont       = 'y';
                            bufferData = "";
                            commandProcessingSuccess = false;
                            error = string.Format("Error in getting response from E2V camera. Reading from serial port {0}. Error code : {1}. Trial Number : {2}. Command sent to camera : {3}. Buffer response : {4} ",
                                                  portNumber, retVal, numberOfTrials, command, readBufferContents);
                        }
                        else
                        {
                            bufferData = readBufferContents;
                            commandProcessingSuccess = true;
                            cont = 'n';
                        }
                    }
                    else
                    {
                        commandProcessingSuccess = false;
                        bufferData = "";

                        error = string.Format("Error in getting response from E2V camera. Reading from serial port {0}. Error code : {1}. Trial Number : {2}. Command sent to camera : {3}",
                                              portNumber, retVal, numberOfTrials, command);
                    }
                }
            } while ((cont == 'y') && ((numberOfTrials++) < 3));
            err = error;
            return(commandProcessingSuccess);
        }