Trying to recieve weight when requested via command over serial from a Mettler Toledo Rice Lake scale. Before anything else it works fine if I use putty to send the command; it returns properly the currently displayed weight on the print head this is in essence all I want to accomplish from these functions.
Here is my code at the moment although I have tried quite a lot of variations:
On form load it will fix variables associated with the port to follow settings that are set elsewhere in the software. This appears to work properly and does in many other parts of the software.
settings: 12: COM1 13: 9600 16: none 17: 8 18: One 19: kprint
private void Get_Weight_Load(object sender, EventArgs e)
{
string path11 = File.ReadAllText(Application.StartupPath + @"\ConfigurationPathFile.txt") + @"\GeneralSettings.txt";
path11 = path11.Replace("\r", "").Replace("\n", "");
string[] settings = File.ReadAllText(path11).Split(',');
LoadResources();
#region Scale
ScalePort.PortName = settings[12];
ScalePort.BaudRate = Int32.Parse(settings[13]);
ScalePort.Handshake = Handshake.RequestToSend; // This is one thing I have been changing in order to try and fix the problem.
switch (settings[16])
{
case "None":
ScalePort.Parity = System.IO.Ports.Parity.None;
break;
case "Odd":
ScalePort.Parity = System.IO.Ports.Parity.Odd;
break;
case "Even":
ScalePort.Parity = System.IO.Ports.Parity.Even;
break;
case "Mark":
ScalePort.Parity = System.IO.Ports.Parity.Mark;
break;
case "Space":
ScalePort.Parity = System.IO.Ports.Parity.Space;
break;
}
ScalePort.DataBits = Int32.Parse(settings[17]);
switch (settings[18])
{
case "None":
ScalePort.StopBits = System.IO.Ports.StopBits.None;
break;
case "One":
ScalePort.StopBits = System.IO.Ports.StopBits.One;
break;
case "Two":
ScalePort.StopBits = System.IO.Ports.StopBits.Two;
break;
case "OnePointFive":
ScalePort.StopBits = System.IO.Ports.StopBits.OnePointFive;
break;
}
try
{
ScalePort.Open();
ScalePort.WriteLine(settings[19]);
}
catch (Exception ex)
{
MessageBox.Show("Contact your system admin if you are seeing this message" + System.Environment.NewLine + ex.ToString());
}
#endregion
}
On form close:
private void Get_Weight_FormClosed(object sender, FormClosedEventArgs
{
if (ScalePort.IsOpen)
{ ScalePort.Close(); }
}
and the ScalePort Data received event handler:
private void ScalePort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(30);
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
textBox1.AppendText(indata + System.Environment.NewLine);
//MessageBox.Show(indata);
}
When this runs all I recieve in the textbox1 is the command I send which in this case is kprint.
So to make it clear what my question is; What am I doing wrong that is preventing me from receiving the weight or, am I sending the command wrong and if so how do I do it properly?
This is an image of the print head:
I got the inline serial port reader and this is the logs i generated my software on the left putty on the right. Each of the tests on my software are separate attempts with the port opened and closed between each attempt.
I adjusted my settings based on the mode cmd command suggested below afterward I used the mode command again I only have one thing I can not seem to change which is the timeout to off, which is set to infinite -1 on both read and wright. There was no change in result after doing this. Here is the two images After Putty right side after software left side: