formula1996
Registered
I would like to send speed, rpm, gear data through UDP. But i don't well in programming. Can anybody send me a basic plugin example how to send data on UDP or help me? It would be great 
public void StartUdpListener(object process) {
List<BinaryReader> m_buffers = new List<BinaryReader>();
IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);
// Create UDP client
UdpClient updClient = new UdpClient(process.pluginPort);
UdpState state = new UdpState(updClient, remoteSender, process.sessionID, m_buffers);
// Start async receiving
updClient.BeginReceive(new AsyncCallback(DataReceived), state);
}
private void DataReceived(IAsyncResult ar)
{
List<BinaryReader> m_buffers = (List<BinaryReader>)((UdpState)ar.AsyncState).m_buffers;
UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);
HandleData(receiveBytes, m_buffers);
// Restart listening for udp data packages
c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
}
public void HandleData(byte[] receiveBytes, List<BinaryReader> m_buffers)
{
MemoryStream stream = new MemoryStream(receiveBytes, 0, receiveBytes.Length, true);
BinaryReader reader = new BinaryReader(stream);
stream.Position = 0; // Rewind so stream can be read again
byte protocolVersion = reader.ReadByte();
byte packet = reader.ReadByte();
short sequence = reader.ReadInt16();
if (packet == 0 && m_buffers.Count != 0)
{
MemoryStream streamDest = new MemoryStream(m_buffers.Count * 512);
BinaryWriter dest = new BinaryWriter(streamDest);
byte[] tmp = new byte[512 - 4];
BinaryReader b = m_buffers.FirstOrDefault();
m_buffers.Remove(b);
while (b != null)
{
int remaining = (int)(b.BaseStream.Length - b.BaseStream.Position);
b.Read(tmp, 0, remaining);
dest.Write(tmp, 0, remaining);
b = m_buffers.FirstOrDefault();
m_buffers.Remove(b);
}
// Rewind so stream can be read again
streamDest.Position = 0;
try
{
ProcessDataStream(streamDest);
}
catch (Exception e)
{
this.Invoke((MethodInvoker)delegate
{
txtLog.Text = txtLog.Text + "Error: " + e.Message + Environment.NewLine;
});
}
// clear the queue
m_buffers.Clear();
}
// add to the queue
m_buffers.Add(reader);
}
private void ProcessDataStream(MemoryStream streamDest)
{
string currentLapNo = "0";
using (BinaryReader dest = new BinaryReader(streamDest))
{
short type = dest.ReadInt16();
//update score
if (type == 2)
{
string pluginSessionID = getString(dest, 16);
string sessionName = getString(dest, 512);
Boolean startSession = dest.ReadBoolean();
Boolean enterRealtime = dest.ReadBoolean();
Boolean exitRealtime = dest.ReadBoolean();
string trackName = getString(dest, 64);
uint session = dest.ReadUInt32();
double eventTime = dest.ReadDouble();
double endEventTime = dest.ReadDouble();
double trackLapDistance = dest.ReadDouble();
uint maxLaps = dest.ReadUInt32();
uint numberOfVehicles = dest.ReadUInt32();
byte gamePhase = dest.ReadByte();
byte yellowFlagState = dest.ReadByte();
byte[] sectorFlags = new byte[3];
sectorFlags[0] = dest.ReadByte();
sectorFlags[1] = dest.ReadByte();
sectorFlags[2] = dest.ReadByte();
byte startLight = dest.ReadByte();
byte numRedLights = dest.ReadByte();
double darkCloud = dest.ReadDouble();
double raining = dest.ReadDouble();
double ambientTemp = dest.ReadDouble();
double trackTemp = dest.ReadDouble();
double windx = dest.ReadDouble();
double windy = dest.ReadDouble();
double windz = dest.ReadDouble();
double onPathWetness = dest.ReadDouble();
double offPathWetness = dest.ReadDouble();
//
// Get Weather data
//
// TODO Values currently set to 0 until the weatherdata can be read from the plugin
//float darkCloud = 0f;
//float raining = 0f;
//float ambientTemp = 0f;
//float windX = 0f;
//float windY = 0f;
//float windZ = 0f;
//float onPathWetness = 0f;
//float offPathWetness = 0f;
//WeatherData weatherData = new WeatherData(darkCloud, raining, ambientTemp, windX, windY, windZ, onPathWetness, offPathWetness);
// End weather data
for (int i = 0; i < numberOfVehicles; i++)
{
uint recordID = dest.ReadUInt32();
uint mID = dest.ReadUInt32();
double xPosition = dest.ReadDouble();
double zPosition = dest.ReadDouble();
byte place = dest.ReadByte();
double lapDistance = dest.ReadDouble();
double pathLateral = dest.ReadDouble();
double speed = dest.ReadDouble();
string vehicleName = getString(dest, 64);
string driverName = getString(dest, 32);
string vehicleClass = getString(dest, 32);
short totalLaps = dest.ReadInt16();
double bestSector1 = dest.ReadDouble();
double bestSector2 = dest.ReadDouble();
double bestLapTime = dest.ReadDouble();
double lastSector1 = dest.ReadDouble();
double lastSector2 = dest.ReadDouble();
double lastLapTime = dest.ReadDouble();
double currentSector1 = dest.ReadDouble();
double currentSector2 = dest.ReadDouble();
double timeBehindLeader = dest.ReadDouble();
uint lapsBehindLeader = dest.ReadUInt32();
double timeBehindNext = dest.ReadDouble();
uint lapsBehindNext = dest.ReadUInt32();
short numberOfPitstops = dest.ReadInt16();
short numberOfPenalties = dest.ReadInt16();
Boolean inPits = dest.ReadBoolean();
byte sector = dest.ReadByte();
byte finishStatus = dest.ReadByte();
}
}
}
}
is DataPlugin_v2 project is for rFactor2 and DataPlugin project is for rFactor1?UdpClient listener = new UdpClient(6789);
IPEndPoint groupEP = new IPEndPoint(127001, 6789);
private void listen()
{
if (listener.Available > 0)
{
MessageBox.Show("There is data");
byte[] vals = listener.Receive(ref groupEP);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
listen();
}
This line cause the crash anybody can help why?sad.sin_addr.S_un.S_addr = *((ULONG*)&(((sockaddr_in*)pResult->ai_addr)->sin_addr));
import struct
import socket
# Configure listener IP & Port for UDP transmission of packed values
udp_ip = "127.0.0.1"
udp_port = 6789
unpacked_data = None
data = None
def checkStatus():
global data, udp_ip, udp_port
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((udp_ip, udp_port))
sock.settimeout(1/30)
try:
data, addr = sock.recvfrom(1024) # receiving from socket
sock.close()
return data
except:
return False
def receiveData():
global unpacked_data, data
fmt = '<' + '70' + 'f' # define structure of packed data
s = struct.Struct(fmt) # declare structure
#rx_data = net_rx(udp_ip, udp_port)
#unpacked_data = s.unpack(data) # unpack data into tuple, requires correct 'fmt'
unpacked_data = struct.unpack('64f', data[0:256])
return True
def getChannel(channel):
global unpacked_data
if channel == 'rpm':
return str(int(unpacked_data[37]))
elif channel == 'max_rpm':
return str(int(unpacked_data[63]))
elif channel == 'gear':
return str(int(unpacked_data[33])-1)
elif channel == 'speed':
return str(int(unpacked_data[7]*0.6214))
elif channel == 'track_loc':
return '-1'
elif channel == 'car_name':
return '-1'
else:
return 0