//获取GPS数据 private gpsdata getgps() { string constr = @"Data Source=NWTVOP0OWTDS4XE; Initial Catalog=WaterServer;Integrated Security=true;"; SqlConnection sqlcon; gpsdata gpsdata1 = new gpsdata(); try { sqlcon = new SqlConnection(); sqlcon.ConnectionString = constr; sqlcon.Open(); string sql = "select top 1 * from GpsData order by id desc";//在数据库中读取 SqlCommand cmd = new SqlCommand(sql, sqlcon); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { gpsdata1.Lng = Convert.ToDouble(reader[1]); gpsdata1.Lat = Convert.ToDouble(reader[2]); } sqlcon.Close(); reader.Close(); cmd.Dispose(); ShowMsg("获取的gps数据:" + gpsdata1.Lng + "," + gpsdata1.Lat); } catch { ShowMsg("gps出现异常"); } return(gpsdata1); }
void Receive(object o) { socketSend = o as Socket; while (true) { try { //客户端连接成功后,服务器接受客户端发送的消息 byte[] buffer = new byte[1024 * 1024 * 2]; //实际接受的有效字节数 int r = socketSend.Receive(buffer); if (r == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, r); ShowMsg(socketSend.RemoteEndPoint + "," + str); if (str.Contains('T'.ToString()) && str.Contains('H'.ToString()) && str.Contains("PH"))//转换接收数据 { char[] delimiter = { ' ' }; string[] sArray = str.Split(delimiter); temp = sArray[0].Substring(sArray[0].IndexOf(":") + 1, sArray[0].IndexOf("#") - sArray[0].IndexOf(":") - 1); hum = sArray[1].Substring(sArray[1].IndexOf(":") + 1, sArray[1].IndexOf("#") - sArray[1].IndexOf(":") - 1); ph = sArray[2].Substring(sArray[2].IndexOf(":") + 1, sArray[2].IndexOf("#") - sArray[2].IndexOf(":") - 1); temp1 = Convert.ToDouble(temp); hum1 = Convert.ToDouble(hum); ph1 = Convert.ToDouble(ph); ShowMsg("接受并保存数据:" + temp1 + "," + hum1 + "," + ph1 + " " + result2.ToString() + "\r\n"); result2 = Savedata(temp1, hum1, ph1); gpsdata gpsdata2 = getgps(); } else { ShowMsg("传输语句有问题"); } } catch { } } }