Hi Guys. I've tried to make an application using InSim.NET to redirect outgauge packets from udp to serial port connected with arduino but ive encountered a problem.
After a couple of seconds of correct transmission, connection is lost.
Numbers in console and display from arduino stop refreshing.
I don't know where the problem is. Please help
This is my c# code:
And arduino code:
After a couple of seconds of correct transmission, connection is lost.
Numbers in console and display from arduino stop refreshing.
I don't know where the problem is. Please help
This is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using InSimDotNet.Out;
namespace dotnet
{
class Program
{
static void Main()
{
TimeSpan timeout = TimeSpan.FromSeconds(10);
SerialPort serialport = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
serialport.Open();
// Create OutGauge object with the specified timeout.
using (OutGauge outgauge = new OutGauge())
{
// Attach event handler to packet-received event.
outgauge.PacketReceived += (sender, e) =>
{
Console.Clear();
//String command = 'S' + speed.ToString("D3") + '\r';
int speed = Convert.ToInt32(e.Speed * 3.6);
int rpm = Convert.ToInt32(e.RPM);
String command = "S " + speed.ToString("D3") + "\r" + "R " + rpm.ToString("D4") + "\r";
Console.Write(command);
serialport.Write(command);
};
outgauge.TimedOut += outgauge_TimedOut;
// Start listening for packets from LFS.
outgauge.Connect("127.0.0.1", 30001);
// Stop program from exiting while listening.
Console.ReadLine();
}
serialport.Close();
}
static void outgauge_TimedOut(object sender, EventArgs e)
{
Console.WriteLine("OutGauge timed out!");
}
}
}
And arduino code:
#include <Messenger.h>
#include <LiquidCrystal.h>
#define LCD_LINES 2
#define LCD_ROWS 16
Messenger message;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void sendVersion()
{
Serial.println(F("AD 01.12 ArduDash"));
}
void sendOK()
{
Serial.println(F("OK"));
}
void sendERROR()
{
Serial.println(F("ERROR"));
}
void setSpeedLCD(int speed)
{
lcd.setCursor(0, 0);
lcd_rjust_number(speed, 3);
lcd.print(F(" km/h"));
}
void setSpeed()
{
int speed = message.readInt();
if (speed < 3 || speed > 230)
{
sendERROR();
speed=0;
}
//setSpeedServo(speed);
setSpeedLCD(speed);
sendOK();
}
void lcd_rjust_number(int num, byte length)
{
if (num < 10)
{
lcd.print(F(" "));
}
if (num < 100)
{
lcd.print(F(" "));
}
if (num < 1000 && length == 4)
{
lcd.print(F(" "));
}
lcd.print(num);
}
void setRPM()
{
int rpm = message.readInt();
if (rpm > 10000 || rpm < 0)
{
sendERROR();
return;
}
setRPMLCD(rpm);
sendOK();
}
void setRPMLCD(int rpm)
{
lcd.setCursor(0, 1);
byte progress_width = map(rpm, 0, 10000, 0, 10);
for (byte x=0; x<10; x++)
{
if (x < progress_width)
{
lcd.print(F("#"));
}
else
{
lcd.print(F(" "));
}
}
lcd.print(" ");
lcd_rjust_number(rpm, 4);
}
void onMessage()
{
switch (message.readChar())
{
case 'S':
setSpeed();
break;
case 'R':
setRPM();
break;
}
Serial.flush();
}
void serialEvent()
{
message.process(Serial.read());
}
void setup()
{
Serial.begin(9600);
message.attach(onMessage);
lcd.begin(LCD_ROWS, LCD_LINES);
}
void loop()
{
// put your main code here, to run repeatedly:
}