Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionusing 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; //} } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DLLInterface { public interface DllInterface { string GetId(); string GetId(string message); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DLLInterface; namespace AVL_VT_SERIAL { public class Basis:DllInterface { public string GetId() { return "$wp+uncfg=0000,?"; } public string GetId(string message) { char[] separator=new char[]{' ',',','=','+',':'}; string[] decoupe = message.Split(separator); return decoupe[3]; } } }