Xây dựng giao diện người dùng bằng Microsoft Visual Studio

Một phần của tài liệu QUẢN LÝ XE SỬ DỤNG MODULE SIM908 VÀ GOOGLE MAP (code Csharp) (Trang 50)

6.2.1 Giao tiếp SIM908 bằng PC qua UART –gửi/ nhận dữ liệu qua UART:

Click phải vào biểu tượng Computer chọn Properties Device Manager Thẻ Ports(COM & LTP) để xác định cổng COM đang kết nối vào máy tính

Hình 24: xác định đúng cổng COM

Thiết kế giao diện winform C# như sau:

Hình 25: Giao tiếp Uart bằng C#

- ComboBox: chứa tất cả các cổng COM được kết nối vào máy tính ⇨ cho phép chọn cổng COM đã xác định đúng

- Button Connect: Sau khi chọn đúng cổng COM nhấn nút Connect để kết nói COM port, lúc này nhãn Disconnect sẽ đổi sang Connect, và text trên button Connect sẽ đổi thành Disconnect

- Button Send để gửi lệnh AT xuống Uart

- TextBox để hiện thị phản hồi từ SIM908 gửi lên

Hình 26: Chọn cổng COM cần kết nối

Hình 27: gửi lệnh AT xuống Uart

Ghi chú: phần giao tiếp Uart này em chỉ làm được chọn cổng COM, kết nối mà chưa gửi lệnh hay nhận phản hồi từ SIM908 được mà vẫn chưa biết nguyên nhân

Code chương trình:

using System;

using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

using System.IO.Ports;//them thu vien de su dung serial port

namespace WindowsFormsApplication8 {

publicpartialclassForm1 : Form

{

public Form1() {

InitializeComponent(); }

privatevoid Form1_Load(object sender, EventArgs e)

{ }

int intlen = 0;//intlen bang tong so com port

privatevoid timer1_Tick(object sender, EventArgs e)

{

//code dùng timer tu dong cap nhat Com ket noi

string[] ports = SerialPort.GetPortNames();//lay tat ca cac cong Com

if(intlen != ports.Length)//intlen khac so Com lay ve

{

intlen=ports.Length;

cbb_Select_Com.Items.Clear();//clear combobox

for (int j = 0; j<intlen; j++)//combobox cap nhat lai cong Com

{

cbb_Select_Com.Items.Add(ports[j]); }

} }

privatevoid bt_Send_Data_Click(object sender, EventArgs e)

{ string s; if(lb_Status.Text=="connect") { s = txt_Send_Data.Text; Com.WriteLine(s); } }

privatevoid bt_Connect_Click(object sender, EventArgs e)

{

if(lb_Status.Text =="Disconnect")//trang thai ban dau cho la disconnect

{

Com.PortName = cbb_Select_Com.Text;//cong com bang cong com da chon o combobox

Com.Open();

lb_Status.Text="Connect";//doi text cua label bt_Connect.Text="Disconnect";//doi text cua button } else { Com.Close(); lb_Status.Text="Disconnect"; bt_Connect.Text="Connect"; }

}

privatevoid OnCom(object sender, SerialDataReceivedEventArgs e)

{

//doc 1 byte se ngat Com, thiet lap trong receivebytethrehold=1 string s;

s=Com.ReadExisting();//doc tat ca du lieu trong buff com Display(s);

//Display(s) thay cho txt_Receive_Data.Text = s de bi loi }

// chuong tring con Display

privatedelegatevoidDlDisplay(string s);

privatevoid Display(string s) {

if(txt_Receive_Data.InvokeRequired) {

DlDisplay sd = newDlDisplay(Display);

txt_Receive_Data.Invoke( sd, newobject [] {s}); } else { txt_Receive_Data.Text = s; } } } }

6.2.2: Xây dựng cơ sở dữ liệu chứa thông tin xe bus:

Sử dụng chương trình Microsoft SQL Server Management để tạo một bảng chứa thông tin xe bus:

- STT: số thứ tự

- Phone: số điện thoại liên lạc với xe bus

- Name: tên tài xế

- BusID: mã số xe bus

- Start: điểm xuất phát

- Finish: điểm đến

- Street: lộ trình

Hình 28: tạo bảng database

Tạo winform C# có tên là form3 để kết nối dữ liệu trong SQL Server và Visua Studio C#

Hình 30:Add một DataSet là mô hình ánh xạ cua database SQL

Hình 32: Lấy dữ liệu từ DataSet lên form

Viết chương trình tìm kiếm dữ liệu từ bảng và quay về form chính

- Tạo 1 ComboBox chứa các trường để lựa chọn tìm kiếm

- Tạo 1 TextBox để người dùng nhập trong tin tìm kiếm

- Tạo 1 button FIND để tìm kiếm dữ liệu

- Tạo 1 button BACK để quay về form chính

Hình 34: ComboBox, TextBox, Button thiết kế như sau

Viết code cho chương trình và chạy thử:

Hình 36: Nhấn nút FIND và giá trị trả về

Code của chương trình: 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; namespace DOAN {

publicpartialclassForm3 : Form

{

public Form3() {

InitializeComponent(); }

privatevoid table_1BindingNavigatorSaveItem_Click(object sender, EventArgs e)

{

this.Validate();

this.table_1BindingSource.EndEdit();

this.tableAdapterManager.UpdateAll(this.dataSet1); }

privatevoid Form1_Load(object sender, EventArgs e)

{

// TODO: This line of code loads data into the 'dataSet1.Table_1' table. You can move, or remove it, as needed.

this.table_1TableAdapter.Fill(this.dataSet1.Table_1); }

privatevoid btnTimKiem_Click(object sender, EventArgs e)

{ string str = txtChuoiTimKiem.Text; switch (cbCot.Text) { case"*": table_1BindingSource.Filter = null; break; case"STT":

table_1BindingSource.Filter = String.Format("STT = {0}", str);// STT dang int dung toan tu “=”

break;

case"Phone":

table_1BindingSource.Filter = String.Format("Phone LIKE '%{0}'", str);

break; case"Name":

table_1BindingSource.Filter = String.Format("Name LIKE '%{0}'", str);//NAME dang nvarchar dùng toan tu LIKE

break;

case"BusID":

table_1BindingSource.Filter = String.Format("BusID = {0}", str);

break;

table_1BindingSource.Filter = String.Format("Start LIKE '%{0}'", str);

break;

case"Finish":

table_1BindingSource.Filter = String.Format("Finish LIKE '%{0}'", str);

break;

case"Street":

table_1BindingSource.Filter = String.Format("Street LIKE '%{0}'", str);

break; case"":

MessageBox.Show("Bạn chua chọn 'tùy chọn' tìm kiếm !", "THÔNG BÁO",

MessageBoxButtons.OK, MessageBoxIcon.Warning);

break; default: break;

} }

privatevoid toolStripButton1_Click(object sender, EventArgs e)

{

this.Hide();//an form data quay ve form chinh`

}

} }

6.2.3 Hiển thị google map trong Microsoft Visual Studio C#

Tạo 1 một winform như sau:

Hình 37: form để hiện thi tọa độ lên google map

- Button OPEN: dùng để mở file BusID101.txt ( ở đây giả sử có được file BusID101.txt lưu lại tất cả các tọa độ trên đường đi do SIM908 kết nối MCU lưu lại vào SD Card)

- Button PLAY: hiện thị tọa độ trong BusID101.txt lên google map

- Button BACK: trở lại form chính

- WebBrowser: môi trường để hiện thị google map trong C#  Viết code và chạy thử chương trình:

Hình 39: thông báo đọc file BusID101 thành công

Hình 40: nhấn nut button và tọa độ sẽ hiện thị trên googlemap trong winform C#

Code chương trình: 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;// dùng làm việc với filestream

using System.Globalization; using System.Threading; namespace DOAN

{

publicpartialclassForm1 : Form

{

List<string> RawStringsFromFile;

string FileCoordinates, FileSource = "";

OpenFileDialog Open = newOpenFileDialog();

public Form1() {

InitializeComponent(); }

privatevoid button2_Click(object sender, EventArgs e)

{

{

//doc file source vao

FileStream fs; StreamReader sr; fs = File.OpenRead(Open.FileName); sr = newStreamReader(fs); FileCoordinates = sr.ReadToEnd(); sr.Close(); fs.Close();

ConvertGPStoWGS84();// chuong trinh con chuyen GPS sang WGS84 string path = @".\source.html";

FileStream fs1; StreamReader sr1; fs1 = File.OpenRead(path); sr1 = newStreamReader(fs1); FileSource = sr1.ReadToEnd(); sr1.Close(); fs1.Close();

string s1 = FileSource.Substring(0, FileSource.LastIndexOf("center: new

google.maps.LatLng(")); s1 += "center: " + ConvertDataLine(RawStringsFromFile[0]) + "," + Environment.NewLine + " " + FileSource.Substring(FileSource.LastIndexOf("mapTypeId: google.maps.MapTypeId.ROADMAP"),

FileSource.LastIndexOf("var flightPlanCoordinates = [") - FileSource.LastIndexOf("mapTypeId: google.maps.MapTypeId.ROADMAP")); s1 += "var flightPlanCoordinates = [\n" + FileCoordinates +

"\n ";

string s2 = FileSource.Substring(FileSource.LastIndexOf("];"));

path = @".\Display.html";

StreamWriter StreamW = newStreamWriter(path);//ghi ra flie display

StreamW.Write(s1 + s2); StreamW.Close();

MessageBox.Show("" + "\n" + "THÀNH CÔNG", "THÔNG BÁO", MessageBoxButtons.OK,

MessageBoxIcon.Information);

// thong bao update thanh cong }

}

privatestring ConvertLatitude(string str)//doi vi do

{

returnMath.Round((double.Parse(str.Substring(0, 2))

+ (double.Parse(str.Substring(2, str.Length - 2)) / 60))

, 15, MidpointRounding.ToEven).ToString(); }

privatestring ConvertLongitude(string str)// doi kinh do

{

returnMath.Round((double.Parse(str.Substring(0, 3))

+ (double.Parse(str.Substring(3, str.Length - 3)) / 60))

, 15, MidpointRounding.ToEven).ToString(); }

privatestring ConvertDataLine(string str)//bien doi chuoi

{

string tempString = "", latitude = "", longitude = ""; if (str != "")

{

string[] DatasInLine = str.Split(',');//tach chuoi o vi tri dau ,

tempString = "new google.maps.LatLng("; latitude = ConvertLatitude(DatasInLine[0]); if (str.Contains('S')) latitude = "-" + latitude;

longitude = ConvertLongitude(DatasInLine[2]); if (str.Contains('W')) longitude = "-" + longitude;

tempString = tempString + latitude + "," + longitude + ")"; } return tempString; } privatevoid ConvertGPStoWGS84() { RawStringsFromFile = FileCoordinates.Split(newchar[] { '\n', '\r' }).ToList(); int countEmptyString = 0;

foreach (string str in RawStringsFromFile) if (str == "") ++countEmptyString;

for (int i = 0; i < countEmptyString; i++) RawStringsFromFile.Remove(""); FileCoordinates = "";

for (int i = 0; i < RawStringsFromFile.Count - 1; i++) { FileCoordinates += ConvertDataLine(RawStringsFromFile[i]) + "," + Environment.NewLine; } FileCoordinates += ConvertDataLine(RawStringsFromFile[RawStringsFromFile.Count - 1]) + Environment.NewLine; }

privatevoid Form1_Load(object sender, EventArgs e)

{

button1.Enabled = false;

Thread.CurrentThread.CurrentCulture = newCultureInfo("en-US");

}

privatevoid button1_Click_1(object sender, EventArgs e)

{

string file = @".\Display.html";

string html = System.IO.File.ReadAllText(file);

webBrowser1.DocumentText = html;//chay file htlm }

privatevoid button3_Click(object sender, EventArgs e)

{ this.Hide(); }

} }

Code của file source.html:

<!DOCTYPE html> <html>

<head>

<title>Google Maps JavaScript API v3 Example: Map Simple</title> <meta name="viewport"

content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta charset="UTF-8"> <style type="text/css"> html, body, #map_canvas { margin: 0; padding: 0; height: 100%; } </style> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var map; function initialize() { var myOptions = { zoom: 18,

center: new google.maps.LatLng(10.7611035386,106.6631039055822), mapTypeId: google.maps.MapTypeId.ROADMAP

};

map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

var flightPlanCoordinates = [ ];

var flightPath = new google.maps.Polyline({ path: flightPlanCoordinates,

strokeColor: "#FF0000", strokeOpacity: 1.0,

strokeWeight: 5 });

flightPath.setMap(map); }

google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="map_canvas"></div> </body> </html>

Hình 41: chay file source.html

6.2.4 Tạo form INTRODUCE:

Hình 42: form INTRODUCE

Code cho button BACK

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; namespace DOAN {

publicpartialclassForm4 : Form

{

public Form4() {

InitializeComponent(); }

privatevoid button1_Click(object sender, EventArgs e)

{ this.Hide(); } } }

6.2.5 Tạo form chính chứa tất cả các form trên:

Tạo một winform C# như sau:

Hình 43: form chính của chương trình

- Button DATA mở đến form chứa thông tin xe bus

- Button MAP mở đến from hiển thị google map

- Button INTRODUCE mở đến form giới thiệu chương trình

- Button EXIT thoát khỏi chương trình  Code của chương trình:

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; namespace DOAN {

publicpartialclassForm2 : Form

{

public Form2() {

InitializeComponent(); }

privatevoid Form2_Load(object sender, EventArgs e)

{ }

privatevoid button1_Click(object sender, EventArgs e)

{

Form3 _data = newForm3();

}

privatevoid button2_Click(object sender, EventArgs e)

{

Form1 _map = newForm1();

_map.Show(); }

privatevoid button3_Click(object sender, EventArgs e)

{

Form4 _gioithieu = newForm4();

_gioithieu.Show(); }

privatevoid button4_Click(object sender, EventArgs e)

{

DialogResult kq = MessageBox.Show("Bạn thực sự muốn thoát", "GPS-GOOGLEMAP",

MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (kq == DialogResult.Yes)

{

MessageBox.Show("THANK YOU", "GPS-GOOGLEMAP");

this.Close(); } } } }

PHẦN III: XÂY DỰNG HỆ THỐNG

Chương 7: KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN

Chương 7: KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN

Nội dung:

7.1 Kết luận

7.2 Hướng phát triển

PHẦN III: XÂY DỰNG HỆ THỐNG

Chương 7: KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN 7.1 Kết luận:

Những việc đã làm được:

- Xây dựng được cơ sở dữ liệu chứa thông tin xe và liên kết được với winform C#

- Xây dựng được giao diện người dùng cơ bản

- Hiển thị tọa độ lên google map  Những việc chưa làm được:

- Chưa gửi/nhận được tọa độ giữa 2 SIM908 bằng SMS

- Chưa lưu được tọa độ vào SD card

- Chưa làm được chế độ xem online trực tiếp cập nhật khi xe đang chạy  Giải pháp:

- Chưa gửi/nhận được tọa độ giữa 2 SIM908 bằng SMS qua tìm hiểu thêm thì có thể là do ứng dụng terminal26 đi kèm theo SIM908 không hiểu lệnh “Ctrl + Z” nên mặc dù đã cho gõ tin nhắn nhưng không gửi tin đi ⇨ chưa kiểm chứng được.

- Bổ sung kiến thức về C#

7.2 Hướng phát triển:

 Do hạn chế về thời gian và kiến thức của bản thân nên đề tài thực hiện xong chỉ đáp ứng một phần nhỏ yêu cầu của một hệ thống hoàn chỉnh.

 Mở rộng đề tài có thể thực hiện:

- Chỉ cần dùng một module SIM908, giao tiếp qua mạng GPRS

- Ngoài theo dõi hành trình xe có thể theo dõi cả tốc độ, số lượng hành khách bằng cách thêm các cảm biến

TÀI LIỆU THAM KHẢO

[1] SIM908_EVB kit_User Guide_V1.00.pdf [2] SIM908 EVB kit user manual -V2.0.pdf

[3] SIM908_AT Command Manual_V1.01.pdf [4] http://www.picvietnam.com/ [5] http://congdongcviet.com/ [6] http://www.youtube.com/ [7] http://vi.wikipedia.org [8] www.payitforward.edu.vn/

Một phần của tài liệu QUẢN LÝ XE SỬ DỤNG MODULE SIM908 VÀ GOOGLE MAP (code Csharp) (Trang 50)

Tải bản đầy đủ (PDF)

(72 trang)