들어오는 데이터를 읽고 버퍼에 저장하는 RS232 응용 프로그램을 만들려고합니다. 나는 RS232 예에서 다음 코드를 발견하지만 난 그것을 사용하는 방법을 잘 모르겠습니다직렬 포트에서 바이트 읽기 및 저장
* RS232 예 port_DataReceived *
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!comport.IsOpen) return;
if (CurrentDataMode == DataMode.Text)
{
string data = comport.ReadExisting();
LogIncoming(LogMsgType.Incoming, data + "\n");
}
else
{
int bytes = comport.BytesToRead;
byte[] buffer = new byte[bytes];
comport.Read(buffer, 0, bytes);
LogIncoming(LogMsgType.Incoming, ByteArrayToHexString(buffer) + "\n");
}
}
내가 들어오는 바이트 배열을 소요하고 결합하여 다른 방법을 쓰기 위해 노력하고 있어요 다른 배열로 ...
private void ReadStoreArray()
{
//Read response and store in buffer
int bytes = comport.BytesToRead;
byte[] respBuffer = new byte[bytes];
comport.Read(respBuffer, 0, bytes);
//I want to take what is in the buffer and combine it with another array
byte AddOn = {0x01, 0x02}
byte Combo = {AddOn[1], AddOn[2], respBuffer[0], ...};
}
들어오는 바이트를 읽는 방법과 포트에 저장하는 방법이 혼동 스럽기 때문에 현재 두 가지 방법이 있습니다. "ReadStoreArray"메서드에서 "port_DataReceived"메서드를 사용할 수 있습니까? "ReadStoreArray"메서드를 수정해야합니까? 아니면 그냥 다시 시작해야합니까?
첫 번째 기능은 이벤트 처리기입니다. http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx. 포트에 이벤트를 추가하면 자동으로 호출됩니다. 'port_DataReceived'는'LogIncoming' 대신'ReadStoreArray'를 호출하십시오 –
하나의 이벤트 핸들러에서 전체 메시지를 수신하지 못할 수도 있습니다. 필요한 바이트 수를 얻을 때까지 읽어야합니다. –
답변 해 주셔서 감사합니다. 대부분의 경우 나는 이벤트 핸들러를 그대로 두길 원한다. 난 단지 위에 언급 된 특정 메서드에 대한 다른 배열과 응답을 결합하려는 "ReadStoreArray". 어떻게 "버퍼"로 읽은 바이트를 "ReadStoreArray"메소드로 가져올 수 있습니까? – Nevets