2016-05-18 7 views
0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO.Ports; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 


namespace SerialPort 
{ 
    public partial class Form1 : Form 
    { 

    public Form1() 
    { 
     InitializeComponent(); 
     cmdClose.Enabled = false; 
     foreach (String s in System.IO.Ports.SerialPort.GetPortNames()) 
     { 
      txtPort.Items.Add(s); 
     } 
    } 

    public System.IO.Ports.SerialPort sport; 

    public void serialport_connect(String port, int baudrate , Parity parity, int databits, StopBits stopbits) 
    { 
     DateTime dt = DateTime.Now; 
     String dtn = dt.ToShortTimeString(); 

     sport = new System.IO.Ports.SerialPort(
     port, baudrate, parity, databits, stopbits); 
     try 
     { 
      sport.Open(); 
      cmdClose.Enabled = true; 
      cmdConnect.Enabled = false; 
      txtReceive.AppendText("[" + dtn + "] " + "Connected\n"); 
      sport.DataReceived += new SerialDataReceivedEventHandler(sport_DataReceived); 
     } 
     catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } 
    } 

    private void sport_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
     this.BeginInvoke(new Action(() => 
     { 
      DateTime dt = DateTime.Now; 
      String dtn = dt.ToShortTimeString();  
      txtReceive.AppendText("[" + dtn + "] " + "Received: " + sport.ReadExisting() + "\n"); 
     })); 
    } 

    private void cmdConnect_Click(object sender, EventArgs e) 
    { 
     String port = txtPort.Text; 
     int baudrate = Convert.ToInt32(cmbbaudrate.Text); 
     Parity parity = (Parity)Enum.Parse(typeof(Parity), cmbparity.Text); 
     int databits = Convert.ToInt32(cmbdatabits.Text); 
     StopBits stopbits = (StopBits)Enum.Parse(typeof(StopBits), cmbstopbits.Text); 

     serialport_connect(port, baudrate, parity, databits, stopbits); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     DateTime dt = DateTime.Now; 
     String dtn = dt.ToShortTimeString(); 
     String data = txtDatatoSend.Text; 
     sport.Write(data); 
     txtReceive.AppendText("[" + dtn + "] " + "Sent: " + data + "\n"); 
    } 

    private void cmdClose_Click_1(object sender, EventArgs e) 
    { 
     DateTime dt = DateTime.Now; 
     String dtn = dt.ToShortTimeString(); 

     if (sport.IsOpen) 
     { 
      sport.Close(); 
      cmdClose.Enabled = false; 
      cmdConnect.Enabled = true; 
      txtReceive.AppendText("[" + dtn + "] " + "Disconnected\n"); 
     } 
    } 
} 

}コマンド

enter image description here

を移動せず、私のロボットの位置は移動しませんすべて。しかし、私がRoboteqと呼ばれるVisual Studioとオープンソフトウェアを閉じると、私のロボットは以前に送られたコマンドにしたがって移動し、Roboteqにポートをロードしなくても動きます。それがどんなアイデアなのか?私はコントローラが確かに私の命令を受けたと思うが、どういうわけかそれは実行されず、おそらくRoboteqを起動すると実行される。前もって感謝します。

答えて

0

デバイスで改行を使用してコマンドを終了する必要がありますか?

sport.WriteLine(data); 

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.writeline(v=vs.110).aspx

+0

ご返信いただきありがとうございます、私はあなたが提案し何をしようとしたが、私は、Write(データ)とWriteLineメソッド(データ)は、ここで任意の違いはありませんと思います。私のコントローラは復帰を必要とする "\ r" – ZoeY

+0

それ以上に、各コマンドを終了するために '\ r'を追加するかどうかにかかわらず、同じことが再び起こります。私はそれがなぜかわかりません。 – ZoeY

+0

おそらくあなたのコードを少しスリムにして、初心者のためにプレイすればあなたのイベントハンドラを取り出せるかどうかを見てみてください。私は、フレームワークのSystem.IO.Ports.SerialPort実装の「フレーク性」に関する記事を読み飛ばしていました。http://www.sparxeng.com/blog/software/must-use-net-system-io- ports-serialport – ffa