Hi I'm a bit new to this, short story is I have a device which act as TCP server, I tested the device with Hercules Tool and it works as expected, the problem is in my code:
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace ModbusJsonTest
{
class Program
{
static void Main(string[] args)
{
const int PORT_NO = 30001;
const string SERVER_IP = "192.168.1.1";
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter((client.GetStream()));
StringBuilder request = new();
request.Append("{");
request.Append("\"funcCode\":3,");
request.Append("\"slaveId\":31,");
request.Append("\"address\":0,");
request.Append("\"quantity\":2,");
request.Append("\"interval\":0");
request.Append("}");
Console.WriteLine(request.ToString());
sw.WriteLine(request.ToString());
sw.Flush();
string data = sr.ReadLine();
while(data!=null)
{
Console.WriteLine(data);
data = sr.ReadLine();
}
client.Close();
}
}
}
This above code sends the following request:
{"funcCode":3,"slaveId":31,"address":0,"quantity":2,"interval":0}
Expecting to get the following respone:
[{"slaveId":31,"funcCode":3,"address":0,"quantity":2,"data":[4,0,0,0,235]}]
As you can see it works when using Hercules:
I checked if my code is able to send the request to the device correctly, seems like it works, I catched this message using Hercules as server (insted the device being the server)
I can't figure out what am I doing wrong, are there any useful code examples I can try ?
EDITED: the code is stuck on string data = sr.ReadLine(); the code does not continue to execute pass this...