I have an issue with reading the weight from a weighing scale.
My Main simply connects via tcp to a remote host and reads the stream to catch the weight.
public static void Main(String[] args)
{
TcpClient tcpClient = Connect.ConnectionSetup(IPAddress.Parse(ipAddress), portNumber);
NetworkStream netStream = tcpClient.GetStream();
while (true)
{
var receivedString = String.Empty;
var timestamp = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss.fff");
var response = Response.Read(netStream, receivedString);
Console.WriteLine($"{timestamp} - {response}");
Thread.Sleep(500);
}
}
The Read method writes the command READ and reads from the stream to return the weight as a string. It is built like this
private static string command = "READrn";
public static string Read(NetworkStream netStream, string receivedString)
{
byte[] commandBuffer;
byte[] readingBuffer;
commandBuffer = Encoding.UTF8.GetBytes(command);
netStream.Write(commandBuffer, 0, commandBuffer.Length);
if (netStream.DataAvailable)
{
readingBuffer = new byte[256];
var response = netStream.Read(readingBuffer, 0, readingBuffer.Length);
receivedString += Encoding.ASCII.GetString(readingBuffer, 0, response);
}
return receivedString;
}
Reading a single weighing was simple, but how can i do to read a double weighing in order to sum the two weights?
Knowing that the string received from the scale is built like this one:
ST,NT, 0,kg
I tried this DoubleWeighing method to catch the total weight from the two weighings but it doesn’t work. This method makes two readings: catches the first response and extracts the weight, catches the second and does the same. If the time difference between the two is less than 60 seconds it means there has been a second weighing. So it sums the two weights and replaces the weight in the second response with the new weight to return the string. If there hasn’t been a double weighing than it returns simply the first response.
public static string DoubleWeighing (NetworkStream netStream, string receivedString)
{
var firstResponse = Read(netStream, receivedString).Trim();
string[] splittedFirstResponse = firstResponse.Split(',');
var firstWeight = Convert.ToDouble(splittedFirstResponse[2]);
DateTime firstWeightTime = DateTime.Now;
var secondResponse = Read(netStream, receivedString).Trim();
string[] splittedSecondResponse = secondResponse.Split(',');
var secondWeight = Convert.ToDouble(splittedSecondResponse[2]);
DateTime secondWeightTime = DateTime.Now;
TimeSpan timeDifference = secondWeightTime - firstWeightTime;
if (secondWeight != firstWeight && secondWeight != 0 && timeDifference.TotalSeconds <= 60)
{
var totalWeight = firstWeight + secondWeight;
string output = secondResponse.Replace(splittedSecondResponse[2], Convert.ToString(totalWeight));
return output;
}
else
{
return firstResponse;
}
}
This returns Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
Am I wrong with this way to think of a double weighing? There is a simplier way that can work?
16