예제 #1
1
		private void AttemptConnect()
		{
			connected = false;

			UInt32 DeviceCount = 0;
			FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

			// Create new instance of the FTDI device class
			ftdi = new FTDI();

			// Determine the number of FTDI devices connected to the machine
			ftStatus = ftdi.GetNumberOfDevices( ref DeviceCount );

			// Check status
			if(ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0)
			{
				commStat = CommStatus.NoDevice;
				return;
			}

			commStat = CommStatus.NoElev8;

			// Allocate storage for device info list
			FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount];

			try
			{
				// Populate our device list
				ftStatus = ftdi.GetDeviceList( DeviceList );

				bool FoundElev8 = false;
				for(int i = 0; i < DeviceCount && FoundElev8 == false; i++)
				{
					if(DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES) continue;

					for(int baud = 0; baud < 2; baud++)
					{
						ftStatus = ftdi.OpenBySerialNumber( DeviceList[i].SerialNumber );
						if(ftStatus == FTDI.FT_STATUS.FT_OK)
						{
							string portName;
							ftdi.GetCOMPort( out portName );
							if(portName == null || portName == "")
							{
								ftdi.Close();
								continue;
							}

							if(baud == 0) {
								ftdi.SetBaudRate( 115200 );	// try this first
							}
							else {
								ftdi.SetBaudRate( 57600 );	// then try this (xbee)
							}

							ftdi.SetDataCharacteristics( 8, 1, 0 );
							ftdi.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0 );


							txBuffer[0] = (byte)'E';
							txBuffer[1] = (byte)'l';
							txBuffer[2] = (byte)'v';
							txBuffer[3] = (byte)'8';
							uint written = 0;

							for(int j = 0; j < 10 && FoundElev8 == false && !quit; j++)	// Keep pinging until it replies, or we give up
							{
								ftdi.Write( txBuffer, 4, ref written );
								System.Threading.Thread.Sleep( 50 );

								uint bytesAvail = 0;
								ftdi.GetRxBytesAvailable( ref bytesAvail );				// How much data is available from the serial port?
								if(bytesAvail > 0)
								{
									int TestVal = 0;

									while(bytesAvail > 0 && !quit)
									{
										uint bytesRead = 0;
										ftdi.Read( rxBuffer, 1, ref bytesRead );
										if(bytesRead == 1)
										{
											TestVal = (TestVal << 8) | rxBuffer[0];
											if(TestVal == (int)(('E' << 0) | ('l' << 8) | ('v' << 16) | ('8' << 24)) )
											{
												FoundElev8 = true;
												commStat = CommStatus.Connected;
												break;
											}
										}

										if(bytesRead == 0) break;
									}
								}
							}

							if(FoundElev8)
							{
								connected = true;
								if(ConnectionStarted != null) {
									ConnectionStarted();
								}
								break;
							}
							else
							{
								ftdi.Close();
							}
						}
					}
				}
			}

			catch(Exception)
			{
				return;
			}
		}
예제 #2
1
		private void AttemptConnect()
		{
			connected = false;

			UInt32 DeviceCount = 0;
			FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

			// Create new instance of the FTDI device class
			ftdi = new FTDI();

			// Determine the number of FTDI devices connected to the machine
			ftStatus = ftdi.GetNumberOfDevices( ref DeviceCount );

			// Check status
			if(ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0)
			{
				commStat = CommStatus.NoDevice;
				return;
			}

			commStat = CommStatus.NoElev8;

			// Allocate storage for device info list
			FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount];

			try
			{
				// Populate our device list
				ftStatus = ftdi.GetDeviceList( DeviceList );

				bool FoundElev8 = false;
				for(int i = 0; i < DeviceCount && FoundElev8 == false; i++)
				{
					if(DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES) continue;

					ftStatus = ftdi.OpenBySerialNumber( DeviceList[i].SerialNumber );
					if(ftStatus == FTDI.FT_STATUS.FT_OK)
					{
						string portName;
						ftdi.GetCOMPort( out portName );
						if(portName == null || portName == "")
						{
							ftdi.Close();
							continue;
						}

						ftdi.SetBaudRate( 115200 );
						ftdi.SetDataCharacteristics( 8, 1, 0 );
						ftdi.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0 );


						txBuffer[0] = (byte)0;		// Set it to MODE_None to stop it writing (reset in case it was disconnected)
						txBuffer[1] = (byte)0xff;	// Send 0xff to the Prop to see if it replies
						uint written = 0;

						for(int j = 0; j < 10 && FoundElev8 == false; j++)	// Keep pinging until it replies, or we give up
						{
							ftdi.Write( txBuffer, 2, ref written );
							System.Threading.Thread.Sleep( 50 );

							uint bytesAvail = 0;
							ftdi.GetRxBytesAvailable( ref bytesAvail );				// How much data is available from the serial port?
							if(bytesAvail > 0)
							{
								uint bytesRead = 0;
								ftdi.Read( rxBuffer, 1, ref bytesRead );			// If it comes back with 0xE8 it's the one we want
								if(bytesRead == 1 && rxBuffer[0] == 0xE8)
								{
									FoundElev8 = true;
									commStat = CommStatus.Connected;
									break;
								}
							}
						}

						if(FoundElev8) {
							connected = true;
							txBuffer[0] = 2;	// MODE_Sensors
							written = 0;
							ftdi.Write( txBuffer, 1, ref written );

							if(ConnectionStarted != null) {
								ConnectionStarted();
							}
							break;
						}
						else {
							ftdi.Close();
						}
					}
				}
			}

			catch(Exception)
			{
				return;
			}
		}
예제 #3
0
 void SendStatusRequest()
 {
     if (!connectionActive /*|| machineState == MachineState.Jog*/)
     {
         return;
     }
     try
     {
         if (requestFullStatus)
         {
             SendByte(CMD_STATUS_REQUEST_FULL);
             requestFullStatus = false;
             showStatusMsg++;
         }
         else
         {
             SendByte(CMD_STATUS_REQUEST);
         }
     }
     catch
     {
         ClosePort();
         ConnectionStatus = CommStatus.Disconnected;
         if (CommStatusChanged != null)
         {
             CommStatusChanged(this, ConnectionStatus);
         }
     }
 }
예제 #4
0
            public CommStatusUpdatedEventArgs(ErrorStatus error_status, CommStatus comm_status, CommStatus comm_status_old)
            {
                ErrorStatus = error_status;

                CommStatus     = comm_status;
                CommStatusMask = comm_status ^ comm_status_old;
            }
        }//SerialDataReceivedEventHandler

        #endregion



        #region DisplayFunctions
        private void CommWatcher_Show(Object sender, EventArgs e)
        {
            CommStatus state = (CommStatus)commWatcher.GetCommStatus();

            switch (state)
            {
            case CommStatus.Lost:
                commWatchStatus.Text      = "Lost";
                commWatchStatus.BackColor = Color.Red;
                this.GraphsButton.Enabled = false;
                FlashWindow.Flash(this);
                break;

            case CommStatus.Silent:
                commWatchStatus.Text      = "Waiting";
                commWatchStatus.BackColor = Color.Yellow;
                this.GraphsButton.Enabled = false;
                FlashWindow.Stop(this);
                break;

            case CommStatus.Working:
                commWatchStatus.Text      = "Receiving";
                commWatchStatus.BackColor = Color.LawnGreen;
                this.GraphsButton.Enabled = true;
                FlashWindow.Stop(this);
                break;
            }
        }  // CommWather_Show
예제 #6
0
파일: GrblComm.cs 프로젝트: Juank23/GrblCNC
 void SendStatusRequest()
 {
     if (!connectionActive /*|| machineState == MachineState.Jog*/)
     {
         return;
     }
     try
     {
         SendByte(CMD_STATUS_REQUEST);
     }
     catch
     {
         ClosePort();
         ConnectionStatus = CommStatus.Disconnected;
         if (StatusChanged != null)
         {
             StatusChanged(this, ConnectionStatus);
         }
     }
 }
예제 #7
0
 void HandleReconnection(string version)
 {
     driverVersion = version;
     activePort    = port.PortName;
     if (machineState == MachineState.Idle)
     {
         GetAllGrblParameters();
     }
     //machineState = MachineState.Idle;
     if (ConnectionStatus != CommStatus.Connected)
     {
         ConnectionStatus = CommStatus.Connected;
         grblStatus.state = GrblStatus.MachineState.Unknown;
         if (CommStatusChanged != null)
         {
             CommStatusChanged(this, ConnectionStatus);
         }
     }
     requestFullStatus = true;
 }
예제 #8
0
        /// <summary>
        /// 得到四个地面站无线网桥和8辆车的网络设备的通讯状态
        /// 需要注意的是:mainCanvas.Children在未添加CarComm时的Count=14,最大索引maxIndex=13;方法中出现的数字13即为此含义
        /// 数字8代表8辆车
        /// </summary>
        private void AssignCommStatus()
        {
            if (Communication.CommStatus.Count == 0)
            {
                for (int i = 0; i < 9; i++)
                {
                    Communication.CommStatus.Add(new CommStatus(new bool[] { false, false, false, false }));
                }
            }

            if (commHelper.SucceedCount[2] < 3)
            {//索引为2时对应的为无线网桥的状态计数
                Communication.CommStatus[0] = new CommStatus(commHelper.CommStatus);
            }
            for (int i = 0; i < 8; i++)
            {//添加8辆车的通讯状态
                CarComm    carComm = (CarComm)mainCanvas.Children[14 + i];
                CommStatus comm    = new CommStatus(carComm.CommStatus);
                Communication.CommStatus[i + 1] = comm;
            }
        }
예제 #9
0
        /// <summary>
        /// Updates the displayed information. Used to show every sent/received packet.
        /// </summary>
        /// <param name="p">Sent or received packet</param>
        /// <param name="status">Current status of the communication</param>
        public void updateCommUI(SerialComm.Packet_t p, CommStatus status)
        {
            // fix; this might have caused an IndexOutOfRangeException
            if (p.data != null && p.dataLength > p.data.Length)
            {
                p.dataLength = (ushort)p.data.Length;
            }

            // temporary fix of IndexOutOfRange exception
            //try
            //{
            // check if the packet should be ignored
            if (ignoreSeqNrQueue.Count >= 1)
            {
                if (ignoreSeqNrQueue.Peek() + 1 == p.sequenceNumber)
                {
                    ignoreSeqNrQueue.Dequeue();
                }
            }
            if (ignoreSeqNrQueue.Count >= 1)
            {
                if (ignoreSeqNrQueue.Peek() == p.sequenceNumber)
                {
                    return;
                }
            }

            // show the packet according to its status.
            switch (status)
            {
            case CommStatus.AckReceived:
                timeoutFlag = false;
                arrowArray[nextPictureBox].Image = Properties.Resources.arrow_left_green;
                flowCommStatus.Controls.Add(arrowArray[nextPictureBox]);
                toolTip.SetToolTip(arrowArray[nextPictureBox], "ack");
                arrowArray[nextPictureBox].Show();
                if (nextPictureBox < 5)
                {
                    nextPictureBox++;
                }
                break;

            case CommStatus.AckTimeout:
                if (timeoutFlag == false)
                {
                    arrowArray[nextPictureBox].Image = Properties.Resources.arrow_left_transparent;
                    flowCommStatus.Controls.Add(arrowArray[nextPictureBox]);
                    toolTip.SetToolTip(arrowArray[nextPictureBox], "ack timeout");
                    arrowArray[nextPictureBox].Show();
                    if (nextPictureBox < 5)
                    {
                        nextPictureBox++;
                    }
                    timeoutFlag = true;
                }
                break;

            case CommStatus.NackReceived:
                timeoutFlag = false;
                if (nextPictureBox < arrowArray.Length)
                {
                    arrowArray[nextPictureBox].Image = Properties.Resources.arrow_left_red;
                    flowCommStatus.Controls.Add(arrowArray[nextPictureBox]);
                    toolTip.SetToolTip(arrowArray[nextPictureBox], "nack");
                    arrowArray[nextPictureBox].Show();
                    nextPictureBox++;
                }
                break;

            case CommStatus.ResponseReceived:
                timeoutFlag = false;
                if (nextPictureBox < arrowArray.Length)
                {
                    arrowArray[nextPictureBox].Image = Properties.Resources.arrow_left_grey;
                    flowCommStatus.Controls.Add(arrowArray[nextPictureBox]);
                    toolTip.SetToolTip(arrowArray[nextPictureBox], "response");
                    arrowArray[nextPictureBox].Show();
                    nextPictureBox++;
                }
                fillInDetails(p);
                break;

            case CommStatus.ResponseTimeout:
                if (timeoutFlag == false && nextPictureBox < arrowArray.Length)
                {
                    arrowArray[nextPictureBox].Image = Properties.Resources.arrow_left_transparent;
                    flowCommStatus.Controls.Add(arrowArray[nextPictureBox]);
                    toolTip.SetToolTip(arrowArray[nextPictureBox], "response timeout");
                    arrowArray[nextPictureBox].Show();
                    nextPictureBox++;
                    timeoutFlag = true;
                }
                break;

            case CommStatus.PacketSent:
                // clear all displayed information.
                timeoutFlag    = false;
                nextPictureBox = 0;
                for (int i = 0; i < arrowArray.Length; i++)
                {
                    arrowArray[i].Hide();
                }

                clearDetails();
                fillOutDetails(p);

                if (nextPictureBox < arrowArray.Length)
                {
                    arrowArray[nextPictureBox].Image = Properties.Resources.arrow_left_grey;
                    arrowArray[nextPictureBox].Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    flowCommStatus.Controls.Add(arrowArray[nextPictureBox]);
                    toolTip.SetToolTip(arrowArray[nextPictureBox], "command/request");
                    arrowArray[nextPictureBox].Show();
                    nextPictureBox++;
                }
                break;

            default:
                break;
            }

            // close the expanded view.
            if (CmdExpanded == true)
            {
                btnExpand_Click(this, new EventArgs());
            }

            //}
            //catch { }
        }
예제 #10
0
파일: GrblComm.cs 프로젝트: Juank23/GrblCNC
        void HandleReceivedLine(string line)
        {
            if (line.Length == 0)
            {
                return;
            }
            bool isStatusLine = false;

            if (line.Length > maxlinelen)
            {
                maxlinelen  = line.Length;
                tmpLongLine = line;
            }
            if (line.StartsWith("Grbl"))
            {
                activePort = port.PortName;
                if (machineState == MachineState.Idle)
                {
                    GetAllGrblParameters();
                }
                //machineState = MachineState.Idle;
                if (ConnectionStatus != CommStatus.Connected)
                {
                    ConnectionStatus = CommStatus.Connected;
                    if (StatusChanged != null)
                    {
                        StatusChanged(this, ConnectionStatus);
                    }
                }
            }
            else if (line.StartsWith("<"))
            {
                isStatusLine = true;
                grblStatus.Parse(line);
                if (StatusUpdate != null)
                {
                    StatusUpdate(this, grblStatus);
                }
            }
            else if (line.StartsWith("$"))
            {
                HandleParamLine(line);
            }
            else if (line.StartsWith("ok"))
            {
                HandleOKLine(line);
            }
            else if (line.StartsWith("["))
            {
                HandleMessageLine(line);
            }
            else if (line.StartsWith("error"))
            {
                HandleErrorLine(line, MessageType.Error);
            }
            else if (line.StartsWith("ALARM"))
            {
                HandleErrorLine(line, MessageType.Alarm);
            }
            if (LineReceived != null)
            {
                LineReceived(this, line, isStatusLine && showStatusMsg == 0);
                if (showStatusMsg > 0)
                {
                    showStatusMsg--;
                }
            }
        }
        private void AttemptConnect()
        {
            connected = false;

            string[] names = SerialPort.GetPortNames();

            List <string> Ports = new List <string>();

            foreach (string name in names)
            {
                if (name != "COM1" && name != "COM2" && name != "COM3" && name != "COM4")
                {
                    Ports.Add(name);
                }
            }

            // Check status
            if (Ports.Count == 0)
            {
                commStat = CommStatus.NoDevice;
                return;
            }

            commStat = CommStatus.NoElev8;

            try
            {
                bool FoundElev8 = false;
                for (int i = 0; i < Ports.Count && FoundElev8 == false; i++)
                {
                    serial = new SerialPort(Ports[i], 115200, Parity.None, 8, StopBits.One);
                    serial.Open();

                    txBuffer[0] = (byte)'E';
                    txBuffer[1] = (byte)'l';
                    txBuffer[2] = (byte)'v';
                    txBuffer[3] = (byte)'8';

                    for (int j = 0; j < 10 && FoundElev8 == false; j++)                         // Keep pinging until it replies, or we give up
                    {
                        serial.Write(txBuffer, 0, 4);
                        System.Threading.Thread.Sleep(50);

                        int bytesAvail = serial.BytesToRead;
                        if (bytesAvail > 0)
                        {
                            int TestVal = 0;

                            while (bytesAvail > 0)
                            {
                                int bytesRead = serial.Read(rxBuffer, 0, 1);
                                if (bytesRead == 1)
                                {
                                    TestVal = (TestVal << 8) | rxBuffer[0];
                                    if (TestVal == (int)(('E' << 0) | ('l' << 8) | ('v' << 16) | ('8' << 24)))
                                    {
                                        FoundElev8 = true;
                                        commStat   = CommStatus.Connected;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (FoundElev8)
                    {
                        connected = true;
                        if (ConnectionStarted != null)
                        {
                            ConnectionStarted();
                        }
                        break;
                    }
                    else
                    {
                        serial.Close();
                        serial.Dispose();
                        serial = null;
                    }
                }
            }

            catch (Exception)
            {
                return;
            }
        }
예제 #12
0
 public virtual void NotifyError(CommStatus errorStatus, String errorMessage)
 {
     GecoSiLogger.Error(errorMessage);
     siListener.Notify(errorStatus, errorMessage);
 }
예제 #13
0
 public virtual void Notify(CommStatus status)
 {
     GecoSiLogger.Log("!", status.GetType().Name);
     siListener.Notify(status);
 }
		private void AttemptConnect()
		{
			connected = false;

			string[] names = SerialPort.GetPortNames();

			List<string> Ports = new List<string>();
			foreach(string name in names)
			{
				if(name != "COM1" && name != "COM2" && name != "COM3" && name != "COM4")
					Ports.Add( name );
			}

			// Check status
			if(Ports.Count == 0)
			{
				commStat = CommStatus.NoDevice;
				return;
			}

			commStat = CommStatus.NoElev8;

			try
			{

				bool FoundElev8 = false;
				for(int i = 0; i < Ports.Count && FoundElev8 == false; i++)
				{
					serial = new SerialPort( Ports[i], 115200, Parity.None, 8, StopBits.One );
					serial.Open();

					txBuffer[0] = (byte)'E';
					txBuffer[1] = (byte)'l';
					txBuffer[2] = (byte)'v';
					txBuffer[3] = (byte)'8';

					for(int j = 0; j < 10 && FoundElev8 == false; j++)	// Keep pinging until it replies, or we give up
					{
						serial.Write( txBuffer, 0, 4 );
						System.Threading.Thread.Sleep( 50 );

						int bytesAvail = serial.BytesToRead;
						if(bytesAvail > 0)
						{
							int TestVal = 0;

							while(bytesAvail > 0)
							{
								int bytesRead = serial.Read( rxBuffer, 0, 1 );
								if(bytesRead == 1)
								{
									TestVal = (TestVal << 8) | rxBuffer[0];
									if(TestVal == (int)(('E' << 0) | ('l' << 8) | ('v' << 16) | ('8' << 24)))
									{
										FoundElev8 = true;
										commStat = CommStatus.Connected;
										break;
									}
								}
							}
						}
					}

					if(FoundElev8) {
						connected = true;
						if(ConnectionStarted != null) {
							ConnectionStarted();
						}
						break;
					}
					else {
						serial.Close();
						serial.Dispose();
						serial = null;
					}
				}
			}

			catch(Exception)
			{
				return;
			}
		}
예제 #15
0
		void ConnectFTDI()
		{
			UInt32 DeviceCount = 0;
			FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

			// Create new instance of the FTDI device class
			ftdi = new FTDI();

			// Determine the number of FTDI devices connected to the machine
			ftStatus = ftdi.GetNumberOfDevices( ref DeviceCount );

			// Check status
			if(ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0) {
				commStat = CommStatus.NoDevices;
				return;
			}

			commStat = CommStatus.NoElev8;

			// Allocate storage for device info list
			FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount];

			try
			{
				// Populate our device list
				ftStatus = ftdi.GetDeviceList( DeviceList );

				bool FoundElev8 = false;
				for(int i = 0; i < DeviceCount && FoundElev8 == false; i++)
				{
					if(DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES) continue;

					ftStatus = ftdi.OpenBySerialNumber( DeviceList[i].SerialNumber );
					if(ftStatus == FTDI.FT_STATUS.FT_OK)
					{
						string portName;
						ftdi.GetCOMPort( out portName );
						if(portName == null || portName == "")
						{
							ftdi.Close();
							continue;
						}

						ftdi.SetBaudRate( 115200 );
						ftdi.SetDataCharacteristics( 8, 1, 0 );
						ftdi.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0 );


						txBuffer[0] = (byte)0xff;	// Send 0xff to the Prop to see if it replies
						uint written = 0;

						for(int j = 0; j < 10 && FoundElev8 == false; j++)	// Keep pinging until it replies, or we give up
						{
							ftdi.Write( txBuffer, 1, ref written );
							System.Threading.Thread.Sleep( 50 );

							uint bytesAvail = 0;
							ftdi.GetRxBytesAvailable( ref bytesAvail );				// How much data is available from the serial port?
							if(bytesAvail > 0)
							{
								uint bytesRead = 0;
								ftdi.Read( rxBuffer, 1, ref bytesRead );			// If it comes back with 0xE8 it's the one we want
								if(bytesRead == 1 && rxBuffer[0] == 0xE8)
								{
									FoundElev8 = true;
									commStat = CommStatus.Connected;
									break;
								}
							}
						}
						if(FoundElev8)
						{
							break;
						}
						else
						{
							ftdi.Close();
						}
					}
				}
			}

			catch(Exception) {
				return;
			}


			Active = true;
			if( ftdi.IsOpen ) {
				currentMode = (Mode)(tcMainTabs.SelectedIndex + 1);
				txBuffer[0] = (byte)currentMode;
				uint written = 0;
				ftdi.Write( txBuffer, 1, ref written );	// Which mode are we in?
			}

			// Start my 'tick timer' - It's set to tick every 20 milliseconds
			// (used to check the comm port periodically instead of using a thread)
			//tickTimer.Start();
		}
예제 #16
0
        private static extern bool ClearCommError(int hFile, out uint lpErrors,
			out CommStatus lpStat);
예제 #17
0
		void UpdateCommStatus()
		{
			if(commStat != lastStat)
			{
				string msg = null;

				lastStat = commStat;
				switch(commStat)
				{
					case CommStatus.Initializing:
						msg = "Initializing";
						break;

					case CommStatus.NoDevices:
						msg = "No FTDI devices found";
						break;

					case CommStatus.NoElev8:
						msg = "Looking for Elev8-FC";
						break;

					case CommStatus.Connected:
						msg = "Connected";
						break;
				}

				tsStatLabel.Text = msg;

				if(commStat == CommStatus.Connected)
				{
					tickTimer.Interval = 20;
				}
				else
				{
					tickTimer.Interval = 100;
				}
			}
		}
        private void AttemptConnect()
        {
            connected = false;

            UInt32 DeviceCount = 0;

            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

            // Create new instance of the FTDI device class
            ftdi = new FTDI();

            // Determine the number of FTDI devices connected to the machine
            ftStatus = ftdi.GetNumberOfDevices(ref DeviceCount);

            // Check status
            if (ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0)
            {
                commStat = CommStatus.NoDevice;
                return;
            }

            commStat = CommStatus.NoElev8;

            // Allocate storage for device info list
            FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount];

            try
            {
                // Populate our device list
                ftStatus = ftdi.GetDeviceList(DeviceList);

                bool FoundElev8 = false;
                for (int i = 0; i < DeviceCount && FoundElev8 == false; i++)
                {
                    if (DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES)
                    {
                        continue;
                    }

                    ftStatus = ftdi.OpenBySerialNumber(DeviceList[i].SerialNumber);
                    if (ftStatus == FTDI.FT_STATUS.FT_OK)
                    {
                        string portName;
                        ftdi.GetCOMPort(out portName);
                        if (portName == null || portName == "")
                        {
                            ftdi.Close();
                            continue;
                        }

                        ftdi.SetBaudRate(115200);
                        ftdi.SetDataCharacteristics(8, 1, 0);
                        ftdi.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0);


                        txBuffer[0] = (byte)0;                                  // Set it to MODE_None to stop it writing (reset in case it was disconnected)
                        txBuffer[1] = (byte)0xff;                               // Send 0xff to the Prop to see if it replies
                        uint written = 0;

                        for (int j = 0; j < 10 && FoundElev8 == false; j++)                             // Keep pinging until it replies, or we give up
                        {
                            ftdi.Write(txBuffer, 2, ref written);
                            System.Threading.Thread.Sleep(50);

                            uint bytesAvail = 0;
                            ftdi.GetRxBytesAvailable(ref bytesAvail);                                                           // How much data is available from the serial port?
                            if (bytesAvail > 0)
                            {
                                uint bytesRead = 0;
                                ftdi.Read(rxBuffer, 1, ref bytesRead);                                                          // If it comes back with 0xE8 it's the one we want
                                if (bytesRead == 1 && rxBuffer[0] == 0xE8)
                                {
                                    FoundElev8 = true;
                                    commStat   = CommStatus.Connected;
                                    break;
                                }
                            }
                        }

                        if (FoundElev8)
                        {
                            connected   = true;
                            txBuffer[0] = 2;                                    // MODE_Sensors
                            written     = 0;
                            ftdi.Write(txBuffer, 1, ref written);

                            if (ConnectionStarted != null)
                            {
                                ConnectionStarted();
                            }
                            break;
                        }
                        else
                        {
                            ftdi.Close();
                        }
                    }
                }
            }

            catch (Exception)
            {
                return;
            }
        }
        private void AttemptConnect()
        {
            connected = false;

            UInt32 DeviceCount = 0;

            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

            // Create new instance of the FTDI device class
            ftdi = new FTDI();

            // Determine the number of FTDI devices connected to the machine
            ftStatus = ftdi.GetNumberOfDevices(ref DeviceCount);

            // Check status
            if (ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0)
            {
                commStat = CommStatus.NoDevice;
                return;
            }

            commStat = CommStatus.NoElev8;

            // Allocate storage for device info list
            FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount];

            try
            {
                // Populate our device list
                ftStatus = ftdi.GetDeviceList(DeviceList);

                bool FoundElev8 = false;
                for (int i = 0; i < DeviceCount && FoundElev8 == false; i++)
                {
                    if (DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES)
                    {
                        continue;
                    }

                    for (int baud = 0; baud < 2; baud++)
                    {
                        ftStatus = ftdi.OpenBySerialNumber(DeviceList[i].SerialNumber);
                        if (ftStatus == FTDI.FT_STATUS.FT_OK)
                        {
                            string portName;
                            ftdi.GetCOMPort(out portName);
                            if (portName == null || portName == "")
                            {
                                ftdi.Close();
                                continue;
                            }

                            if (baud == 0)
                            {
                                ftdi.SetBaudRate(115200);                                       // try this first
                            }
                            else
                            {
                                ftdi.SetBaudRate(57600);                                        // then try this (xbee)
                            }

                            ftdi.SetDataCharacteristics(8, 1, 0);
                            ftdi.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0);


                            txBuffer[0] = (byte)'E';
                            txBuffer[1] = (byte)'l';
                            txBuffer[2] = (byte)'v';
                            txBuffer[3] = (byte)'8';
                            uint written = 0;

                            for (int j = 0; j < 10 && FoundElev8 == false && !quit; j++)                                // Keep pinging until it replies, or we give up
                            {
                                ftdi.Write(txBuffer, 4, ref written);
                                System.Threading.Thread.Sleep(50);

                                uint bytesAvail = 0;
                                ftdi.GetRxBytesAvailable(ref bytesAvail);                                                               // How much data is available from the serial port?
                                if (bytesAvail > 0)
                                {
                                    int TestVal = 0;

                                    while (bytesAvail > 0 && !quit)
                                    {
                                        uint bytesRead = 0;
                                        ftdi.Read(rxBuffer, 1, ref bytesRead);
                                        if (bytesRead == 1)
                                        {
                                            TestVal = (TestVal << 8) | rxBuffer[0];
                                            if (TestVal == (int)(('E' << 0) | ('l' << 8) | ('v' << 16) | ('8' << 24)))
                                            {
                                                FoundElev8 = true;
                                                commStat   = CommStatus.Connected;
                                                break;
                                            }
                                        }

                                        if (bytesRead == 0)
                                        {
                                            break;
                                        }
                                    }
                                }
                            }

                            if (FoundElev8)
                            {
                                connected = true;
                                if (ConnectionStarted != null)
                                {
                                    ConnectionStarted();
                                }
                                break;
                            }
                            else
                            {
                                ftdi.Close();
                            }
                        }
                    }
                }
            }

            catch (Exception)
            {
                return;
            }
        }
예제 #20
0
 public void Notify(CommStatus errorStatus, String errorMessage)
 {
     Console.WriteLine("Error -> " + errorStatus + " " + errorMessage);
 }
예제 #21
0
 public void Notify(CommStatus status)
 {
     Console.WriteLine("Status" + DateTime.Now.ToString("hh:mm:ss.fff") + " -> " + status);
 }