Skip to content

Commit

Permalink
Mono support
Browse files Browse the repository at this point in the history
Downgrade to .Net 2.0 compatibility
  • Loading branch information
docbender committed Jan 28, 2016
1 parent eba52ed commit 6a0f3c8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 26 deletions.
1 change: 0 additions & 1 deletion SerialMonitor/Argument.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SerialMonitor
Expand Down
1 change: 0 additions & 1 deletion SerialMonitor/ArgumentCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SerialMonitor
Expand Down
67 changes: 60 additions & 7 deletions SerialMonitor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
Expand Down Expand Up @@ -29,13 +28,13 @@ class Program
/// <param name="args"></param>
static void Main(string[] args)
{
consoleWriteLine("SerialMonitor v.1.0.0");
consoleWriteLine("SerialMonitor v.1.1.0");

string portName = "COM1";
string portName = IsRunningOnMono() ? "/dev/ttyS1" : "COM1";

if(args.Length > 0)
{
if(args[0].Equals("-?") || args[0].Equals("-?") || args[0].Equals("?"))
if(args[0].Equals("-?") || args[0].Equals("-help") || args[0].Equals("--help") || args[0].Equals("?") || args[0].Equals("/?"))
{
printHelp();
Console.WriteLine("\nPress [Enter] to exit");
Expand Down Expand Up @@ -91,9 +90,12 @@ static void Main(string[] args)

SerialPort port = new SerialPort(portName, baudrate, parity, dataBits, stopBits);

#if __MonoCS__
#else
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);
port.PinChanged += new SerialPinChangedEventHandler(port_PinChanged);
#endif


//log option
Expand Down Expand Up @@ -193,10 +195,36 @@ static void Main(string[] args)
PrepareRepeatFile(repeatfile.Parameter);
}

#if __MonoCS__
SerialPinChange _pinsStatesNow = 0;
SerialPinChange _pinsStatesOld = 0;
#endif


while(port.IsOpen)
{
#if __MonoCS__
if(port.BytesToRead > 0)
port_DataReceived(port, null);
else
{
_pinsStatesNow = (SerialPinChange)(Convert.ToInt32(port.CtsHolding) * ((int)SerialPinChange.CtsChanged)
| Convert.ToInt32(port.CDHolding) * ((int)SerialPinChange.CDChanged)
| Convert.ToInt32(port.DsrHolding) * ((int)SerialPinChange.DsrChanged)
| Convert.ToInt32(port.BreakState) * ((int)SerialPinChange.Break));

if(_pinsStatesNow != _pinsStatesOld)
{
SerialPinChange _pinsStatesChange = _pinsStatesNow ^ _pinsStatesOld;

port_PinChanged(port, _pinsStatesChange);
_pinsStatesOld = _pinsStatesNow;
}
Thread.Sleep(100);
}
#else
Thread.Sleep(100);
#endif

if(Console.KeyAvailable)
{
Expand Down Expand Up @@ -248,7 +276,7 @@ private static bool isFileNameValid(string filePath)
{
foreach(char c in System.IO.Path.GetInvalidPathChars())
{
if(filePath.Contains(c))
if(filePath.Contains(c.ToString()))
{
consoleWriteError("File name {0} contains invalid character [{1}]. Enter right file name.", filePath, c);

Expand All @@ -258,7 +286,7 @@ private static bool isFileNameValid(string filePath)

foreach(char c in System.IO.Path.GetInvalidFileNameChars())
{
if(System.IO.Path.GetFileName(filePath).Contains(c))
if(System.IO.Path.GetFileName(filePath).Contains(c.ToString()))
{
consoleWriteError("File name {0} contains invalid character [{1}]. Enter right file name.", filePath, c);

Expand Down Expand Up @@ -398,16 +426,30 @@ private static void PrepareRepeatFile(string fileName)
}
}


#if __MonoCS__
/// <summary>
/// Event on serial port "pin was changed"
/// </summary>
/// <param name="sender"></param>
/// <param name="pinsStatesChange"></param>
static void port_PinChanged(object sender, SerialPinChange pinsStatesChange)
{
#else
/// <summary>
/// Event on serial port "pin was changed"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void port_PinChanged(object sender, SerialPinChangedEventArgs e)
{
SerialPinChange pinsStatesChange = e.EventType;
#endif
SerialPort port = ((SerialPort)sender);
Console.ForegroundColor = ConsoleColor.Cyan;
consoleWriteLine("Pin {0} changed", e.EventType.ToString());

//TODO: change enumerator SerialPinChange printing???
consoleWriteLine("Pin {0} changed", pinsStatesChange.ToString());

writePinState("RTS", port.RtsEnable);
writePinState("CTS", port.CtsHolding);
Expand Down Expand Up @@ -648,6 +690,17 @@ private static void printHelp()
Console.WriteLine("Sended data");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error");

Console.ResetColor();
}

/// <summary>
/// Check Mono runtime
/// </summary>
/// <returns></returns>
public static bool IsRunningOnMono()
{
return Type.GetType("Mono.Runtime") != null;
}
}
}
4 changes: 2 additions & 2 deletions SerialMonitor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
1 change: 0 additions & 1 deletion SerialMonitor/RepeatFileException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SerialMonitor
Expand Down
22 changes: 8 additions & 14 deletions SerialMonitor/SerialMonitor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
Expand All @@ -10,9 +10,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SerialMonitor</RootNamespace>
<AssemblyName>SerialMonitor</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<StartupObject>SerialMonitor.Program</StartupObject>
<TargetFrameworkSubset>
</TargetFrameworkSubset>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -33,17 +35,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Argument.cs" />
Expand All @@ -52,6 +43,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RepeatFileException.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down

0 comments on commit 6a0f3c8

Please sign in to comment.