2017-12-08 22 views
0

winformプログラムを作成してシリアルポートからデータを読み取り、テキストボックスに表示します。データは5秒間隔で外部デバイスから送信されます。
データ "1"、 "2"、 "3"のとき、pictureBox1、pictureBox2、pictureBox3をvisisbleにしてgifを再生させます。それぞれ受け取った。問題は、ピクチャボックスは、次のデータが到着したにもかかわらず、次のそれぞれの一つに切り替わりません。私は私が正しく呼び出し機能を使用していますかどうかわからないいつかある?C#シリアルDataReceivedイベント呼び出し関数が正しく機能していません

public Form1() 
{ 
    InitializeComponent(); 
    SerialPortProgram(); 
    pictureBox1.Image= 
    Image.FromFile(@"C:\Users\user\Downloads\Gif#1.gif"); 
    pictureBox2.Image = 
    Image.FromFile(@"C:\Users\user\Downloads\Gif#2.gif"); 
    pictureBox3.Image = 
    Image.FromFile(@"C:\Users\user\Downloads\Gif#3.gif"); 
} 

// Create the serial port with basic settings 
private SerialPort port = new SerialPort("COM17", 9600, Parity.None, 8, StopBits.One); 

private void SerialPortProgram() 
{ 
    // Attach a method to be called when there 
    // is data waiting in the port's buffer 
    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); 

    // Begin communications 
    port.Open(); 

    // Enter an application loop to keep this thread alive 
    // Application.Run();  // if this Application.Run() is not commented out, Form1 will not be displayed 
} 

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 
    // Show all the incoming data in the port's buffer 
    string data = (port.ReadExisting()); 
    Log(data); 
    if (data == "1") 
    { 
     gif1Showl(); 
    } 

    if (data == "2") 
    { 
     gif2Show(); 
    } 

    if (data == "3") 
    { 
     gif3Show(); 
    } 
} 


private void Log(string msg) 
{ 
    textBox1.Invoke(new EventHandler(delegate 
    { 
     textBox1.AppendText(msg); 
    })); 
} 

private void gif1Showl() 
{ 
    this.Invoke(new ThreadStart(() => 
    { 
     pictureBox1.Visible = true; 
     pictureBox1.Enabled = true; 
     pictureBox2.Visible = false; 
     pictureBox2.Enabled = false; 
     pictureBox3.Visible = false; 
     pictureBox3.Enabled = false; 
    })); 
} 

private void gif2Show() 
{ 
    this.Invoke(new ThreadStart(() => 
    { 
     pictureBox1.Visible = false; 
     pictureBox1.Enabled = false; 
     pictureBox2.Visible = true; 
     pictureBox2.Enabled = true; 
     pictureBox3.Visible = false; 
     pictureBox3.Enabled = false; 
    })); 
} 

private void gif3Show() 
{ 
    this.Invoke(new ThreadStart(() => 
    { 
     pictureBox1.Visible = false; 
     pictureBox1.Enabled = false; 
     pictureBox2.Visible = false; 
     pictureBox2.Enabled = false; 
     pictureBox3.Visible = true; 
     pictureBox3.Enabled = true; 
    })); 
} 
+0

あなたがpシリアルI/O自体以外のものには、シリアルI/Oコードがある限り、コード例は[mcve]ではありません。シリアルI/Oで問題が発生していると思われる場合は、特定のハードウェアを他の人が利用できないため、問題を再現できる人を見つけるのが非常に困難になります。どちらの場合でも、問題の個々の部分を分離して、問題がシリアルI/Oに関係しているかどうかを知るために、十分なデバッグを行ったはずです。 –

+0

このプログラムには、検証の目的でテキストボックスに到着したデータを表示するLog(データ)メソッドがあります。データは上記のパターンで一貫してテキストボックスに記録されていますが、ピクチャボックスはそれぞれの場合に切り替えられていません。 – Syscomer

答えて

0

起動する場合は、チェックする必要があります新しいスレッドを作成してUI自体を更新することは間違っています

MethodInvoker methodInvokerDelegate = delegate() 
{ 
    pictureBox1.Visible = false; 
    pictureBox1.Enabled = false; 
    pictureBox2.Visible = false; 
    pictureBox2.Enabled = false; 
    pictureBox3.Visible = true; 
    pictureBox3.Enabled = true; 
}; 

//This will be true if Current thread is not UI thread. 
if (this.InvokeRequired) 
    this.Invoke(methodInvokerDelegate); 
else 
    methodInvokerDelegate(); 
関連する問題