1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Management;
using Microsoft.Win32;
using System.Globalization;
using DLLInterface;
namespace TrakerCfgSoft
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class GPSForm : Form
{
//List<string> listofport=new List<string>();
//List<string> listofdevice=new List<string>();
SerialPort mySerialPort;
XElement trackerlist=new XElement(new XElement ("trackers"));
Assembly assembly;
Type type;
MethodInfo method;
DllInterface dllInterface;
public GPSForm()
{
InitializeComponent();
}
private void MainFormLoad(object sender, EventArgs e)
{
mySerialPort = new SerialPort();
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Event for receiving data
// Read the buffer to text box.
this.Invoke(new EventHandler(DoUpdate));
}
private void DoUpdate(object s, EventArgs e)
{
//Then, the textbox always display new messages when they arrive.
//txResponse.Text = txResponse.Text + mySerialPort.ReadExisting();
txResponse.AppendText(mySerialPort.ReadExisting());
}
public void CreatePort(string port)
{
//paramètres de connexion
mySerialPort.PortName = txPort.Text.ToUpper();
mySerialPort.BaudRate = 57600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DtrEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//to get messages from the port.
}
public void SendCommand(string cmd)
{
// Button to send test data
try
{
string commandeaffiche = "===> " + txSend.Text + "\n";
txResponse.SelectionColor = Color.Firebrick;
txResponse.AppendText(commandeaffiche);
txResponse.SelectionColor = Color.Black;
string data = txSend.Text + (char)13; //where 13 is the <CR> ASCII code.
byte[] commande = Encoding.ASCII.GetBytes(data);
mySerialPort.Write(commande, 0, commande.Length);
//txResponse.Text = txResponse.Text + txSend.Text;
}
catch (Exception sendex)
{
MessageBox.Show(sendex.ToString());
}
}
private void btSend_Click(object sender, EventArgs e)
{
SendCommand(txSend.Text);
}
void BtOpenClick(object sender, EventArgs e)
{
if (mySerialPort.IsOpen == false)
{
CreatePort(txPort.Text);
mySerialPort.Open();
StatusLabel.Text = "Port Ouvert.";
}
}
void BtCloseClick(object sender, EventArgs e)
{
//To close the connection
mySerialPort.Close();
StatusLabel.Text = "Port Fermé.";
}
private void portList_SelectedIndexChanged(object sender, EventArgs e)
{
//Send the selected item port(DeviceID from listofport) in the txPort textbox
//
//txPort.Text = (listofport[(portList.SelectedIndex)]).ToString();
string port = (from element in trackerlist.Elements("port") where element.Attribute("ident").Value == (string)portList.SelectedItem select element.Attribute("port").Value).First().ToString();
txPort.Text=port;
}
private void portList_Update(object sender, EventArgs e)
{
//empty the two lists
//listofdevice.Clear();
//listofport.Clear();
//will scan the serialports to get DeviceID (ports COMx) and Name of the connected device
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_SerialPort");
foreach (ManagementObject queryObj in searcher.Get())
{
//listofport.Add(Convert.ToString(queryObj["DeviceID"]));
//listofdevice.Add(Convert.ToString(queryObj["Name"]));
string port = Convert.ToString(queryObj["DeviceID"]);
string model = Convert.ToString(queryObj["Name"]);
txPort.Text=port;
CreatePort(port);
mySerialPort.Open();
GetDriver(model);
SendCommand(dllInterface.GetId());
string ident = dllInterface.GetId(mySerialPort.ReadExisting());
mySerialPort.Close();
//btSend_Click
try
{
trackerlist.Add(
(new XElement("tracker",
new XAttribute("model", model),
new XAttribute("port", Convert.ToString(queryObj["DeviceID"])),
new XAttribute("ident", ident))));
}
catch
{
trackerlist.Add(
(new XElement("tracker",
new XAttribute("model", model),
new XAttribute("port", Convert.ToString(queryObj["DeviceID"])),
new XAttribute("ident", "Connexion non identifiable"))));
}
}
}
catch (ManagementException me)
{
MessageBox.Show("An error occurred while querying for WMI data: " + me.Message);
}
//refill the porList with the ports scan result
portList.Items.Clear();
//portList.Items.AddRange((string[])listofdevice.ToArray());
var tracklist = (from element in trackerlist.Elements("ident") select trackerlist.Attribute("ident").Value).ToList();
tracklist.ToList().ForEach(v => portList.Items.Add(v));
}
private void txSend_KeyPress(object sender, KeyPressEventArgs e)
{
//initiate btSend_Click event when press the Return or Enter keys
if (e.KeyChar == (char)Keys.Return || e.KeyChar == (char)Keys.Enter)
btSend_Click(sender,e);
}
private void txResponse_TextChanged(object sender, EventArgs e)
{
//to scroll the text to last line
txResponse.SelectionStart = txResponse.Text.Length;
txResponse.ScrollToCaret();
txResponse.Refresh();
}
public void GetDriver(string model)
{
if (model.Contains("AVL VT-SERIAL"))
{
dllInterface=(DllInterface)assembly.CreateInstance("AVL_VT_SERIAL.Base");
//assembly = Assembly.LoadFrom("AVL_VT_SERIAL.dll");
//else if (model.Contains("Port de communication"))
// assembly = Assembly.LoadFrom("X8.dll");
//type = assembly.GetType("AVL_VT_SERIAL.Base");
method = type.GetMethod("GetId", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
}
}
//public string GetId()
//{
// return;
//}
}
} |