Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify initialization flow #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void setup() {
Serial.begin(57600, SERIAL_8N1);
Serial.setTimeout(100);
while (!Serial);
Serial.println("INITIALIZED#");

myStepper.setCurrentPosition(0);
myStepper.setMaxSpeed(200.0);
Expand All @@ -24,6 +23,7 @@ void loop() {
String command = Serial.readStringUntil('#');
String response = processCommand(command);
Serial.println(response);
Serial.flush();
}

myStepper.runSpeedToPosition();
Expand Down
Empty file.
Binary file modified SHSimpleFocuser/.vs/SHSimpleFocuser/v17/.suo
Binary file not shown.
43 changes: 32 additions & 11 deletions SHSimpleFocuser/SHSimpleFocuser/DeviceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public void Connect(String comPort)
serialConnection.Parity = SerialParity.None;
serialConnection.StopBits = SerialStopBits.One;
serialConnection.DataBits = 8;
serialConnection.ReceiveTimeout = 10;
serialConnection.ReceiveTimeout = 2;
serialConnection.Connected = true;
_ = ReadResponse();
connected = CommandBool(Commands.Connect);
if(!connected)
{
Expand Down Expand Up @@ -106,22 +105,44 @@ private bool CommandBool(string command)
/// <returns>Response returned by the device</returns>
private string CommandString(string command)
{
traceLogger.LogMessage("CommandString", "Sending command " + command);
//All commands from and to the arduino ends with #
serialConnection.Transmit(command + "#");
return ReadResponse();
for (int retries = 3; retries >= 0; retries--)
{
traceLogger.LogMessage("CommandString", "Sending command " + command);
//All commands from and to the arduino ends with #
serialConnection.Transmit(command + "#");
string response = ReadResponse();
if (response.Length > 0) {
return response;
}
if (retries > 0)
{
traceLogger.LogMessage("CommandString", "Empty response encountered. Retrying...");

}
}

traceLogger.LogMessage("CommandString", "Retry limit has reached!");
return "";
}

/// <summary>
/// Read a response from the arduino and returns it
/// </summary>
private String ReadResponse()
{
traceLogger.LogMessage("ReadResponse", "Reading response");
String response = serialConnection.ReceiveTerminated("#");
response = response.Replace("#", "").Replace("\r", "").Replace("\n", "");
traceLogger.LogMessage("ReadResponse", "Received response " + response);
return response;
try
{
traceLogger.LogMessage("ReadResponse", "Reading response");
String response = serialConnection.ReceiveTerminated("#");
response = response.Replace("#", "").Replace("\r", "").Replace("\n", "");
traceLogger.LogMessage("ReadResponse", "Received response " + response);
return response;
}
catch (Exception)
{
traceLogger.LogMessage("ReadResponse", "Port in use or timeout!");
return "";
}
}


Expand Down
4 changes: 1 addition & 3 deletions SHSimpleFocuser/SHSimpleFocuser/FocuserDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ internal string DetectCOMPort()
Speed = SerialSpeed.ps57600,
PortName = portName,
Connected = true,
ReceiveTimeout = 1,
ReceiveTimeout = 2,
Parity = SerialParity.None,
StopBits = SerialStopBits.One,
DataBits = 8
Expand All @@ -721,8 +721,6 @@ internal string DetectCOMPort()
string response = "";
try
{
// Try to handle the INITIALIZED# message
_ = serial.ReceiveTerminated(SEPARATOR);
serial.Transmit("P" + SEPARATOR);
response = serial.ReceiveTerminated(SEPARATOR).Trim().Replace("#", "").Replace("\r", "").Replace("\n", "");
}
Expand Down
1 change: 1 addition & 0 deletions SHSimpleFocuser/SHSimpleFocuser/SHSimpleFocuser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<OutputPath>bin\Debug\</OutputPath>
<RegisterForComInterop>true</RegisterForComInterop>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down