2017-12-08 4 views
0

winform 프로그램을 만들어 직렬 포트에서 데이터를 읽고 텍스트 상자에 표시하십시오. 데이터는 5 초 간격으로 외부 장치에서 전송됩니다. 첫 번째 데이터는 "1"다음에 "2"다음에 "3"이며 같은 패턴을 영원히 반복합니다. "1", "2"및 "3"데이터가 표시 될 때 pictureBox1, pictureBox2, pictureBox3이 visisble로 표시되고 gif가 재생됩니다. 각각 받았다. 문제는 언젠가 다음 데이터가 도착에도 불구하고 다음 각 하나에 전환하지 않는있는 PictureBox이다. 내가 제대로 호출 기능을 사용하고 있는지 확실하지 않다?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

흠집이 시리얼 I/O 자체가 아닌 뭔가에 있다면, 코드 예제는 여전히 시리얼 I/O 코드가있는 한 [mcve]가 아닙니다. 시리얼 I/O에서 문제가 있다고 생각된다면 문제를 재현 할 수있는 사람을 찾는데 어려움을 겪을 것입니다. 특정 하드웨어를 다른 사람들이 사용할 수 없기 때문입니다. 두 경우 모두 문제의 개별 부분을 분리하여 충분한 디버깅을 수행해야 문제가 직렬 I/O와 관련이 있는지 여부를 알 수 있습니다. –

+0

이 프로그램에는 확인을 위해 입력란에 도착한 데이터를 표시하는 Log (data) 메소드가 있습니다. 것은 데이터가 위에서 언급 한 패턴으로 일관되게 텍스트 상자에 기록되었지만 그림 상자는 각 경우에 전환되지 않았습니다. – 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(); 
관련 문제