I tried to connect the scale and retrieve the weight from visual studio 2013, but it's wire that sometimes I can get the exacted weight and sometimes I couldn't. I was not sure what's wrong with the code. Can someone help? My code is listed below
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows;
using System.Data.SqlClient;
using System.Collections;
using System.Threading;
namespace PortDataReceived
{
class PortData
{
public static void Main(string[] args)
{
try
{
string lineOne = "One";
string lineTwo = "Two";
char CarriageReturn = (char)0x0D;
string final = lineOne + CarriageReturn.ToString() + lineTwo + CarriageReturn.ToString();// define the hexvalues to the printer
SerialPort mySerialPort = new SerialPort("COM3");//initiate the new object and tell that we r using COM1
mySerialPort.BaudRate = 9600;
//mySerialPort.Parity = Parity.Odd;
//mySerialPort.StopBits = StopBits.Two;
//mySerialPort.DataBits = 7;
mySerialPort.Parity = Parity.Odd;
mySerialPort.StopBits = StopBits.Two;
mySerialPort.DataBits = 7;
mySerialPort.Handshake = Handshake.None;
mySerialPort.ReadTimeout = 20;
mySerialPort.WriteTimeout = 50;
mySerialPort.DtrEnable = true;
mySerialPort.RtsEnable = true;
//those r all the setting based on the scale requirement
/*foreach (string port in System.IO.Ports.SerialPort.GetPortNames())
{
Console.WriteLine(port);
}*/
while (true)
{
mySerialPort.Open();
mySerialPort.Write(final);
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
}
//mySerialPort.Close();
}
catch (System.IO.IOException e)
{
if (e.Source != null)
Console.WriteLine("IOException source: {0}", e.Source);
throw;
}
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
Console.ReadKey();
}
}
}