CompactFramework
에서 GPRS 연결을 확인하기 위해 스레드에서 작업하고 있습니다.스레드에서 실행중인 serialPort.DataReceived가 작동하지 않습니다.
스레드의 아이디어는 간단합니다. 프로그램이 연결되어 있지 않으면 연결 코드 (이 코드는 오류가 있음)를 실행하지만 연결이 정상이면 60 초 후에 다시 다시 검사합니다. 에.
이제 연결 코드를 중점적으로 살펴 보겠습니다. 다음 코드는 연결되었는지 여부를 확인한 후 연결되지 않은 경우 DataReceive
이벤트를 구독합니다.
void initFormText()
{
if (isThereConnect()) //true if it is connected
{
//enable timer to recheck if it's connected
}
else //it isn't connected
{
serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
serialPort1.Open();
timerStep.Enabled = true;
}
}
는 이제 데이터를 확인하고 timerStep에 따라 시험 할 때, 변수를 설정하고 몇 가지 단계를 구성하는 serialPort1_DataReceived에 문제가 온다.
문제는 DataReceived 이벤트 발생는 것은 내가 잘 작동 스레드 외부에서 다음 코드를 실행하면, 모든 작업을 수행하고 아무튼 스레드에서 연결, 하지만을 할 것입니다 일하지 마라. 나는 이것을 약간의 MessageBox
을 추가하는 것을 시험해 본다. 그리고 나는 DataReceive 안에 결코 나타나지 않는다는 것을 깨닫는다.
void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
byte[] data = new byte[1024];
int n = serialPort1.Read(data, 0, data.Length);
string rec = Encoding.GetEncoding("windows-1252").GetString(data, 0, n);
if (string.IsNullOrEmpty(rec))
{
return;
}
if (rec.Contains("AT+CIMI") && rec.Contains("OK"))
{
MessageBox.Show("serialPort 1");
currState = 1;
}
else if (rec.Contains("READY"))
{
MessageBox.Show("serialPort 11");
currState = 1;
}
else if (rec.Contains("0,1") || rec.Contains("0,5"))
{
MessageBox.Show("serialPort 2");
currState = 2;
}
}
그래서 어떤 이유로 serialPort에 아무 것도 수신되지 않아서 그 이유를 알 수 없습니다. 스레드가 아니라 스레드 외부에서 작동한다는 사실은 나를 좌절하게 만듭니다.
감사합니다. 고마워요!
고마워요 !!! Application.Run으로 잘 작동합니다! 당신의 도움에 많은 감사드립니다! –