private static bool GetPacket(SerialPort port, ref int length, byte[] buf, ref int cnt, ref bool escChar) {
int b;
if(port==null || !port.IsOpen) {
return false;
}
while(port.BytesToRead>0) {
b=port.ReadByte();
if(b<0) {
break;
}
#if !UART_RAW_MQTTSN
if(b==0xC0) {
escChar=false;
if(cnt>1 && cnt==length) {
return true;
} else {
if(cnt>1) {
Log.Warning("r {0}: {1} size mismatch: {2}/{3}", port.PortName, BitConverter.ToString(buf, 0, cnt), cnt, length);
}
cnt=-1;
}
continue;
}
if(b==0xDB) {
escChar=true;
continue;
}
if(escChar) {
b^=0x20;
escChar=false;
}
if(cnt==0x100) {
cnt=-1;
continue;
}
#endif
if(cnt>=0) {
buf[cnt++]=(byte)b;
#if UART_RAW_MQTTSN
if(cnt==length) {
return true;
}
#endif
} else {
#if UART_RAW_MQTTSN
if(b<2 && b>MsMessage.MSG_MAX_LENGTH) {
if(_verbose.value) {
Log.Warning("r {0}:0x{1:X2} wrong length of the packet", port.PortName, b);
}
cnt=-1;
port.DiscardInBuffer();
return false;
}
#endif
length=b;
cnt++;
}
}
return false;
}
private static void SendRaw(SerialPort port, byte[] buf, byte[] tmp) {