tintinma
Messages postés2Date d'inscriptionlundi 30 mars 2015StatutMembreDernière intervention30 mars 2015
-
30 mars 2015 à 08:28
tintinma
Messages postés2Date d'inscriptionlundi 30 mars 2015StatutMembreDernière intervention30 mars 2015
-
30 mars 2015 à 11:55
Bonjour,
mon projet consiste a établir une interface de communication entre mon pc et une commande d'axe (MCS 32EX SERAD) via un port série.
j'ai réaliser l'interface avec C#, pour teste la communication j'ai utilisé un Hyper terminal et pour le moment ça marche je peux envoyer et recevoir des données, mais quand je les essayer avec la commande d'axe ça marche pas, je me connecter pas. donc je me suis dit peut être si au niveau de la configuration du port ( les led RTS, CTS, DSR, XonXoffOut, XonXoffIn)
pouvez-vous m'explique l'événement Pinchangerd avec un petit code, j'ai bout chercher sur google mais je comprend pas trop?????
voila mon 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]); }
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_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++ ) {