using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Net.Sockets;
using System.Xml;
using System.Net;
using System.IO;
namespace 实验提取GPS数据1
{
public partial class Form1 : Form
{
string userName;
string passWord;
string IDcard;
string trueName;
string longitude;
string latitude;
bool finding = false;
string ip; //Ip地址
int port; //端口号
TcpClient tcpclient; //定义一个客户端
NetworkStream ns; // 提供用于网络访问的基础数据流。
StreamReader sr; // 实现一个 System.IO.TextReader,使其以一种特定的编码从字节流中读取字符。
StreamWriter sw; // 实现一个 System.IO.TextReader,使其以一种特定的编码向字节流中写入字符。
public Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
SerialPort sp = new SerialPort();
//private void button1_Click(object sender, EventArgs e)
//{
// //sp.Close();
// sp.PortName = "COM8";
// sp.BaudRate = 38400;
// sp.Open();
//}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
sp.PortName = "COM8"; //端口
sp.BaudRate = 38400; //波特率
sp.Open(); //打开串口
bool stop = false; //标志位
/*GPS数据*/
longitude = "0";//经度
latitude = "0";//纬度
while (!stop)
{
try
{
string message = sp.ReadLine();
message = message.Substring(0, message.Length - sp.NewLine.Length);
if (message.StartsWith("$GPGGA"))
{
//模拟定位
//message = "$GPGGA,012440.00,2232.458380,N,11356.91024,E,1,05,2.7,40.2,M,0.5,M,,*6F";
//message = "$GPGGA,012440.00,2232.301654,N,11402.058366,E,1,05,2.7,40.2,M,0.5,M,,*6F";
char[] separator = { ',' };
string[] data = message.Split(separator);
if (data[6].Equals("1"))
{
string s;
s = data[2];
int idx = s.IndexOf(".");
string d1 = s.Substring(0, idx - 2);
string d2 = s.Substring(idx - 2);
double degree = Double.Parse(d1) + Double.Parse(d2) / 60;
latitude = Convert.ToString(Convert.ToDouble(s) / 100);//纬度
string j;
j = data[4];
int idxj = j.IndexOf(".");
string d1j = j.Substring(0, idxj - 2);
string d2j = j.Substring(idxj - 2);
double degreej = Double.Parse(d1j) + Double.Parse(d2j) / 60;
longitude = Convert.ToString(Convert.ToDouble(j)/100);//经度
textBox1.Text = longitude;
textBox2.Text = latitude;
stop = true;
}
else
{
sp.Close();
sp.PortName = "COM8";
sp.BaudRate = 38400;
sp.Open();
// button2_Click(button2,EventArgs e);
}
}
else if (message.StartsWith("$GPGGA"))
{
char[] separator = { ',' };
string[] data = message.Split(separator);
}
}
catch (TimeoutException) { }
}
sp.Close();
// sp = null;
//textBox4.Text = adrss();2
MessageBox.Show("GPS已停止,请等待地址信息的查询!");
if (finding == false)
new Thread(find).Start();
//find();
}
private void find()
{
if (finding == false)
{
finding = true;
txtAdrss.Text = GPSadrss.adrss(latitude, longitude);
//Thread.CurrentThread.Abort();
}
}
private void button1_Click(object sender, EventArgs e)
{
//if (cheakRegister() == true)
//{
ip = "192.168.191.1"; //这是自己电脑的ip
port = 8080;
tcpclient = new TcpClient();
tcpclient.Connect(ip, 8080); //连接到服务器的8080端口
ns = tcpclient.GetStream();// 返回用于发送和接收数据的 System.Net.Sockets.NetworkStream。
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
new Thread(run).Start();
//string register = userName + passWord + IDcard;
//sw.WriteLine(register);
////this.richTxtSend.AppendText("用户A:" + tt + "\r\n");
//sw.Flush();
//}
}
private void run()
{
while (true)
{
string rec = sr.ReadLine();
this.richTextBox1.AppendText("服务器:" + rec + "\r\n");
}
}
private void btnSend_Click(object sender, EventArgs e)
{
string tt = this.richTextBox1.Text;
sw.WriteLine(tt);
this.richTextBox1.AppendText("服务器:" + tt + "\r\n");
sw.Flush(); //清理当前编写器的所有缓冲区,并使所有缓冲数据写入基础流。
}
//验证姓名
if (txtRegName.Text.Length >= 2 && txtRegName.Text.Length <= 8)
{
trueName = txtRegName.Text;
}
else
{
MessageBox.Show("用户名长度不符合要求,请重新输入!");
txtRegName.Text = "";
return false;
}
return true;
}
private void btnReg_Click(object sender, EventArgs e)
{
if (cheakRegister() == true && txtAdrss.Text!="")
{
ip = "192.168.191.1"; //这是自己电脑的ip
port = 8080;
tcpclient = new TcpClient();
tcpclient.Connect(ip, 8080); //连接到服务器的8080端口
ns = tcpclient.GetStream();// 返回用于发送和接收数据的 System.Net.Sockets.NetworkStream。
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
new Thread(run).Start();
string register = "register" + "," + userName + "," + passWord + "," + IDcard + "," + txtAdrss.Text;
sw.WriteLine(register);
//this.richTxtSend.AppendText("用户A:" + tt + "\r\n");
sw.Flush();
}
}
}
}
程序需要先点button2,然后在点btnReg
|