Unity / Arduino wifi communication for multiwii serial protocol
Hello, im using unity to send and recieve socket using serial protocol after converting values and assembling the socket and send it i couldn't get any answer from the arduino considering that when i send it via a socket test android application and write the socket manually (24 4d 3c 00 64 64) in Hex, i get an answer, i think it has somethink with the speed of transmission, this is my code:
`
public void setupSocket() { // Socket setup here
try {
mySocket = new TcpClient(Host, Port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
socketReady = true;
cnx.Play (); //voice assistant "connected"
}
catch (Exception e) {
Debug.Log("Socket error:" + e); // catch any exceptions
lostcnx.Play (); //Disconnected voice assistant
}
}
public void writeSocket(string theLine) { // function to write data out
if (!socketReady)
return;
string tmpString = theLine;
theWriter.WriteLine(tmpString);
theWriter.Flush();
}
public string readSocket() { // function to read data in
if (!socketReady)
return "";
if (theStream.DataAvailable)
return theReader.ReadLine();
return "NoData";
}
and this is the socket encoding and decoding code :
`
//send msp with payload
private List<Byte> requestMSP(int msp, byte[] payload)
{
if (msp < 0)
{
return null;
}
List<Byte> bf = new List<Byte>();
foreach (byte c in MSP_HEADER.ToCharArray())
{
bf.Add(c);
}
byte checksum = 0;
byte pl_size = (byte)((payload != null ? (int)(payload.Length) : 0) & 0xFF);
bf.Add(pl_size);
checksum ^= (byte)(pl_size & 0xFF);
bf.Add((byte)(msp & 0xFF));
checksum ^= (byte)(msp & 0xFF);
if (payload != null)
{
foreach (byte b in payload)
{
bf.Add((byte)(b & 0xFF));
checksum ^= (byte)(b & 0xFF);
}
}
bf.Add(checksum);
return (bf);
}
void sendRequestMSP(List<Byte> msp)
{
byte[] arr = new byte[msp.Count];
int i = 0;
foreach (byte b in msp)
{
arr[i++] = b;
}
client.writeSocket(BitConverter.ToString(arr)); // send the complete byte sequence in one go
string output=BitConverter.ToString(arr);
Debug.Log (output);/*
for (int l = 0; l < arr.Length; l ++) {
Debug.Log (arr [l]);
}*/
}
`
its a multiwii serial protocol, i've tried many possible solution the socket looks correct in the debug messages but i guess the hex value is converted in wrong way before sending, and the baudrate for arduino is 115200 i don't know if i can control the speed of transmition some how or not as in the serial port example it is possible, i appreciate any help it is for my final year project and im running out of time
Your answer
Follow this Question
Related Questions
UNet Networking problem 1 Answer
Unity [PUN] New Instantiated players cannot see previously instantiated players 0 Answers
Unity hangs when I press play (serial port, unity + arduino) 0 Answers
Mirror network error : There is already a player for this connection. 1 Answer
Using serial makes game very laggy 0 Answers