110 likes | 299 Vues
GPS simulator. Et C# program som sender data fra en GPS fil til RS232 og simulerer en GPS engine. Tre linjer pr. sekund er satt opp. Programvinduet. RS232 settes oppp i form load. private void Form1_Load(object sender, EventArgs e) { this.Text = "GPS simulator";
E N D
GPS simulator Et C# program som sender data fra en GPS fil til RS232 og simulerer en GPS engine. Tre linjer pr. sekund er satt opp
RS232 settes oppp i form load • private void Form1_Load(object sender, EventArgs e) • { • this.Text = "GPS simulator"; • if(serialPort1.IsOpen==true) • { MessageBox.Show("SerialPort is in use"); • this.Close(); • } • //---------------------- • // open a file • FileInfo theSourceFile = new FileInfo(@"C:\work\gps001.txt"); • // create a reader for the text • // StreamReader reader = theSourceFile.OpenText(); • reader = theSourceFile.OpenText(); • //--------------------------------- • serialPort1.BaudRate=9600; • serialPort1.DataBits=8; • serialPort1.Parity=0; • serialPort1.StopBits = System.IO.Ports.StopBits.One; • serialPort1.Open(); • //------------------------
Vi kan plassere buttons i form load • //------------------------ • Width = 900; • Height = 300; • button1.Left = 600; • button1.Top = 20; • button1.Width = 120; • button1.Height = 30; • button1.Text = "Start"; • button1.Left = 600; • //----------------------- • button2.Left=600; • button2.Top = 70; • button2.Width = 120; • button2.Height = 30; • button2.Text = "Continue"; • //----------------------- • button3.Left = 600; • button3.Top = 120; • button3.Width = 120; • button3.Height = 30; • button3.Text = "Stop"; • button3.Left = 600; • //-----------------------
Buttons og labels spesifiseres • //----------------------- • button4.Left = 600; • button4.Top = 170; • button4.Width = 120; • button4.Height = 30; • button4.Text = "Exit"; • //----------------------- • textBox1.Left = 30; • textBox1.Width = 500; • textBox1.Top = 20; • textBox1.Multiline = true; • textBox1.Height = 200; • textBox1.Text = ""; • //----------------------- • textBox2.Left = 600; • textBox2.Width = 50; • textBox2.Top = 230; • textBox2.Multiline = false; • textBox2.Height = 30; • //---------------------- • label1.Left = 600; • label1.Top = 210; • label1.Text = "Lines read:"; • //------------ • label2.Left = 10; • label2.Top = 240; • label2.Text = "Program sends GPS file (gps001.txt) to RS232 3 lines pr. sec."; • //------------ • }
Start og Continue • private void button1_Click(object sender, EventArgs e) • { • timer1.Enabled = true; • timer1.Interval = 1000; • } • private void button2_Click(object sender, EventArgs e) • { • timer1.Enabled = true; • }
Stop og Exit • private void button3_Click(object sender, EventArgs e) • { • timer1.Enabled = false; • } • private void button4_Click(object sender, EventArgs e) • { • timer1.Enabled = false; • serialPort1.Close(); • this.Close(); • }
Timeren • private void timer1_Tick(object sender, EventArgs e) • { • sendstrings(); • } • Det er sendstrings() som vi kaller opp hvert sekund. Merk at hvis timeren er stoppet kommer det ikke noe kall til sendstrings()
StreamReader og Streamwriter • using System; • using System.IO; • using System.Collections.Generic; • using System.ComponentModel; • using System.Data; • using System.Drawing; • using System.Linq; • using System.Text; • using System.Windows.Forms; • namespace WindowsFormsApplication1 • { • public partial class Form1 : Form • { • int n = 0; • StreamReader reader; • StreamWriter writer;
Tre setninger sendes som standard • void sendstrings() • { • string a, b; • int i,len; • for (i = 0; i < 3; i++) • { • a = reader.ReadLine(); • serialPort1.WriteLine(a); • b = serialPort1.ReadLine(); • len = b.Length; • if(len >1) • textBox1.Text = textBox1.Text+"\r\n"+b; • textBox2.Text = (++n).ToString(); • } • } • Mottatte setninger som er korte – blir ikke skrevet ut