Skip to content

Commit

Permalink
Generic com plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersMalmgren committed Aug 17, 2014
1 parent 62f06b1 commit df28df3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions FreePIE.Core.Plugins/FreePIE.Core.Plugins.csproj
Expand Up @@ -59,6 +59,7 @@
<Compile Include="ApplePlugin.cs" />
<Compile Include="ComDevicePlugin.cs" />
<Compile Include="FreePieIOPlugin.cs" />
<Compile Include="GenericComPlugin.cs" />
<Compile Include="Globals\GlobalIndexer.cs" />
<Compile Include="HydraPlugin.cs" />
<Compile Include="FreeImuPlugin.cs" />
Expand Down
115 changes: 115 additions & 0 deletions FreePIE.Core.Plugins/GenericComPlugin.cs
@@ -0,0 +1,115 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;re
using FreePIE.Core.Contracts;

namespace FreePIE.Core.Plugins
{
[GlobalType(Type = typeof(GenericComPluginGlobal))]
public class GenericComPlugin : ComDevicePlugin
{
private ConcurrentQueue<byte[]> buffers;
private string line;

public override object CreateGlobal()
{
buffers = new ConcurrentQueue<byte[]>();
return new GenericComPluginGlobal(this);
}

public override string FriendlyName
{
get { return "Generic Serial COM"; }
}

protected override void Init(SerialPort serialPort)
{
}

protected override void Read(SerialPort serialPort)
{
if (serialPort.BytesToRead != 0)
{
var buffer = new byte[serialPort.BytesToRead];
serialPort.Read(buffer, 0, buffer.Length);
buffers.Enqueue(buffer);

newData = true;
}
}

public IEnumerable<byte[]> ReadExisting()
{
while (buffers.Count != 0)
{
byte[] result;
if (buffers.TryDequeue(out result))
yield return result;

Thread.Yield();
}
}

public IEnumerable<string> ReadExistingString()
{
return ReadExisting().Select(Encoding.Default.GetString);
}

public string ReadLine()
{
var text = string.Join(string.Empty, ReadExistingString());
line += text;

var indexOfNewLine = line.IndexOf(Environment.NewLine, StringComparison.Ordinal);
if (indexOfNewLine != -1)
{
var end = indexOfNewLine + Environment.NewLine.Length;
var result = line.Substring(0, indexOfNewLine);

line = line.Substring(end, line.Length - end);

return result;
}

return string.Empty;
}

protected override string BaudRateHelpText
{
get { return "Baud rate"; }
}

protected override int DefaultBaudRate
{
get { return 57600; }
}
}

[Global(Name = "genericCom")]
public class GenericComPluginGlobal : UpdateblePluginGlobal<GenericComPlugin>
{
public GenericComPluginGlobal(GenericComPlugin plugin) : base(plugin)
{
}

public IEnumerable<byte[]> ReadExisting()
{
return plugin.ReadExisting();
}

public IEnumerable<string> ReadExistingString()
{
return plugin.ReadExistingString();
}

public string ReadLine()
{
return plugin.ReadLine();
}
}
}

0 comments on commit df28df3

Please sign in to comment.