Damn! I took that DarkTimes C# OutGauge example and combined it with
this and I have excatly what I need!
Now my (or DarkTimes's and Microsoft's) C# program sends data to my microcontroller. It's WIP thingy but I have now RPM, Speed and emulated gear "N" (Edit: wrote it really, so it works now, see the code) on external LCD. I did all this with VB too, but outdated lib didn't understand new flags so this is as far as I got. Next I'll hook few LEDs to my uC and see how ABS, TC and stuff like that gives me signals.
My analog tacho is working somehow too, but it's old and slow. I'm more interested in this LCD and LEDs right now. I'll post some pics and videos when I'm far enough with this. Thank you DarkTimes, once again!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO.Ports;
using System.Threading;
namespace OutGauge
{
class Program
{
const int BufferSize = 1024;
const string Host = "127.0.0.1";
const ushort Port = 35555;
static SerialPort _serialPort;
static void Main()
{
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = "COM3";
_serialPort.BaudRate = 57600;
_serialPort.DataBits = 8;
_serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), "None");
_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "Two");
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
// Create UDP socket.
Socket socket = new Socket(
AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
byte[] buffer = new byte[BufferSize];
try
{
// Create EndPoint for LFS and bind the socket to it.
IPEndPoint endPoint = new IPEndPoint(
IPAddress.Parse(Host),
Port);
socket.Bind(endPoint);
while (true)
{
// Receive packet data.
int received = socket.Receive(buffer);
if (received > 0)
{
// Copy data from buffer into correctly sized array.
byte[] data = new byte[received];
Buffer.BlockCopy(buffer, 0, data, 0, received);
// Packet received...
OutGaugePack packet = new OutGaugePack(data);
OutGaugePacketReceived(packet);
}
else
{
Console.WriteLine("Lost connection to OutGauge");
break;
}
}
}
catch (SocketException ex)
{
Console.WriteLine("Socket Error: {0}", ex.Message);
}
}
static void OutGaugePacketReceived(OutGaugePack packet)
{
// Do stuff with packet etc..
_serialPort.Write("S"); // Start-byte for uC
if ((int)packet.RPM < 10){
_serialPort.Write("000");
} else if ((int)packet.RPM < 100){
_serialPort.Write("00");
} else if ((int)packet.RPM < 1000){
_serialPort.Write("0");
}
_serialPort.Write(((int)packet.RPM).ToString());
Int32 speed_kph = (int)(packet.Speed *3.6);
if (speed_kph < 10)
{
_serialPort.Write("00");
}
else if (speed_kph < 100)
{
_serialPort.Write("0");
}
_serialPort.Write(speed_kph.ToString());
if (packet.Gear == 0){
_serialPort.Write("R");
} else if (packet.Gear == 1){
_serialPort.Write("N");
} else if (packet.Gear > 1){
_serialPort.Write((packet.Gear - 1).ToString());
}
}
class OutGaugePack
{
public TimeSpan Time { get; private set; }
public string Car { get; private set; }
public OutGaugeFlags Flags { get; private set; }
public int Gear { get; private set; }
public int SpareB { get; private set; }
public float Speed { get; private set; }
public float RPM { get; private set; }
public float Turbo { get; private set; }
public float EngTemp { get; private set; }
public float Fuel { get; private set; }
public float OilPressure { get; private set; }
public float OilTemp { get; private set; }
public DashLightFlags DashLights { get; private set; }
public DashLightFlags ShowLights { get; private set; }
public float Throttle { get; private set; }
public float Brake { get; private set; }
public float Clutch { get; private set; }
public string Display1 { get; private set; }
public string Display2 { get; private set; }
public int ID { get; private set; }
public OutGaugePack(byte[] data)
{
Time = TimeSpan.FromMilliseconds(BitConverter.ToUInt32(data, 0));
Car = ASCIIEncoding.ASCII.GetString(data, 4, 4).TrimEnd(char.MinValue);
Flags = (OutGaugeFlags)BitConverter.ToUInt16(data, 8);
Gear = data[10];
SpareB = data[11];
Speed = BitConverter.ToSingle(data, 12);
RPM = BitConverter.ToSingle(data, 16);
Turbo = BitConverter.ToSingle(data, 20);
EngTemp = BitConverter.ToSingle(data, 24);
Fuel = BitConverter.ToSingle(data, 28);
OilPressure = BitConverter.ToSingle(data, 32);
OilTemp = BitConverter.ToSingle(data, 36);
DashLights = (DashLightFlags)BitConverter.ToUInt32(data, 40);
ShowLights = (DashLightFlags)BitConverter.ToUInt32(data, 44);
Throttle = BitConverter.ToSingle(data, 48);
Brake = BitConverter.ToSingle(data, 52);
Clutch = BitConverter.ToSingle(data, 56);
Display1 = ASCIIEncoding.ASCII.GetString(data, 60, 16).TrimEnd(char.MinValue);
Display2 = ASCIIEncoding.ASCII.GetString(data, 76, 16).TrimEnd(char.MinValue);
if (data.Length == 96)
{
ID = BitConverter.ToInt32(data, 92);
}
}
}
[Flags]
enum OutGaugeFlags
{
OG_TURBO = 8192,
OG_KM = 16384,
OG_BAR = 32768,
}
[Flags]
enum DashLightFlags
{
DL_SHIFT = 1,
DL_FULLBEAM = 2,
DL_HANDBRAKE = 4,
DL_PITSPEED = 8,
DL_TC = 16,
DL_SIGNAL_L = 32,
DL_SIGNAL_R = 64,
DL_SIGNAL_ANY = 128,
DL_OILWARN = 256,
DL_BATTERY = 512,
DL_ABS = 1024,
DL_SPARE = 2048,
}
}
}