private FlBinMessageResponse GetResponse(FlBinMessageCommand command) { FlBinMessageResponse response = null; int count = 0; int i; lock (_generalLock) { count = _responseQ.Count; } if (count > 0) { Console.WriteLine($"Response count : {count}"); } for (i = 0; i < count; i++) { if ((_responseQ[i].MessageId == command.MessageId) && (_responseQ[i].Header.flag1.sequence_num == command.Header.flag1.sequence_num)) { lock (_generalLock) { response = _responseQ[i]; _responseQ.RemoveAt(i); } break; } } return(response); }
private void InternalMessageProc() { int bytesToRead; int i; IFlMessage parsedMessage; FlParseState parseState; byte[] data = new byte[128]; _logger.Debug("InternalMessageProc started"); while (_messageLoop == true) { bytesToRead = _serialPort.BytesToRead; if (bytesToRead > 0) { _serialPort.Read(data, 0, bytesToRead); for (i = 0; i < bytesToRead; i++) { parseState = _parser.Parse(data[i], out parsedMessage); if (parseState == FlParseState.ParseOk) { lock (_generalLock) { switch (parsedMessage.MessageCategory) { case FlMessageCategory.Response: int commandCount = 0; lock (_generalLock) { commandCount = _commandQ.Count; } if (commandCount > 0) { FlBinMessageResponse response = (FlBinMessageResponse)parsedMessage; _responseQ.Add(response); } else { _logger.Debug("Timeout response received"); } break; case FlMessageCategory.Event: FlBinMessageEvent evt = (FlBinMessageEvent)parsedMessage; OnEventReceived?.Invoke(this, evt); break; } } } } } } _logger.Debug("InternalMessageProc stopped"); }
private void ReadTempAndHum(uint deviceId, byte sensorNum) { IFlMessage command = null; if (_currentParserType == ParserType.Binary) { command = new FlBinMessageCommand() { MessageId = FlMessageId.ReadTempAndHum, Arguments = new List <object>() { sensorNum } }; ((FlBinMessageCommand)command).Header.device_id = deviceId; ((FlBinMessageCommand)command).Header.flag1.sequence_num = _sequenceNumber; } else if (_currentParserType == ParserType.Text) { command = new FlTxtMessageCommand() { MessageId = FlMessageId.ReadTempAndHum, Arguments = new List <object>() { deviceId.ToString(), sensorNum.ToString() } }; } if (command != null) { CommandResult result = _msgManager.ReadTemperatureAndHumidity(command); LbMessageHistory.Items.Add($"S : ReadTemperature"); string response = string.Empty; if (result != null) { if (result.Response != null) { if (_currentParserType == ParserType.Binary) { FlBinMessageResponse resp = (FlBinMessageResponse)result.Response; if (resp.Header.flag2.error == FlConstant.FL_OK) { if (result.Response.Arguments?.Count == 3) { response = string.Format("R : {0},{1:0.##}.{2:0.##}", (byte)result.Response.Arguments[0], (double)result.Response.Arguments[1], (double)result.Response.Arguments[1] ); } else { response = "R : Invalid response(invalid argument count)"; } } else { response = "R : Error"; } } else if (_currentParserType == ParserType.Text) { if (result.Response.Arguments?.Count == 5) { if ((string)result.Response.Arguments[1] == FlConstant.FL_OK.ToString()) { response = $"R : OK, {(string)result.Response.Arguments[2]}, {(string)result.Response.Arguments[3]}, {(string)result.Response.Arguments[4]}"; } else { response = $"R : {GetArgumentString(result.Response.Arguments)}"; } } else { response = $"R : {GetArgumentString(result.Response.Arguments)}"; } } else { response = "R : Invalid parser type"; } } else { response = "R : No response"; } } else { response = "R : Command fail"; } LbMessageHistory.Items.Add(response); } }
private void ReadGpio(uint deviceId, byte gpioNum) { IFlMessage command = null; if (_currentParserType == ParserType.Binary) { command = new FlBinMessageCommand() { MessageId = FlMessageId.ReadGpio, Arguments = new List <object>() { gpioNum } }; ((FlBinMessageCommand)command).Header.device_id = deviceId; ((FlBinMessageCommand)command).Header.flag1.sequence_num = _sequenceNumber; } else if (_currentParserType == ParserType.Text) { command = new FlTxtMessageCommand() { MessageId = FlMessageId.ReadGpio, Arguments = new List <object>() { deviceId.ToString(), gpioNum.ToString() } }; } if (command != null) { CommandResult result = _msgManager.ReadGpio(command); LbMessageHistory.Items.Add($"S : ReadGpio"); string response = string.Empty; if (result != null) { if (result.Response != null) { if (_currentParserType == ParserType.Binary) { FlBinMessageResponse resp = (FlBinMessageResponse)result.Response; if (resp.Header.flag2.error == FlConstant.FL_OK) { if (result.Response.Arguments?.Count == 2) { response = $"R : {(byte)result.Response.Arguments[0]},{(byte)result.Response.Arguments[1]}"; } else { response = "R : Invalid response(invalid argument count)"; } } else { response = "R : Error"; } } else if (_currentParserType == ParserType.Text) { if (result.Response.Arguments?.Count == 4) { if ((string)result.Response.Arguments[1] == FlConstant.FL_OK.ToString()) { response = $"R : OK, {(string)result.Response.Arguments[2]}, {(string)result.Response.Arguments[3]}"; } else { response = $"R : {GetArgumentString(result.Response.Arguments)}"; } } else { response = $"R : {GetArgumentString(result.Response.Arguments)}"; } } else { response = "R : Invalid parser type"; } } else { response = "R : No response"; } } else { response = "R : Command fail"; } LbMessageHistory.Items.Add(response); } }
private unsafe bool MakeResponseObject(FlBinMsgFullStruct fullMsg, ref IFlMessage message) { bool ret = false; ushort receivedCrc = 0; int crcDataLen = _bufPos - 4; int crcStartIndex = _bufPos - 3; List <object> arguments = null; FlMessageId messageId = (FlMessageId)fullMsg.header.message_id; // Payload size = header.length - 6; // stx header payload crc etx // crc : header payload, // crc data length = received bytes - stx(1) - crc(2) - etx(1) switch (messageId) { case FlMessageId.ReadHardwareVersion: case FlMessageId.ReadFirmwareVersion: { _crc16 = FlUtil.CRC16(_buf, 1, crcDataLen); receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex); if ((CompareCrc(_crc16, receivedCrc) == true) && (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1])) { string hwVerString = Encoding.ASCII.GetString(fullMsg.payload, fullMsg.header.length - 6); arguments = new List <object>() { hwVerString }; ret = true; } } break; case FlMessageId.ReadGpio: { _crc16 = FlUtil.CRC16(_buf, 1, crcDataLen); receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex); if ((CompareCrc(_crc16, receivedCrc) == true) && (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1])) { arguments = new List <object>() { fullMsg.payload[0], // GPIO number fullMsg.payload[1] // GPIO value }; ret = true; } } break; case FlMessageId.WriteGpio: case FlMessageId.BootMode: case FlMessageId.Reset: { _crc16 = FlUtil.CRC16(_buf, 1, crcDataLen); receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex); if ((CompareCrc(_crc16, receivedCrc) == true) && (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1])) { ret = true; } } break; case FlMessageId.ReadTemperature: case FlMessageId.ReadHumidity: { _crc16 = FlUtil.CRC16(_buf, 1, crcDataLen); receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex); if ((CompareCrc(_crc16, receivedCrc) == true) && (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1])) { arguments = new List <object>() { fullMsg.payload[0], // Sensor number BitConverter.ToDouble(_buf, _bufPos - 3 - sizeof(double)) // Sensor value }; ret = true; } } break; case FlMessageId.ReadTempAndHum: { _crc16 = FlUtil.CRC16(_buf, 1, crcDataLen); receivedCrc = BitConverter.ToUInt16(_buf, crcStartIndex); if ((CompareCrc(_crc16, receivedCrc) == true) && (FlConstant.FL_BIN_MSG_ETX == _buf[_bufPos - 1])) { arguments = new List <object>() { fullMsg.payload[0], // Sensor number BitConverter.ToDouble(_buf, _bufPos - 3 - sizeof(double) * 2), // Temperature BitConverter.ToDouble(_buf, _bufPos - 3 - sizeof(double)) // Humidity }; ret = true; } } break; } if (ret == true) { FlBinMessageResponse response = new FlBinMessageResponse() { Arguments = arguments, ReceiveTime = DateTime.UtcNow }; response.Header.device_id = fullMsg.header.device_id; response.Header.length = fullMsg.header.length; response.Header.message_id = (FlMessageId)fullMsg.header.message_id; response.Header.flag1 = fullMsg.header.flag1; response.Header.flag2 = fullMsg.header.flag2; message = response; } return(ret); }