Browse Source

first version of debug messages display in Serial Monitor console

timer1
Youen Toupin 10 years ago
parent
commit
b728db1e26
  1. 13
      OneWireIO.ino
  2. 69
      SerialChannel.cpp
  3. 19
      SerialChannel.h
  4. 10
      SerialMonitor/SerialMonitor/App.xaml
  5. 83
      SerialMonitor/SerialMonitor/App.xaml.cs
  6. 7
      SerialMonitor/SerialMonitor/MainWindow.xaml
  7. 3
      SerialMonitor/SerialMonitor/MainWindow.xaml.cs
  8. 40
      SerialMonitor/SerialMonitor/MainWindowContext.cs
  9. 26
      SerialMonitor/SerialMonitor/SerialMessage.cs
  10. 3
      SerialMonitor/SerialMonitor/SerialMonitor.csproj
  11. 44
      SerialMonitor/SerialMonitor/SerialPortExtensions.cs

13
OneWireIO.ino

@ -14,8 +14,8 @@ byte backBufferPos = 0;
byte samplesSkipped = SkipSamples; byte samplesSkipped = SkipSamples;
unsigned long backBufferStartTime = micros(); unsigned long backBufferStartTime = micros();
SerialChannel oscilloscope; SerialChannel oscilloscope("oscilloscope");
SerialChannel debug; SerialChannel debug("debug");
void setup() void setup()
{ {
@ -45,8 +45,6 @@ void setup()
sei();//enable interrupts sei();//enable interrupts
Serial.begin(9600); Serial.begin(9600);
oscilloscope.init("oscilloscope");
debug.init("debug");
} }
void loop() void loop()
@ -60,15 +58,10 @@ void loop()
backBufferStartTime = micros(); backBufferStartTime = micros();
sei();//enable interrupts sei();//enable interrupts
unsigned long now = micros();
debug.write((byte*)&now, 4);
debug.write("Starting buffer transmission"); debug.write("Starting buffer transmission");
oscilloscope.write((byte*)&currentBufferStartTime, 4); oscilloscope.write(currentBuffer, currentBufferSize, currentBufferStartTime);
oscilloscope.write(currentBuffer, currentBufferSize);
now = micros();
debug.write((byte*)&now, 4);
debug.write("Buffer transmitted"); debug.write("Buffer transmitted");
} }

69
SerialChannel.cpp

@ -1,29 +1,72 @@
#include "Arduino.h" #include "Arduino.h"
#include "SerialChannel.h" #include "SerialChannel.h"
short SerialChannel::nextId = 0; byte SerialChannel::nextId = 1;
SerialChannel* SerialChannel::first = 0;
SerialChannel::SerialChannel() SerialChannel::SerialChannel(const char* name_)
: next(0)
, id((byte)-1)
, name(name_)
{ {
} if(first == 0)
first = this;
else
{
SerialChannel* c = first;
while(c->next != 0) c = c->next;
c->next = this;
}
void SerialChannel::init(const char* name)
{
id = nextId++; id = nextId++;
Serial.write((short)0);
Serial.write(id);
Serial.write(strlen(name));
Serial.write(name);
} }
void SerialChannel::write(byte* data, short byteCount) void SerialChannel::write(byte* data, short byteCount, unsigned long time)
{ {
Serial.write(byteCount); if (time == (unsigned long)-1)
time = micros();
handleConnection();
Serial.write("START");
Serial.write(id); Serial.write(id);
writeULong(time);
writeShort(byteCount);
Serial.write(data, byteCount); Serial.write(data, byteCount);
} }
void SerialChannel::write(const char* text) void SerialChannel::write(const char* text, unsigned long time)
{
write((byte*)text, strlen(text), time);
}
void SerialChannel::writeShort(short num)
{ {
Serial.write((byte*)text, strlen(text)); Serial.write((byte*)&num, 2);
} }
void SerialChannel::writeULong(unsigned long num)
{
Serial.write((byte*)&num, 4);
}
void SerialChannel::handleConnection()
{
int b = Serial.read();
if(b == (int)'C')
{
Serial.write("CONNECTION");
SerialChannel* c = first;
while(c)
{
Serial.write("START");
Serial.write(0);
Serial.write("ChannelInit");
Serial.write(c->id);
writeShort(strlen(c->name));
Serial.write(c->name);
c = c->next;
}
}
}

19
SerialChannel.h

@ -4,17 +4,24 @@
class SerialChannel class SerialChannel
{ {
private: private:
static short nextId; static SerialChannel* first;
short id; SerialChannel* next;
static byte nextId;
byte id;
const char* name;
public: public:
SerialChannel(); SerialChannel(const char* name_);
void init(const char* name); void write(byte* data, short byteCount, unsigned long time = (unsigned long)-1);
void write(byte* data, short byteCount); void write(const char* text, unsigned long time = (unsigned long)-1);
void write(const char* text); private:
void handleConnection();
void writeShort(short num);
void writeULong(unsigned long num);
}; };
#endif #endif

10
SerialMonitor/SerialMonitor/App.xaml

@ -4,5 +4,15 @@
StartupUri="MainWindow.xaml"> StartupUri="MainWindow.xaml">
<Application.Resources> <Application.Resources>
<Style x:Key="Console" TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="TextTrimming" Value="None"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="Black"/>
</Style>
<Style x:Key="Oscilloscope" TargetType="{x:Type Canvas}">
<Setter Property="Background" Value="White"/>
</Style>
</Application.Resources> </Application.Resources>
</Application> </Application>

83
SerialMonitor/SerialMonitor/App.xaml.cs

@ -2,9 +2,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration; using System.Configuration;
using System.Data; using System.Data;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace SerialMonitor namespace SerialMonitor
{ {
@ -13,5 +18,83 @@ namespace SerialMonitor
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
private SerialPort Serial;
private IDictionary<int, string> Channels = new Dictionary<int, string>();
private bool Connected = false;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Serial = new SerialPort();
Serial.PortName = "COM4";
Serial.BaudRate = 9600;
Serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(OnDataReceived);
Serial.Open();
}
private void OnDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (!Connected)
{
Serial.Write("C");
Serial.ReadTo("CONNECTION");
Connected = true;
}
// channel declaration message: byte 0 ; string "ChannelInit" ; byte <channel id> ; short <length> ; byte[length] <channel name>
// regular message: byte <channel id> ; short <length> ; byte[length] <data>
SerialMessage message = null;
Serial.ReadTo("START");
int id = Serial.ReadByte();
if (id == 0)
{
string check = Encoding.UTF8.GetString(Serial.ReadBytes(11));
if (check != "ChannelInit")
throw new Exception("Incorrect data check for channel initialization");
id = Serial.ReadByte();
int length = Serial.ReadShort();
string name = Encoding.UTF8.GetString(Serial.ReadBytes(length));
Channels[id] = name;
message = new SerialMessage { ChannelName = "debug", StringData = "Channel " + name + " opened", SendTime = 0 };
}
else
{
ulong sendTime = Serial.ReadULong();
int length = Serial.ReadShort();
byte[] data = Serial.ReadBytes(length);
message = new SerialMessage { ChannelName = Channels[id], Data = data, SendTime = sendTime };
}
if(message != null)
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => OnMessageReceived(message)));
}
private void OnMessageReceived(SerialMessage message)
{
switch (message.ChannelName)
{
case "debug":
string text = ((float)message.SendTime / 1000000).ToString("0.000") + "s " + message.StringData;
//Debug.WriteLine(text);
MainWindowContext.Get.WriteLine(text);
break;
case "oscilloscope":
/*float visibleDuration = 10.0f;
float sampleDelay = 0.000936f;
float startOffset = (float)message.SendTime / 1000000;
for (int i = 1; i < message.Data.Length; ++i )
{
var line = new Line();
canvas.Children.Add(line);
}*/
break;
}
}
} }
} }

7
SerialMonitor/SerialMonitor/MainWindow.xaml

@ -1,8 +1,11 @@
<Window x:Class="SerialMonitor.MainWindow" <Window x:Class="SerialMonitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"> Title="Serial Monitor" Height="396" Width="629">
<Grid> <Grid>
<TextBlock TextWrapping="Wrap" VerticalAlignment="Bottom" Height="150" Text="{Binding ConsoleText}" Style="{DynamicResource Console}" Grid.ColumnSpan="2"/>
<ScrollViewer HorizontalScrollBarVisibility="Visible" Margin="0,0,0,155" Grid.ColumnSpan="2">
<Canvas Style="{DynamicResource Oscilloscope}"/>
</ScrollViewer>
</Grid> </Grid>
</Window> </Window>

3
SerialMonitor/SerialMonitor/MainWindow.xaml.cs

@ -23,6 +23,9 @@ namespace SerialMonitor
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
var context = new MainWindowContext();
context.WriteLine("Connecting...");
this.DataContext = context;
} }
} }
} }

40
SerialMonitor/SerialMonitor/MainWindowContext.cs

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SerialMonitor
{
public class MainWindowContext : INotifyPropertyChanged
{
public MainWindowContext()
{
Get = this;
}
public static MainWindowContext Get { get; private set; }
private List<string> Lines = new List<string>();
public string ConsoleText { get { return String.Join("\n", Lines); } }
public void WriteLine(string line)
{
Lines.Add(line);
if (Lines.Count > 9)
Lines.RemoveAt(0);
OnPropertyChanged("ConsoleText");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}

26
SerialMonitor/SerialMonitor/SerialMessage.cs

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SerialMonitor
{
public class SerialMessage
{
public string ChannelName { get; set; }
/// <summary>
/// Time at which the message was sent, in microseconds since the start of the remote executable
/// </summary>
public ulong SendTime { get; set; }
public byte[] Data { get; set; }
public string StringData
{
get { return Encoding.UTF8.GetString(Data); }
set { Data = Encoding.UTF8.GetBytes(value); }
}
}
}

3
SerialMonitor/SerialMonitor/SerialMonitor.csproj

@ -53,6 +53,9 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="MainWindowContext.cs" />
<Compile Include="SerialMessage.cs" />
<Compile Include="SerialPortExtensions.cs" />
<Page Include="MainWindow.xaml"> <Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>

44
SerialMonitor/SerialMonitor/SerialPortExtensions.cs

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SerialMonitor
{
public static class SerialPortExtensions
{
public static int ReadShort(this SerialPort port)
{
int b1 = port.ReadByte();
int b2 = port.ReadByte();
return b2 * 256 + b1;
}
public static ulong ReadULong(this SerialPort port)
{
ulong b1 = (ulong)port.ReadByte();
ulong b2 = (ulong)port.ReadByte();
ulong b3 = (ulong)port.ReadByte();
ulong b4 = (ulong)port.ReadByte();
return b4 * 256 * 256 * 256 + b3 * 256 * 256 + b2 * 256 + b1;
}
public static byte[] ReadBytes(this SerialPort port, int numBytes)
{
var bytes = new byte[numBytes];
int remaining = numBytes;
int pos = 0;
while (remaining > 0)
{
int read = port.Read(bytes, pos, remaining);
if (read < 0)
throw new Exception("Connection closed");
remaining -= read;
pos += read;
}
return bytes;
}
}
}
Loading…
Cancel
Save