The online racing simulator
Help with insim initialiasation (C#)
Hello,

I wrote a small c# app to initialize insim through the udp port.

I'm getting an error saying:

insim: port 30000
insim - udp : unnamed (port 30000)
[RED text] InSim: Packet received before ISI packet

obviously, it did not initialise insim properly.

I've followed the C# tutorial as reference on how to marshal all the data types into a byte packet of size 44

here is my code. Note that I was too lazy to figure out how to marshal the admin and server name, but i suspect that these data values do not matter. so I just set all the bytes to 0

note: please don't laugh at my code, this is more or less my first c# app.


public byte [] initPacket()
{
//create an array for the Insim Initialisation packet
byte[] data = new byte[size];

//size of packet
data[0] = size;

//packet type
data[1] = 0x01;

//ReqI
data[2] = ReqI;

//zero value packet
data[3] = 0;

//UDP Port
data[4] = BitConverter.GetBytes(UDPPort)[0];
data[5] = BitConverter.GetBytes(UDPPort)[1];

//flags
data[6] = BitConverter.GetBytes(Flags)[0];
data[7] = BitConverter.GetBytes(Flags)[1];

//Sp0 - Set value to 0
data[8] = 0;

//prefix
data[9] = (byte)Prefix;

//interval
data[10] = BitConverter.GetBytes(Interval)[0];
data[11] = BitConverter.GetBytes(Interval)[1];

for(int i = 12; i<43; i++){
data[i] = 0;
}
data[43] = 0;
return data;
}

}

here is the code that creates the instance of my struct.

IS_ISI isInit = new IS_ISI();
isInit.type = (byte)PacketType.ISP_ISI;
isInit.ReqI = 1;
isInit.Zero = 0;
isInit.UDPPort = 30000; //use port 0
isInit.Flags = 0;
isInit.Sp0 = 0;
isInit.Prefix = '!';
isInit.Interval = 100; //100msec interval -> send data through UDP port instead of TCP


and this is the command to send the packet

sendSocket.SendTo(isInit.initPacket(),sendEndpoint);
Writing an insim app from scratch is not a good idea if you dont know enough. Go download an insim base(LFS_External etc etc).

Remember: Dont run before you can walk...
I've done Java, so all the concepts of object oriented programming is exactly the same. The only difference is marshalling, which I should have sufficient resource from example codes.

I would really like to take this opportunity as a learning process, and I believe I'm pretty close!

edit. hrm, i think I got it.

i'm not getting the red text anymore after changing the ReqI value to 0
OK - Well, For starters, Why do you do this...

//packet type
data[1] = 0x01;

data[1] Should be the packet type. Which, IIRC from that example, Should be set via the struct. Not done manually in the class.

Have a look at insim.txt in Yourlfs/docs/insim.txt.

E: All adding a Reqi higher then 0 to the ISI packet will do, Is make LFS send a VER packet on sucussfull connection.
Hmm, that's strange, I wonder why setting the ReqI to zero fixed the problem, as a non-zero ReqI should only cause InSim to sent an IS_VER packet after connecting. Out of interest, what value were you assigning to the ReqI before?
Quote from DarkTimes :Hmm, that's strange, I wonder why setting the ReqI to zero fixed the problem, as a non-zero ReqI should only cause InSim to sent an IS_VER packet after connecting. Out of interest, what value were you assigning to the ReqI before?

yea I don't think it's working whether I set ReqI to either 0 or 1

I'm still geting that random red error text

maybe my laptop is really that slow

as I said, I'm fairly new to c#, so I was just forcing values to variables to ensure they are the storing the values that I want. which was why i set reqi to 0x01

I've attached a .zip of my project. the Initialize Insim button is where all the packet sending takes place. The 2nd packet is suppose to initialise outgauge, which, doesn't work

as I said, I'm just sort of debugging the programming at the moment. Please give me user feedback! I don't have formal training in c# programming, so any tips/hints would be great in debugging methods, better algorithms etc.
Quote from pizza_delivery :yea I don't think it's working whether I set ReqI to either 0 or 1

I'm still geting that random red error text

maybe my laptop is really that slow

Wouldnt it just make sence to use the example DarkTimes made and tweak it to your liking. That is afterall, A working insim app.
brrr, can't attach files in an edited post

anyways, the 1st button "initialise insim" has all the code for sending packets.

as I said, this is a really raw version, and I'm only writing quick code to test if insim initialized properly.

the 2nd packet I am sending is supposed to initialise outgauge, which isn't working either.

please give me user feedback as to how I can improve my coding or hints on methods of debugging etc.
Attached files
G-outgauge.zip - 60.7 KB - 169 views
This works. Read over it.

E: I should add, I havnt done the OutGauge bit. Thats upto you.

I really think you should grab a already made insim base. Im not going to tell you what to do though...
Attached files
G-outgauge.zip - 65.7 KB - 169 views
Quote from pizza_delivery :brrr, can't attach files in an edited post

anyways, the 1st button "initialise insim" has all the code for sending packets.

as I said, this is a really raw version, and I'm only writing quick code to test if insim initialized properly.

the 2nd packet I am sending is supposed to initialise outgauge, which isn't working either.

please give me user feedback as to how I can improve my coding or hints on methods of debugging etc.

Well, I have a few points to mention.

First of all, InSim only allows a single UDP connection at a time, so if you are connecting with UDP you don't need to specify a separate UDPPort in the IS_ISI packet, as LFS will just use the existing one. In fact setting the UDPPort here is what is causing the ReqI error.

Second of all the second packet you're sending is actually the IS_VER packet, which does not initialise OutGauge. This is a packet that LFS replies with if the ReqI is set to non-zero. Also the array you have created to store the IS_VER packet isn't large enough and causes an IndexOutOfRangeException.

To initialise OutGauge you need to send an IS_SMALL packet with a SubT of SMALL_SSG and then set the interval the packets are to be sent using the UVal.

Hope that helps.
Thanks to all who replied.

I finally got outgauge to enable. Hopefully the rest will be easy. The reason why I want to write my own app is for learning purposes only, not only that, but the motivation!

My plan is to make a dash using led's and a lcd display. I was able to send data out through the usb port to my usb device the other day, so in theory, it shouldn't be too bad.

again, thanks for the help, I realized I made some stupid programming mistakes, but then again, it was 4am in the morning
err, more problems:

after sending the outgauge init packet right after the ISI packet, i'm getting a repetitive error message:

Insim : first bye in packet does not match size.

I did a search on teh forums, and someone mentioend outdated insim version.

I'm looking in the insim.txt and I can already see that the IS_VER.Product only has two available parameters - Demo or S1
Can I see the packet, How you built it please?
public struct IS_SMALL
{
public const byte Size = 8; // always 8
public byte Type; // always ISP_SMALL
public byte ReqI; // 0 unless it is an info request or a reply to an info request
public byte SubT; // subtype, from SMALL_ enumeration (e.g. SMALL_SSP)
public uint interval; // value (e.g. for SMALL_SSP this would be the OutSim packet rate)

public byte [] getPacket()
{
byte [] data = new byte[8];
data[0] = Size;
data[1] = Type;
data[2] = ReqI;
data[3] = SubT;
data[4] = BitConverter.GetBytes(interval)[0];
data[5] = BitConverter.GetBytes(interval)[1];
data[6] = BitConverter.GetBytes(interval)[2];
data[7] = BitConverter.GetBytes(interval)[3];

return data;
}
}

//create a constructor for outgauge init packet
IS_SMALL ogInit = new IS_SMALL();
ogInit.Type = (byte)PacketType.ISP_SMALL;
ogInit.ReqI = 0;
ogInit.SubT = (byte)smallType.SMALL_SSG;
ogInit.interval = 1000; //set time interval to 1000 msec.
Well, that looks pretty much correct to me, so the problem must lie elsewhere.

By the way, you can put your code inside [code] tags, which makes it easier to read.

FGED GREDG RDFGDR GSFDG