2014-03-28 32 views
0

我試圖讓一個應用程序使用InSim.NET來將udp的outgauge數據包重定向到與arduino連接的串口,但是我遇到了問題。InSim.NET和arduino之間的串口連接鬆動

正確傳輸幾秒鐘後,連接丟失。 控制檯中的數字和來自arduino的顯示停止刷新。 我不知道問題出在哪裏。請幫助

這是我的C#代碼:

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!"); 
     } 

    } 
} 

與Arduino的代碼:

#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: 

} 

回答

0

你解決這個問題?

我會嘗試改變SeriarlEvent代碼

while (Serial.available()) message.process(Serial.read());