안녕하세요. 시리얼 통신을 하는데 문제가 있어 질문드립니다.
cas 인디케이터와 연결을 할려고 합니다. cas 인디케이터는 스트림모드인데 다른 시리얼프로그램을 통해 테스트를 하
면 데이터가 정상적으로 들어옵니다.
그런데 .net으로 프로그램을 만들어 통신을 하면 포트는 연결이 되는데 데이터가 들어오질 않습니다.
만든 프로그램이 이상한가 싶어 다른 시리얼프로그램과 연동해서 TEST를 하면 프로그램으로 데이터가 잘 들어옵니다.
지금 이틀째 고민중인데 도통이유를 모르겠습니다. 인디케이터로부터만 넘어오는 데이터를 수신을 하지 못하는데
리시브이벤트 핸들러가 전혀 작동을 안하는거 같습니다.
이유가 무엇일까요?? 이런 경우는 첨인데 도움부탁드립니다. 아래는 통신 포트 연결 소스입니다.
try {
sp = new SerialPort(“COM7” , 9600 , Parity.None ,8 ,StopBits.One);
sp.Open();
sp.DataReceived += (sender, e) =>
{
blankLabel(1);
byte[] weightbytes = new byte[sp.BytesToRead];
sp.Read(weightbytes , 0 , sp.BytesToRead);
// MessageBox.Show(sp.BytesToRead.ToString());
foreach (byte weightbyte in weightbytes)
{
weightData.Add(weightbyte);
if (weightData.Count >=2)
{
if (weightData[weightData.Count - 2] == 0x0D && weightData[weightData.Count - 1] == 0x0A)
{
byte[] weightBytes = weightData.ToArray();
byte[] weightDataBytes = new byte[9];
weightDataBytes[0] = weightBytes[9];
weightDataBytes[1] = weightBytes[10];
weightDataBytes[2] = weightBytes[11];
weightDataBytes[3] = weightBytes[12];
weightDataBytes[4] = weightBytes[13];
weightDataBytes[5] = weightBytes[14];
weightDataBytes[6] = weightBytes[15];
weightDataBytes[7] = weightBytes[16];
weightDataBytes[8] = weightBytes[17];
displayWeight(weightDataBytes);
weightData.Clear();
}
}
}
blankLabel(0);
};
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}