Skip to content

Commit

Permalink
fix: update parsing/response handling for rs232
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-welker committed May 14, 2024
1 parent 2dc0144 commit 21e393e
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 223 deletions.
26 changes: 26 additions & 0 deletions src/Rs232Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,29 @@ public static IQueueMessage GetInputQuery(IBasicCommunication coms, Action<eComm
}*/
}

public class Rs232Response : IQueueMessage
{
private readonly Action<byte[]> action;
private readonly byte[] _message;

public Rs232Response(byte[] message, Action<byte[]> action)
{
_message = message;

this.action = action;
}

public void Dispatch()
{
if(action == null || _message.Length == 0)
{
return;
}

action(_message);
}
}

public class Rs232Command:IQueueMessage
{
private readonly Action<eCommandType> _action;
Expand All @@ -321,6 +344,8 @@ public class Rs232Command:IQueueMessage

private readonly eCommandType _commandType;

public eCommandType CommandType => _commandType;

public Rs232Command(IBasicCommunication coms, byte[] message, Action<eCommandType> updateCommandAction, eCommandType commandType)
{
if(coms == null)
Expand Down Expand Up @@ -360,6 +385,7 @@ public void Dispatch()
{
_action(_commandType);

Debug.Console(DebugLevels.DebugLevel, "Sending command {0}", _message.ToReadableString());
_comm.SendBytes(_message);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Rs232ParsingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public static class Rs232ParsingUtils

public static bool ParsePowerResponse(this byte[] response)
{
Debug.Console(DebugLevels.DebugLevel, "ParsePowerResponse response: {0}", response.ToReadableString());
Debug.Console(DebugLevels.DebugLevel, "ParsePowerResponse response: {0}", ComTextHelper.GetEscapedText(response));

if (response[2] == 0x00)
{

return response[3] == 0x01;
}

Expand All @@ -44,7 +45,7 @@ public static bool ParsePowerResponse(this byte[] response)
public static string ParseInputResponse(this byte[] response)
{
// TODO [ ] actually add in parsing
Debug.Console(DebugLevels.DebugLevel, "ParseInputResponse response: {0}", response.ToReadableString());
Debug.Console(DebugLevels.DebugLevel, "ParseInputResponse response: {0}", ComTextHelper.GetEscapedText(response));

//add together the input type byte & the input number byte
var inputNumber = response[3] << 8 | response[4];
Expand All @@ -62,6 +63,7 @@ public static string ParseInputResponse(this byte[] response)

public static int ParseVolumeResponse(this byte[] response)
{
Debug.Console(DebugLevels.DebugLevel, "ParseVolumeResponse response: {0}", ComTextHelper.GetEscapedText(response));
//not a direct volume response
if (response[3] != 0x01)
{
Expand All @@ -78,6 +80,7 @@ public static int ParseVolumeResponse(this byte[] response)
/// <returns></returns>
public static bool ParseMuteResponse(this byte[] response)
{
Debug.Console(DebugLevels.DebugLevel, "ParseMuteResponse response: {0}", ComTextHelper.GetEscapedText(response));
//not a direct mute response
if (response[3] != 0x01) { return false; }

Expand Down
Loading

0 comments on commit 21e393e

Please sign in to comment.