Rs232 user control

Soyez le premier à donner votre avis sur cette source.

Vue 8 918 fois - Téléchargée 677 fois

Description

User control permettant de paramétrer un flux RS232 (Vitesse, parité, stop bit..). Explore les ressources machine et enregistre dans un fichier paramètres choisis (serialisation) pour les utiliser au prochain lancement.

Source / Exemple :


/// Rédacteur : st.antoine
/// copie libre
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Reflection.Emit;
using System.Reflection;
namespace Rs232
{
  public partial class ctlSerialSettings : UserControl
  {
    SerialPort SerialP;
    clsSettings Settings;
    public ctlSerialSettings(SerialPort SerialP,clsSettings Settings)
    {
      this.SerialP = SerialP;
      this.Settings = Settings;
      InitializeComponent();
      GetInitBit();
      GetInitParity();
      GetInitHandshake();
      GetInitStop();
      GetPort();
      GetSpeed(this.RsPort);
    }
    // ==================================
    // Propriétés
    // ==================================
    // byte lenght
    public int Bit
    {
      get
      {
        return (this.Settings.rsValGrpBit);
      }
      set
      {
        this.Settings.rsValGrpBit = value;
      }
    }
    // Parity
    public Parity Parity
    {
      get
      {
        return (this.Settings.rsValGrpParity);
      }
      set
      {
        this.Settings.rsValGrpParity = value;
      }
    }
    // Stop
    public StopBits StopBits
    {
      get
      {
        return (this.Settings.rsValGrpStop);
      }
      set
      {
        this.Settings.rsValGrpStop = value;
      }
    }
    // Handshakes
    public Handshake Handshake
    {
      get
      {
        return (this.Settings.rsValGrpHdk);
      }
      set
      {
        this.Settings.rsValGrpHdk = value;
      }
    }
    public string RsPort
    {
      get
      {
        return (this.Settings.RsValCom);
      }
      set
      {
        this.Settings.RsValCom = value;
      }
    }
    // Speed
    public string RsSpeed
    {
      get
      {
        return (this.Settings.RsValSpeed);
      }
      set
      {
        this.Settings.RsValSpeed = value;
      }
    }

    // ==================================
    // Init
    // ==================================

    // byte size
    private void GetInitBit()
    {
      switch (Bit)
      {
        case 8:
          {
            this.rsValbits8.Checked = true;
            break;
          }
        case 7:
          {
            this.rsValbits7.Checked = true;
            break;
          }
      }
    }
    // Parity
    private void GetInitParity()
    {
      switch (Parity)
      {
        case Parity.Even:
          {
            this.rsValParityEven.Checked = true;
            break;
          }
        case Parity.Odd:
          {
            this.rsValParityOdd.Checked = true;
            break;
          }
        case Parity.None:
          {
            this.rsValParityNone.Checked = true;
            break;
          }
      }
    }
    // Stop bit
    private void GetInitStop()
    {
      switch (StopBits)
      {
        case StopBits.None:
          {
            this.rsValStopNone.Checked = true;
            break;
          }
        case StopBits.One:
          {
            this.rsValStop1.Checked = true;
            break;
          }
        case StopBits.Two:
          {
            this.rsValStop2.Checked = true;
            break;
          }
      }
    }
    // Stop bit
    private void GetInitHandshake()
    {
      switch (Handshake)
      {
        case Handshake.RequestToSend:
          {
            this.rsValHdkHard.Checked = true;
            break;
          }
        case Handshake.RequestToSendXOnXOff:
          {
            this.rsValHdkTwice.Checked = true;
            break;
          }
        case Handshake.None:
          {
            this.rsValHdkNone.Checked = true;
            break;
          }
        case Handshake.XOnXOff:
          {
            this.rsValHdkSoft.Checked = true;
            break;
          }
      }
    }
    // Port name
    void GetPort()
    {
      string[] PortNames = SerialPort.GetPortNames();
      foreach (string Port in PortNames)
      {
        this.RsValCom.Items.Add(Port);
      }
      this.RsValCom.Text = this.RsPort;
    }
    // Speed of port
    void GetSpeed(string Com)
    {
      SerialPort _port = new SerialPort(Com);
      string[] Speed = {"75","110","134_5","150","300","600","1200","1800","2400","4800",
      "7200","9600","14400","19200","38400","56000","128000","115200","57600"};

      _port.Open();
      object p = _port.BaseStream.GetType().GetField("commProp", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(_port.BaseStream);
      Int32 bv = (Int32)p.GetType().GetField("dwSettableBaud", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).GetValue(p);
      for (int i = 0; i < 18; i++)
      {
        if ((bv & 0x00000001) == 1)
        {
          this.RsValSpeed.Items.Add(Speed[i]);
        }
      }
      this.RsValSpeed.Text = this.RsSpeed;
      _port.Close();
    }
    // ==================================
    // Event
    // ==================================
    // Byte size
    private void rsValbits8_Click(object sender, EventArgs e)
    {
      this.rsValbits8.Checked = true;
      this.Bit = 8;
    }
    private void rsValbits7_Click(object sender, EventArgs e)
    {
      this.rsValbits7.Checked = true;
      this.Bit = 7;
    }
    // Parity
    private void rsValParityOdd_Click(object sender, EventArgs e)
    {
      this.rsValParityOdd.Checked = true;
      this.Parity = System.IO.Ports.Parity.Odd;
    }
    private void rsValParityNone_Click(object sender, EventArgs e)
    {
      this.rsValParityNone.Checked = true;
      this.Parity = System.IO.Ports.Parity.None;
    }
    private void rsValParityEven_Click(object sender, EventArgs e)
    {
      this.rsValParityEven.Checked = true;
      this.Parity = System.IO.Ports.Parity.Even;
    }
    // Stop
    private void rsValStop1_Click(object sender, EventArgs e)
    {
      this.rsValStop1.Checked = true;
      this.StopBits = System.IO.Ports.StopBits.One;
    }
    private void rsValStop2_Click(object sender, EventArgs e)
    {
      this.rsValStop2.Checked = true;
      this.StopBits = System.IO.Ports.StopBits.Two;
    }
    private void rsValStopNone_Click(object sender, EventArgs e)
    {
      this.rsValStopNone.Checked = true;
      this.StopBits = System.IO.Ports.StopBits.None;
    }
    // handshake
    private void rsValHdkHard_Click(object sender, EventArgs e)
    {
      this.rsValHdkHard.Checked = true;
      this.Handshake = System.IO.Ports.Handshake.RequestToSend;
    }
    private void rsValHdkSoft_Click(object sender, EventArgs e)
    {
      this.rsValHdkSoft.Checked = true;
      this.Handshake = System.IO.Ports.Handshake.XOnXOff;
    }
    private void rsValHdkTwice_Click(object sender, EventArgs e)
    {
      this.rsValHdkTwice.Checked = true;
      this.Handshake = System.IO.Ports.Handshake.RequestToSendXOnXOff;
    }
    private void rsValHdkNone_Click(object sender, EventArgs e)
    {
      this.rsValHdkNone.Checked = true;
      this.Handshake = System.IO.Ports.Handshake.None;
    }
    // Com port
    private void RsValCom_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.RsPort = this.RsValCom.Text;
    }
    // speed
    private void RsValSpeed_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.RsSpeed = this.RsValSpeed.Text;
    }
    private void rsValbtOK_Click(object sender, EventArgs e)
    {
      this.SerialP.BaudRate = Convert.ToInt32(this.RsSpeed);
      this.SerialP.PortName = this.RsPort;
      this.SerialP.Parity = this.Parity;
      this.SerialP.DataBits = this.Bit;
      this.SerialP.StopBits = this.SerialP.StopBits;
      this.Settings.Save();
      this.Parent.Controls.Remove(this);
    } 
  }
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.