Created
February 12, 2017 00:02
-
-
Save victoryforphil/fa746f0bf97529a86fd21aed14d246a4 to your computer and use it in GitHub Desktop.
GUI C# TCP Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Net.Sockets; | |
using System.Threading; | |
using Newtonsoft.Json; | |
namespace TitanPlanner | |
{ | |
public partial class TitanLogger : Form | |
{ | |
public class LogArray | |
{ | |
public LogItem[] Items; | |
} | |
public class LogItem | |
{ | |
public string key = null; | |
public string data = null; | |
} | |
delegate void SetTextCallback(string text); | |
TcpListener listener; | |
TcpClient client; | |
NetworkStream ns; | |
Thread t = null; | |
public TitanLogger() | |
{ | |
InitializeComponent(); | |
} | |
public void DoWork() | |
{ | |
listener = new TcpListener(7777); | |
listener.Start(); | |
client = listener.AcceptTcpClient(); | |
Console.WriteLine("Client Connectedd"); | |
ns = client.GetStream(); | |
byte[] bytes = new byte[1024]; | |
int i; | |
while (true) | |
{ | |
client.Client.Receive(bytes); | |
this.SetText(Encoding.ASCII.GetString(bytes)); | |
bytes = new byte[256]; | |
} | |
} | |
private void SetText(string text) | |
{ | |
// InvokeRequired required compares the thread ID of the | |
// calling thread to the thread ID of the creating thread. | |
// If these threads are different, it returns true. | |
if (this.listBox_logs.InvokeRequired) | |
{ | |
SetTextCallback d = new SetTextCallback(SetText); | |
this.Invoke(d, new object[] { text }); | |
} | |
else | |
{ | |
LogItem[] Items = JsonConvert.DeserializeObject<LogItem[]>(text); | |
if(Items == null) | |
{ | |
return; | |
} | |
for (int i = 0; i < Items.Length; i++) | |
{ | |
if (Items[i].key != null) | |
{ | |
if (listBox_logs.Items.Count <= i) | |
{ | |
listBox_logs.Items.Add(Items[i].key + " - " + Items[i].data); | |
} | |
else | |
{ | |
listBox_logs.Items[i] = Items[i].key + " - " + Items[i].data; | |
} | |
} | |
} | |
} | |
} | |
private void label_phonestatus_Click(object sender, EventArgs e) | |
{ | |
} | |
private void label_serverstatus_Click(object sender, EventArgs e) | |
{ | |
} | |
private void button_start_Click(object sender, EventArgs e) | |
{ | |
t = new Thread(DoWork); | |
t.Start(); | |
} | |
private void listBox_logs_SelectedIndexChanged(object sender, EventArgs e) | |
{ | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.firstinspires.ftc.teamcode; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.io.PrintWriter; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
/** | |
* Created by VictoryForPhil on 2/10/2017. | |
*/ | |
public class TitanLogger { | |
Map<String,String> CurrentData = new HashMap<String,String>(); | |
CClient mClient = new CClient(); | |
Thread myThready = new Thread(mClient); | |
Timer sendTimer = new Timer(); | |
public void ConnectToServer() { | |
myThready.start(); | |
sendTimer.scheduleAtFixedRate(new TimerTask() { | |
@Override | |
public void run() { | |
SendData(); | |
} | |
}, 1000, 100);//put here time 1000 milliseconds=1 second | |
} | |
public void SendData(){ | |
JSONArray finalData = new JSONArray(); | |
for (Map.Entry<String, String> entry : CurrentData.entrySet()){ | |
JSONObject newObj = new JSONObject(); | |
try { | |
newObj.put("data",entry.getValue()); | |
newObj.put("key",entry.getKey()); | |
finalData.put(newObj); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
if(finalData.length() != 0){ | |
mClient.Send(finalData.toString()); | |
} | |
} | |
public void Stop(){ | |
sendTimer.cancel(); | |
mClient.Close(); | |
} | |
public void AddData(String key, String data) { | |
CurrentData.put(key, data); | |
} | |
public class CClient | |
implements Runnable { | |
private Socket socket; | |
private String ServerIP = "10.0.0.7"; | |
public void run() { | |
try { | |
socket = new Socket(ServerIP, 7777); | |
} catch (Exception e) { | |
System.out.print("Whoops! It didn't work!:"); | |
System.out.print(e.getLocalizedMessage()); | |
System.out.print("\n"); | |
} | |
} | |
public void Send(String s) { | |
try { | |
PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); | |
Log.d("NETWORK", s); | |
outToServer.print(s + "\n"); | |
outToServer.flush(); | |
} catch (UnknownHostException e) { | |
System.out.print(e.toString()); | |
} catch (IOException e) { | |
System.out.print(e.toString()); | |
} catch (Exception e) { | |
System.out.print(e.toString()); | |
} | |
} | |
public void Close(){ | |
try { | |
socket.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.firstinspires.ftc.teamcode; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import com.qualcomm.robotcore.eventloop.opmode.Autonomous; | |
import com.qualcomm.robotcore.eventloop.opmode.Disabled; | |
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; | |
import com.qualcomm.robotcore.eventloop.opmode.TeleOp; | |
import com.qualcomm.robotcore.hardware.DcMotor; | |
import com.qualcomm.robotcore.util.ElapsedTime; | |
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; | |
import com.qualcomm.robotcore.eventloop.opmode.TeleOp; | |
import com.qualcomm.robotcore.eventloop.opmode.Disabled; | |
import com.qualcomm.robotcore.hardware.DcMotor; | |
import com.qualcomm.robotcore.hardware.DcMotorSimple; | |
import com.qualcomm.robotcore.util.ElapsedTime; | |
import org.firstinspires.ftc.robotcore.external.Telemetry; | |
@Autonomous(name="Titan Planner", group="Linear Opmode") // @Autonomous(...) is the other common choice | |
public class TitanOpTest extends LinearOpMode { | |
private ElapsedTime runtime = new ElapsedTime(); | |
private TitanLogger Logger = new TitanLogger(); | |
@Override | |
public void runOpMode() { | |
telemetry.addData("Status", "Initialized"); | |
telemetry.update(); | |
Logger.ConnectToServer(); | |
waitForStart(); | |
runtime.reset(); | |
while (opModeIsActive()) { | |
telemetry.addData("Status", "Run Time: " + runtime.seconds()); | |
telemetry.update(); | |
Logger.AddData("RUNTIME", runtime.toString()); | |
Logger.AddData("RUNTIME 2", runtime.toString()); | |
} | |
Logger.Stop(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment