1

I have a robotic system that can be interacted with over TCP/IP. I have been able to control the system in Matlab using the following code:

AT = tcpip('cim-up',8000);
fopen(AT);
fprintf(AT, '$global[1] = 33');

I need to emulate the same command in C#. I have tried the following code:

// Connect to Robot using TCPIP

string IP = "cim-up";
TcpClient tcpclnt = new TcpClient();

Console.WriteLine("Connecting.....");

try
{
    tcpclnt.Connect(IP, 8000);
    Console.WriteLine("Connected");
}
catch
{
    Console.WriteLine("Failed");                
}

StreamWriter AT_writer = new StreamWriter(tcpclnt.GetStream(), Encoding.ASCII);

AT_writer.Write("$global[1]=33");

This code will connect to the TCP/IP address but the server does not respond to the $global[1]=33 command.

I have also tried the following:

Byte[] data = System.Text.Encoding.ASCII.GetBytes("$global[1]=33");         

// Get a client stream for reading and writing.

NetworkStream stream = tcpclnt.GetStream();

// Send the message to the connected TcpServer. 
stream.Write(data, 0, data.Length);

Does anyone have any suggestions as I have a successful Matlab implementation?

Thanks

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • What is the problem? – PepitoSh Jun 06 '18 at 01:44
  • Maybe you have to flush the stream? – PepitoSh Jun 06 '18 at 01:45
  • This code will connect to the TCP/IP address but the server does not respond to the $global[1]=33 command. - just added this to the main article as (thanks to you) i realised i hadn't mentioned my issue – Christopher Wright Jun 06 '18 at 01:51
  • i have just added `AT_writer.Flush();` after `AT_writer.Write("$global[1]=33");` but this did not help – Christopher Wright Jun 06 '18 at 01:51
  • Isn't matlab sending "C-like" strings ? (null-terminated). Maybe you could take inspiration from https://stackoverflow.com/questions/2793548/how-to-get-a-null-terminated-string-from-a-c-sharp-string – Pac0 Jun 06 '18 at 02:12
  • (basically, try to add `char.MinValue` at the end of your string, and then send `ASCIIEncoding.ASCII.GetBytes(yourString)` – Pac0 Jun 06 '18 at 02:14
  • so after packet sniffing matlab adds 0a to the string which is a line feed. Therefore the solution is `AT_writer.Write("$global[1]=33\n"); AT_writer.Flush();` – Christopher Wright Jun 06 '18 at 02:45
  • In c# a \n is 0x0c, 0x0a (windows). In matlab is is \r\n. That maybe why you are using the Flush(). – jdweng Jun 06 '18 at 02:59
  • @ChristopherWright nice reverse engineering with packet sniffing. You should then write this as an answer to your own question. – Pac0 Jun 06 '18 at 03:03

0 Answers0