Jump to content

GPS - Acesso negado a porta COM


Recommended Posts

Posted

Boas!

Estou a desenvolver uma aplicação que tenho de receber a latitude e longitude através de um GPS que tenho (MSI SF100 Bluetooth Receiver).

O GPS é emparelhado por Bluetooth, na porta de serie COM4.

O problema é que, sempre que tento obter dados, diz que não tenho acesso a porta COM4.

http://www.alojaimagens.com/images/m89v00u5ybw58srbq9c.png

Ajuda sff.

EDIT: "Resolvido!" Só que agora obtenho sempre GPS Unavailable. Sinceramente, não percebo porquê.  😛

Código para obter coordenadas:

O que obtemos no data(ligação com o aparelho na porta de serie COM4):

data = "7A\r\n$GPGGA,001457.944,0000.0000,N,00000.0000,E,0,00,50.0,,M,,,,0000*1B\r\n$GPGSA,A,1,,,,,,,,,,,,,50.0,50.0,50.0*05\r\n$GPRMC,001457.944,V,0000.0000,N,00000.0000,E,,,020504,,,N*7B\r\n$GPGGA
 
/// <summary>
        /// Try to update present position if the port is configured correctly
        /// and the GPS device is returning values
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (serialPort1.IsOpen)
            {
                string data = serialPort1.ReadExisting();
                string[] strArr = data.Split('$');
                for (int i = 0; i < strArr.Length; i++)
                {
                    string strTemp = strArr[i];
                    string[] lineArr = strTemp.Split(',');
                    if (lineArr[0] == "GPGGA")
                    {

                        try
                        {
                            //Latitude
                            Double dLat = Convert.ToDouble(lineArr[2]);
                            dLat = dLat / 100;
                            string[] lat = dLat.ToString().Split('.');
                            Latitude = lineArr[3].ToString() + lat[0].ToString() + "." + ((Convert.ToDouble(lat[1]) / 60)).ToString("#####");

                            //Longitude
                            Double dLon = Convert.ToDouble(lineArr[4]);
                            dLon = dLon / 100;
                            string[] lon = dLon.ToString().Split('.');
                            Longitude = lineArr[5].ToString() + lon[0].ToString() + "." + ((Convert.ToDouble(lon[1]) / 60)).ToString("#####");

                            //Display
                            txtLat.Text = Latitude;
                            txtLong.Text = Longitude;

                            btnMapIt.Enabled = true;
                        }
                        catch
                        {
                            //Can't Read GPS values
                            txtLat.Text = "GPS Unavailable";
                            txtLong.Text = "GPS Unavailable";
                            btnMapIt.Enabled = false;
                        }
                    }
                }

            }
            else
            {
                txtLat.Text = "COM Port Closed";
                txtLong.Text = "COM Port Closed";
                btnMapIt.Enabled = false;
            }
        }
 

Solução do primeiro problema:

using System.Threading;

        /// <summary>
        /// Swap serialPort1 to port COM4
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Close();
                Thread.Sleep(2000); //Esperar um pouco pelo fecho da porta (linha cima)
                serialPort1.PortName = "COM4";
                serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "COM4");
            }
        }
  • 4 weeks later...
Posted

Mas queres que te ajudem em quê?

Tens no tópico a dizer que está resolvido...

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.