private byte[] readData(PhyLayer phy) { byte[] data = phy.readData(parameters.timeoutMillis, new PhyParserIsFrameComplete()); int offset = 0; while (data[offset] != HDLC_FLAG) { offset++; } if (offset > 0) { data = helper.extensions.copyOfRange(data, offset, data.Length); } verifyReceivedData(data); return(connection.receivedData); }
/// <summary> /// Performs the steps necessary for a connection in the MODE-E </summary> /// <param name="com"> PhyLayer object to be used for data tx/rx </param> /// <exception cref="PhyLayerException"> </exception> public static void connect(PhyLayer com) { //PhyParserIsFrameComplete phyLayerPaser = new PhyParserIsFrameComplete(); try { com.sendData(Encoding.ASCII.GetBytes("/?!\r\n")); byte[] rxBuff = com.readData(1000, new PhyParserIsFrameComplete()); byte baud = ((byte)rxBuff[4] & (byte)0xFF) > (byte)0x35 ? (byte)0x35 : (byte)rxBuff[4]; com.sendData(new byte[] { 0x06, 0x32, baud, 0x32, 0x0D, 0x0A }); Thread.Sleep(550); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } }
/// <summary> /// Retrieves the data encapsulated inside a Wrapper frame </summary> /// <param name="phy"> the PhyLayer to receive bytes </param> /// <returns> array of bytes with the application data unit contents inside the Wrapper frame </returns> public byte[] read(PhyLayer phy) { byte[] data = phy.readData(parameters.timeoutMillis, new PhyParserIsFrameComplete()); short version = BitConverter.ToInt16(new byte[2] { data[1], data[0] }, 0); short wPortSource = BitConverter.ToInt16(new byte[2] { data[3], data[2] }, 0); short wPortDestination = BitConverter.ToInt16(new byte[2] { data[5], data[4] }, 0); if (version != WRAPPER_VERSION) { throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_FRAME_FORMAT); } if (wPortSource != parameters.wPortDestination || wPortDestination != parameters.wPortSource) { throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_ADDRESS); } return(helper.extensions.copyOfRange(data, 8, data.Length)); }