public void Begin(string ipAddress, int port) { // Give the network stuff its own special thread var thread = new Thread(() => { // We'll use `LowPassFilter` to filter out some incorrect readings coming from the sensor var filter = new LowPassFilter(0.95f); // This class makes it super easy to do network stuff var client = new TcpClient(); // Change this to your devices real address client.Connect(ipAddress, port); var stream = new StreamReader(client.GetStream()); // We'll read values and buffer them up in here var buffer = new List <byte>(); while (client.Connected) { // Read the next byte var read = stream.Read(); // We split readings with a carriage return, so check for it if (read == 13) { // Once we have a reading, convert our buffer to a string, since the values are coming as strings var str = Encoding.ASCII.GetString(buffer.ToArray()); // We assume that they're floats var dist = float.Parse(str); // Ignore any value outside of our expected input range dist = Mathf.Clamp(dist, _minInputY, _maxInputY); // Use the `LowPassFilter` to smooth out values filter.Step(dist); // Remap the value from our input range to our planes movement range CurrentValue = filter.SmoothedValue.Remap(_minInputY, _maxInputY, _minFinalY, _maxFinalY); // Clear the buffer ready for another reading buffer.Clear(); } else { // If this wasn't the end of a reading, then just add this new byte to our buffer buffer.Add((byte)read); } } }); thread.Start(); }
/*void SocketSend(string sendStr) * { * //清空发送缓存 * sendData=new byte[1024]; * //数据类型转换 * sendData=Encoding.ASCII.GetBytes(sendStr); * //发送给指定客户端 * socket.SendTo(sendData,sendData.Length,SocketFlags.None,clientEnd); * }*/ void SocketReceive() { //进入接收循环 while (true) { var filter = new LowPassFilter(0.95f); //对data清零 recvData = new byte[1024]; //获取客户端,获取客户端数据,用引用给客户端赋值 recvLen = socket.ReceiveFrom(recvData, ref clientEnd); //print("message from: "+clientEnd.ToString()); //打印客户端信息 //输出接收到的数据 recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); // print(recvStr); char[] splitChar = { ' ', ',', ':', '\t', ';' }; string[] dataRaw = recvStr.Split(splitChar); RotateX = float.Parse(dataRaw[2]); RotateY = float.Parse(dataRaw[1]); RotateZ = float.Parse(dataRaw[0]); PoseX2 = float.Parse(dataRaw[3]); PoseY2 = float.Parse(dataRaw[4]); PoseZ2 = float.Parse(dataRaw[5]); // Use the `LowPassFilter` to smooth out values filter.Step(RotateX); filter.Step(RotateY); filter.Step(RotateZ); filter.Step(PoseX2); filter.Step(PoseY2); filter.Step(PoseZ2); } }
/*void SocketSend(string sendStr) * { * //清空發送緩存 * sendData=new byte[1024]; * //數據類型轉換 * sendData=Encoding.ASCII.GetBytes(sendStr); * //發送給指定客戶端 * socket.SendTo(sendData,sendData.Length,SocketFlags.None,clientEnd); * }*/ void SocketReceive() { //進入接收循環 while (true) { var filter = new LowPassFilter(0.95f); //對data清零 recvData = new byte[1024]; //獲取客戶端,獲取客戶端數據,用引用給客戶端賦值 recvLen = socket.ReceiveFrom(recvData, ref clientEnd); //print("message from: "+clientEnd.ToString()); //列印客户端信息 //輸出接收到的數據 recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); // print(recvStr); char[] splitChar = { ' ', ',', ':', '\t', ';' }; string[] dataRaw = recvStr.Split(splitChar); RotateX = float.Parse(dataRaw[0]); RotateY = float.Parse(dataRaw[1]); RotateZ = float.Parse(dataRaw[2]); RotateX2 = float.Parse(dataRaw[3]); RotateY2 = float.Parse(dataRaw[4]); RotateZ2 = float.Parse(dataRaw[5]); Flexval = int.Parse(dataRaw[6]); // Use the `LowPassFilter` to smooth out values filter.Step(RotateX); filter.Step(RotateY); filter.Step(RotateZ); filter.Step(RotateX2); filter.Step(RotateY2); filter.Step(RotateZ2); } }
/* void SocketSend(string sendStr) * { * //清空發送緩存 * sendData=new byte[1024]; * //數據類型轉換 * sendData=Encoding.ASCII.GetBytes(sendStr); * //發送給指定客戶端 * clientSocket.Send(sendData,sendData.Length,SocketFlags.None); * }*/ //伺服器接收 void SocketReceive() { //連接 SocketConnet(); //接收迴圈 while (true) { sw.Start(); for (int i = 0; i < 10; i++) { var filter = new LowPassFilter(0.95f); //清空數據 recvData = new byte[1024]; //獲取收到的數據長度 recvLen = clientSocket.Receive(recvData); //如果收到的數據長度為0,則重新連線進入下一個循環 if (recvLen == 0) { SocketConnet(); continue; } //輸出接收到的數據 recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); print(recvStr); //分割字串 char[] splitChar = { ' ', ',', ':', '\t', ';' }; string[] dataRaw = recvStr.Split(splitChar); RotateX = int.Parse(dataRaw[0]); RotateY = int.Parse(dataRaw[1]); RotateZ = int.Parse(dataRaw[2]); filter.Step(RotateX); filter.Step(RotateY); filter.Step(RotateZ); } sw.Stop();//碼錶停止 string result1 = sw.Elapsed.TotalMilliseconds.ToString(); print(result1); } }
void SocketReceive() { //接收資料端 while (true) { var filter = new LowPassFilter(0.95f); //清空資料緩存 recvData = new byte[1024]; //獲取客戶端送來的資料 recvLen = socket.ReceiveFrom(recvData, ref clientEnd); //輸出接收到的資料 recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); // print(recvStr); //分割字串 char[] splitChar = { ' ', ',', ':', '\t', ';' }; string[] dataRaw = recvStr.Split(splitChar); RotateX = int.Parse(dataRaw[0]); RotateY = int.Parse(dataRaw[1]); RotateZ = int.Parse(dataRaw[2]); RotateX2 = int.Parse(dataRaw[3]); RotateY2 = int.Parse(dataRaw[4]); RotateZ2 = int.Parse(dataRaw[5]); AccX = int.Parse(dataRaw[6]); AccY = int.Parse(dataRaw[7]); AccZ = int.Parse(dataRaw[8]); AccX2 = int.Parse(dataRaw[9]); AccY2 = int.Parse(dataRaw[10]); AccZ2 = int.Parse(dataRaw[11]); // FlexValue = float.Parse(dataRaw[6]); // 使用低通濾波 filter.Step(RotateX); filter.Step(RotateY); filter.Step(RotateZ); filter.Step(RotateX2); filter.Step(RotateY2); filter.Step(RotateZ2); //filter.Step(FlexValue); } }