2015-07-13 2 views
2

J'utilise une application de port série pour envoyer une commande avec des caractères de contrôle Unicode et je n'ai aucun succès. Quand j'envoie la même commande dans hyperterminal ou Putty la communication fonctionne donc je suis curieux si ces deux programmes écrivent un tableau d'octets ou de caractères Unicode ou même une chaîne. Si je mime ce qu'ils font, je pourrais avoir du succès ... Aussi, j'ai remarqué que la propriété de port série que j'ai retiré de la boîte à outils dans Visual Studio a été répertoriée comme utilisant un port COM différent que j'écris dans mon code, ne reçois aucune réponse ou mon code remplace-t-il cela? Lorsque j'effectue des tests de bouclage, j'obtiens aussi la sortie de la commande correcte avec les caractères de contrôle, donc je n'arrive pas à comprendre ce que je fais de mal. Ce .NET Framework 2.0 est FYISerialPorts: Envoyer en octets ou caractères?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.IO.Ports; 
using System.Threading; 

namespace SimpleSerial 
{ 
    public partial class Form1 : Form 
    { 
     string RxString; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void buttonStart_Click(object sender, EventArgs e) 
     { 
      serialPort1.PortName = "COM3"; 
      serialPort1.BaudRate = 9600; 
      serialPort1.Parity = Parity.None; 
      serialPort1.DataBits = 8; 
      serialPort1.StopBits = StopBits.One; 
      serialPort1.Handshake = Handshake.None; 
      serialPort1.ReadTimeout = 500; 
      serialPort1.WriteTimeout = 500; 
      serialPort1.Open(); 
      if (serialPort1.IsOpen) 
      { 
       buttonStart.Enabled = false; 
       buttonStop.Enabled = true; 
       textBox1.ReadOnly = false; 
      } 
     } 
     const char STX = '\u0002'; 
     const char ETX = '\u0003'; 
     readonly string pull_shelf_104 = string.Format("{0}01P00104##{1}" , STX, ETX); 
     private byte[] WrapString(string pull_shelf_104) 
     { 
      return System.Text.Encoding.ASCII.GetBytes(pull_shelf_104); 
     } 
     private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
     { 
      if (serialPort1.IsOpen) 
      { 
       byte[] data = WrapString(pull_shelf_104); 
       serialPort1.Write(data, 0, data.Length); 
      } 
     } 
     private void buttonStop_Click(object sender, EventArgs e) 
     { 
      if (serialPort1.IsOpen) 
      { 
       serialPort1.Close(); 
       buttonStart.Enabled = true; 
       buttonStop.Enabled = false; 
       textBox1.ReadOnly = true; 
      } 
     } 
     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      if (serialPort1.IsOpen) serialPort1.Close(); 
     } 
     private void DisplayText(object sender, EventArgs e) 
     { 
      textBox1.AppendText(RxString); 
     } 
     private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
     { 
      RxString = serialPort1.ReadExisting(); 
      this.Invoke(new EventHandler(DisplayText)); 
     } 
    } 
} 
+0

Vous ouvrez le port de communication pour envoyer/recevoir des octets. Quels sont ces octets (sont-ils dans un certain codage, etc.) cela dépend. Putty fonctionne en mode terminal (?), Il s'attend donc à recevoir des caractères ASCII du modem. En ce qui concerne les com-ports, oh ouais, grande surprise, ils peuvent être n'importe quoi sur un PC différent, ce qui signifie que vous ne devriez pas coder '' com3 ''. Vous pouvez modifier le nom du port COM dans le Gestionnaire de périphériques, mais cela n'élimine pas le problème d'identification du port COM. – Sinatr

+0

'byte [] ackData = Encoding.ASCII.GetBytes (chaîne ici);' essaye ceci pour convertir les fixations en octets bruts. – hypheni

+0

Vous vous concentrez sur le mauvais problème. Il n'y a pas beaucoup de périphériques de port série qui utilisent Handshake.None. Lorsque vous utilisez cela, c'est à vous de définir les propriétés RtsEnable et DtrEnable sur * true *. Si vous ne le faites pas, l'appareil n'enverra rien. –

Répondre

0

Les commentaires ci-dessus m'a aidé à résoudre le problème, merci pour votre aide! J'avais besoin d'activer à la fois RTS et DTS dans mon code, la liste des ports COM n'était pas le problème même si je suis sûr que c'est dans certains cas. Voici mon code pour toute personne ayant des problèmes similaires.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.IO.Ports; 
using System.Threading; 

namespace SimpleSerial 
{ 
    public partial class Form1 : Form 
    { 
     string RxString; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void buttonStart_Click(object sender, EventArgs e) 
     { 
      serialPort1.PortName = "COM3"; 
      serialPort1.BaudRate = 9600; 
      serialPort1.Parity = Parity.None; 
      serialPort1.DataBits = 8; 
      serialPort1.StopBits = StopBits.One; 
      serialPort1.Handshake = Handshake.None; 
      serialPort1.RtsEnable = true; 
      serialPort1.DtrEnable = true; 
      serialPort1.ReadTimeout = 2000; 
      serialPort1.WriteTimeout = 2000; 
      serialPort1.Open(); 
      if (serialPort1.IsOpen) 
      { 
       buttonStart.Enabled = false; 
       buttonStop.Enabled = true; 
       textBox1.ReadOnly = false; 
      } 
     } 
     const char STX = '\u0002'; 
     const char ETX = '\u0003'; 
     readonly string pull_shelf_104 = string.Format("{0}01P00204##{1}" , STX, ETX); 
     private byte[] WrapString(string pull_shelf_104) 
     { 
      return System.Text.Encoding.ASCII.GetBytes(pull_shelf_104); 
     } 
     private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
     { 
      if (serialPort1.IsOpen) 
      { 
       byte[] data = WrapString(pull_shelf_104); 
       serialPort1.Write(data, 0, data.Length); 
      } 
     } 
     private void buttonStop_Click(object sender, EventArgs e) 
     { 
      if (serialPort1.IsOpen) 
      { 
       serialPort1.Close(); 
       buttonStart.Enabled = true; 
       buttonStop.Enabled = false; 
       textBox1.ReadOnly = true; 
      } 
     } 
     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      if (serialPort1.IsOpen) serialPort1.Close(); 
     } 
     private void DisplayText(object sender, EventArgs e) 
     { 
      textBox1.AppendText(RxString); 
     } 
     private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
     { 
      RxString = serialPort1.ReadExisting(); 
      this.Invoke(new EventHandler(DisplayText)); 
     } 
    } 
}