Uderc programming article aggregator sites

éventement PinChanged port série

Advertisement
Hello,
My project is to establish a communication interface between my pc and motion control (MCS 32EX SERAD) via a serial port.
I realized the interface with C #, for testing the communication I used a Hyper terminal and it works for now I can send and receive data, but when I try them with motion control that does not work, I not connect. So I thought may be so at the port configuration
(leds RTS, CTS, DSR, XonXoffOut, XonXoffIn)
Can you explain the Pinchangerd event with a short code, I end search google but I do not really understand ?????
sorry for my english
here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Threading;
namespace Communicatio_1
public partial class Form1 : Form
public byte[] bBufferBytes; //buffer a envoyer
public delegate void myDelegate(byte sData);
//public event SerialDataReceivedEventHandler DataReceived;
public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
public bool RtsEnable { get; set; }
public Form1()
InitializeComponent();
this.Refresh();
private void Form1_Load(object sender, EventArgs e) //l'événement Form_Load nous lire tous les ports série disponibles et nous assigner les valeurs à la liste déroulante.
//on commence par lister les voies séries présentes
String[] ports = SerialPort.GetPortNames(); //fonction statique
//string [] ports = System.IO.Ports.SerialPort.GetPortNames ();
for (int i = 0; i < ports.Length; i++) //on ajoute les ports au combo box
portBox.Items.Add(ports[i]);
private void button_charger_Click(object sender, EventArgs e)
openFileDialog1.Title = "Chargement";// Titre
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK) // Ouverture boite de dialogue OpenFile
textBox1.Text = string.Empty; // On vide le TextBox
backgroundWorker1.RunWorkerAsync();// executer le Background worker doWork()
// timer1.Start();
StreamReader sr = new StreamReader(openFileDialog1.OpenFile(), Encoding.Default); // Ouverture du fichier sélectionné
// son nom est dans openFileDialog1.FileName
private void button_connecter_Click(object sender, EventArgs e)
serialPort1.PortName = portBox.SelectedItem.ToString();
serialPort1.BaudRate = 9600;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.DataBits = 8;
serialPort1.Handshake = Handshake.None;
serialPort1.PortName = portBox.Text;
serialPort1.RtsEnable = true;
serialPort1.DtrEnable = true;
// serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
try
serialPort1.Open(); //Ouvrez le SerialPort
StatusLabel1.Text = "serialPort1 : " + portBox.Text + " Opened";//Mettre à jour le StatusStrip
MessageBox.Show("Opened");
catch (Exception ex)
MessageBox.Show("Failed: " + ex.Message);
button_connecter.Enabled = false;
button_deconnecter.Enabled = true;
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
radioButton.Enabled = true;
radioButton.BackColor = Color.Black;
private void button_deconnecter_Click(object sender, EventArgs e)
try
serialPort1.Close(); //fermer le SerialPort
StatusLabel1.Text = "serialPort1 : " + portBox.Text + " Closed"; //Mettre à jour le StatusStrip
MessageBox.Show("Closed");
catch (Exception ex)
MessageBox.Show("Failed: " + ex.Message);
button_connecter.Enabled = true; // activer le button connecter
button_deconnecter.Enabled = false;// desctiver le bitton deconnecter
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
radioButton.BackColor = Color.White;
private void button_recevoir_Click(object sender, EventArgs e)
StatusLabel1.Text = "Appuyez sur 'F6' de la commande numérique puis patientez...";
textBox2.SelectedText = string.Empty;
//textBox2.AppendText(DataReceived.ToString());
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
//string vide
string value = "";
//tant qu'il y a des données entrée on continue
while (serialPort1.BytesToRead > 0)
// on crée un char de 255 caractere
char[] buffer = new char[255];
//lecture du buffer jusqu'a sa longeur buffer.Length
int longueur_bytes_read = serialPort1.Read(buffer, 0, buffer.Length);
//tes characteres sont contenus dans buffer.
//on remplit notre string value byte par byte par notre tableau de charactere buffer, le += fait que l'on écrase pas les données qui sont inserées, mais on les ajoutes.
for (int i = 0; i < longueur_bytes_read; i++)
value += buffer[i];
//la methode toString passe tes bytes en characteres lisibles.
safeInput(value.ToString());
private void safeInput(string msg)
MethodInvoker method = delegate
// on efface la textbox
textBox2.SelectedText = string.Empty;
// on met les données dedans en string (conversion char->string)
textBox2.AppendText(msg.ToString());
if (InvokeRequired)
BeginInvoke(method);
else
method.Invoke();
private void button_envoyer_Click(object sender, EventArgs e)
StatusLabel1.Text = "Appuyez sur 'F6' de la commande numérique puis patientez...";
// serialPort1.Write(bBufferBytes, 0, bBufferBytes.Length);
// serialPort1.WriteLine("");
StreamReader sr = new StreamReader(openFileDialog1.OpenFile(), Encoding.Default);
try
string data = sr.ReadLine();
// bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
while (data != null)
bBufferBytes = System.Text.Encoding.ASCII.GetBytes(data); //Place le contenu dans un buffer d'octets (ASCII: 1 char = 1 octet) (UNICODE: 1 Char = 2 octets)
textBox1.AppendText(data + "\r\n");
data = sr.ReadLine();
serialPort1.Write(bBufferBytes, 0, bBufferBytes.Length); // Ecrit le buffer complet sur le SerialPort
StatusLabel1.Text = "Transfert en cours...";
finally
if (sr != null)
sr.Close();
StatusLabel1.Text = "Transfert Fini ";
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
for (int i = 0; i <= 100; i++ )
backgroundWorker1.ReportProgress(i);
System.Threading.Thread.Sleep(100);
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
progressBar2.Value = e.ProgressPercentage;
StatusLabel1.Text = e.ProgressPercentage.ToString()+ "%" + "Télechargement en court...";
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
StatusLabel1.Text = ("Télechargement Fini");
Advertisement
The best answer: Je ai ajouté ce morceau de code, mais il ne fonctionne pas
private void serialPort1_PinChanged (object sender, SerialPinChangedEventArgs e){if (e.EventType == SerialPinChange.Break){serialPort1.BreakState = serialPort1.BreakState;}else if (e.EventType == SerialPinChange.CDChanged){}else if (e.EventType == SerialPinChange.CtsChanged){}else if (e.EventType == SerialPinChange.DsrChanged){}else if (e.EventType == SerialPinChange.Ring){
et l'initialisation:
serialPort1.PinChanged + = new SerialPinChangedEventHandler (serialPort1_PinChanged);
Help me