2016-11-23 10 views
0

学校プロジェクトでは、arduinoに接続されたポンゲームを作成しています。ボタンを押し続けるとラケットは一方向に移動し、そうでなければ他の方向に移動します。 arduinoデータを入力として使用しないと、ゲームは正常に動作します。私は別の機能を使用しようとしましたが、それは動作しません。c#シリアルポートはarduinoからデータを読み取ることができません

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO.Ports; 

namespace pong 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 

      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 

     } 
    } 
} 

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; 




    namespace pong 
    { 

     public partial class Form1 : Form 
     { 
      SerialPort port1 = new SerialPort(); 
      public int points = 0; 
      public int speed_left = 0; 
      public int speed_top = 0; 
      public Form1() 
      { 

       InitializeComponent(); 
       timer1.Enabled = true; 
       Cursor.Hide(); //skrije kurzor 
       this.TopMost = true; 
       this.Bounds = Screen.PrimaryScreen.Bounds; 
       this.FormBorderStyle = FormBorderStyle.None; 
       racket.Top = panel1.Bottom - (panel1.Bottom/10); 
       SerialPort port1 = new SerialPort("COM3"); 
       port1.BaudRate = 9600; 
       port1.Parity = Parity.None; 
       port1.StopBits = StopBits.One; 
       port1.DataBits = 8; 
       port1.Handshake = Handshake.None; 
       port1.RtsEnable = true; 
       port1.Open(); 


      } 

      private void Form1_Load(object sender, EventArgs e) 
      { 

      } 

      private void timer1_Tick(object sender, EventArgs e) 
      { 
       if(!port1.IsOpen) 
       { 
        port1.Open(); 
        port1.ReadTimeout = 1000; 
       } 
       if (port1.IsOpen) 
       { 
        if (port1.ReadExisting()=="") 
        { 
         racket.Left = racket.Left - 0; 
        } 
        else 
        { 
         racket.Left = racket.Left + 10; 
        } 
       } 

       ball.Left += speed_left; 
       ball.Top += speed_top; 

       if(ball.Bottom >=racket.Top && ball.Bottom <= racket.Bottom && ball.Left >= racket.Left && ball.Right <= racket.Right) 
       { 
        speed_left += 2; 
        speed_top += 2; 
        speed_top = -speed_top; 
        points += 1; 
        label2.Text = points.ToString(); 


       } 

       if(ball.Left<=panel1.Left) 
       { 
        speed_left = -speed_left; 
       } 
       if (ball.Right >= panel1.Right) 
       { 
        speed_left = -speed_left; 
       } 
       if(ball.Top <= panel1.Top) 
       { 
        speed_top = -speed_top; 
       } 
       if(ball.Bottom>=panel1.Bottom) 
       { 
        timer1.Enabled=false; 
       } 
      } 

      private void Form1_KeyDown(object sender, KeyEventArgs e) 
      { 
       if(e.KeyCode==Keys.Escape) 
       { 
        this.Close(); 
       } 
      } 

      private void label1_Click(object sender, EventArgs e) 
      { 

      } 

     } 
    } 

アルドゥイーノ:

void setup() { 
    // put your setup code here, to run once: 
    pinMode(8,INPUT); 
    digitalWrite(8,LOW); 
} 

void loop() { 
    Serial.begin(9600); 
    while(1){ 
    if(digitalRead(8)==HIGH) 
    { 
    Serial.write('1'); 
    } 
    } 
} 
+0

ハードウェアを問題として排除しましたか? –

+0

はいシリアルデータを別のプログラムに送信しようとしましたが、うまくいきました。 –

+0

PCをarduinoに接続しようとすると、異なる電圧レベルを変換するためにアダプタが必要になることがあります。http://stackoverflow.com/questions/25020887/serial-communication-between-pc-and-duduino -via-rs232-using-c – PaulF

答えて

0

私はtimer1_Tickにブレークポイントを置くことを示唆している、私はあなたが

timer1.Start(); 

と呼ばれていないため、全くと呼ばれることになるだろうされていない疑いがありますタイマーを開始します。

関連する問題