static void SendTCPAndUpdateDO(TcpClient client, NetworkStream stream, int message_int, bool showVerboseMessage) { TCPDigitalIOTranslator translator = new TCPDigitalIOTranslator(showVerboseMessage); //StaticDIO staticDIO = new StaticDIO(showVerboseMessage); byte[] responsebyte = { 0, 0, 0, 0 }; responsebyte = ConnectAndSend(client, stream, message_int); Console.WriteLine("TCP Response: {0:X}.", translator.getDOfromTCPResponse(responsebyte)); Console.WriteLine("TCP Response: {0}.", translator.getStatusStringfromTCPResponse(responsebyte)); //staticDIO.UpdateStaticDO(translator.getDOfromTCPResponse(responsebyte)); }
static void Main(string[] args) { String IP = "127.0.0.1"; Int32 port = 1700; TcpClient client = new TcpClient(IP, port); NetworkStream stream = client.GetStream(); stream.ReadTimeout = 2000; stream.WriteTimeout = 2000; int message_int = 0; bool manualInput = true; bool showVerboseMessage = false; TCPDigitalIOTranslator translator = new TCPDigitalIOTranslator(showVerboseMessage); StaticDIO staticDIO = new StaticDIO(showVerboseMessage); //byte[] message_test = {} byte projectNum = 2; byte robotNum = 0; byte command = 1; Console.WriteLine("{0}", translator.getTCPCommand(projectNum, robotNum, command)); byte[] response_test = { 0, 202, 0, 6 }; string yourByteString = Convert.ToString(translator.getDOfromTCPResponse(response_test), 2).PadLeft(16, '0'); Console.WriteLine("{0:X}", translator.getDOfromTCPResponse(response_test)); Console.WriteLine(yourByteString); //Thread.Sleep(2000000); while (true) { if (manualInput) { Console.WriteLine(""); Console.Write("Value to send: "); message_int = Convert.ToInt32(Console.ReadLine()); } else { staticDIO.UpdateStaticDI(); Console.WriteLine("TCP command: {0}. ProjectNum: {1}. RobotNum: {2}. Command: {3}", translator.getTCPCommand(staticDIO.ProjectNum, staticDIO.RobotNum, staticDIO.Command), staticDIO.ProjectNum, staticDIO.RobotNum, staticDIO.Command); message_int = translator.getTCPCommand(staticDIO.ProjectNum, staticDIO.RobotNum, staticDIO.Command); } if (CheckMessageValidity(message_int)) { Console.WriteLine("Message is valid."); if (staticDIO.Execute == 1 || manualInput) { SendTCPAndUpdateDO(client, stream, message_int, showVerboseMessage); } } else { Console.WriteLine("Message is invalid."); } //UpdateAllStatus(client, showVerboseMessage); Thread.Sleep(200); Console.WriteLine(""); } }