How to get weight from weighing scale my code as follows..........
public Main()
{
InitializeComponent();
VIDTextBox.Text = MyDeviceVID.ToString("X4");
PIDTextBox.Text = MyDevicePID.ToString("X4");
USBPort = new USBClass();
USBDeviceProperties = new USBClass.DeviceProperties();
USBPort.USBDeviceAttached += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceAttached);
USBPort.USBDeviceRemoved += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceRemoved);
USBPort.RegisterForDeviceChange(true, this);
USBTryMyDeviceConnection();
MyUSBDeviceConnected = false;
}
#region USB
private bool USBTryMyDeviceConnection()
{
if (USBClass.GetUSBDevice(uint.Parse(VIDTextBox.Text, System.Globalization.NumberStyles.AllowHexSpecifier), uint.Parse(PIDTextBox.Text, System.Globalization.NumberStyles.AllowHexSpecifier), ref USBDeviceProperties, SerialPortCheckBox.Checked))
{
DeviceTypeTextBox.Text = USBDeviceProperties.DeviceType;
FriendlyNameTextBox.Text = USBDeviceProperties.FriendlyName;
DeviceDescriptionTextBox.Text = USBDeviceProperties.DeviceDescription;
DeviceManufacturerTextBox.Text = USBDeviceProperties.DeviceManufacturer;
DeviceClassTextBox.Text = USBDeviceProperties.DeviceClass;
DeviceLocationTextBox.Text = USBDeviceProperties.DeviceLocation;
DevicePathTextBox.Text = USBDeviceProperties.DevicePath;
DevicePhysicalObjectNameTextBox.Text = USBDeviceProperties.DevicePhysicalObjectName;
SerialPortTextBox.Text = USBDeviceProperties.COMPort;
Connect();
return true;
}
else
{
Disconnect();
return false;
}
}
private void USBPort_USBDeviceAttached(object sender, USBClass.USBDeviceEventArgs e)
{
if (!MyUSBDeviceConnected)
{
if (USBTryMyDeviceConnection())
{
MyUSBDeviceConnected = true;
}
}
}
private void USBPort_USBDeviceRemoved(object sender, USBClass.USBDeviceEventArgs e)
{
if (!USBClass.GetUSBDevice(MyDeviceVID, MyDevicePID, ref USBDeviceProperties, false))
{
MyUSBDeviceConnected = false;
Disconnect();
}
}
protected override void WndProc(ref Message m)
{
USBPort.ProcessWindowsMessage(ref m);
base.WndProc(ref m);
}
private void Connect()
{
MessageBox.Show("Connected!");
ConnectionToolStripStatusLabel.Text = "Connected";
}
private void Disconnect()
{
MessageBox.Show("Disconnected!");
ConnectionToolStripStatusLabel.Text = "Disconnected";
DeviceTypeTextBox.Text = String.Empty;
FriendlyNameTextBox.Text = String.Empty;
DeviceDescriptionTextBox.Text = String.Empty;
DeviceManufacturerTextBox.Text = String.Empty;
DeviceClassTextBox.Text = String.Empty;
DeviceLocationTextBox.Text = String.Empty;
DevicePathTextBox.Text = String.Empty;
DevicePhysicalObjectNameTextBox.Text = String.Empty;
}
#endregion
private void VIDTextBox_Leave(object sender, EventArgs e)
{
uint VID = 0;
if (!uint.TryParse(VIDTextBox.Text, System.Globalization.NumberStyles.AllowHexSpecifier, new System.Globalization.CultureInfo("en-US"), out VID))
{
VIDTextBox.Focus();
ErrorProvider.SetError(VIDTextBox, "VID is expected to be an hexadecimal number, allowed characters: 0 to 9, A to F");
}
else
{
ErrorProvider.SetError(VIDTextBox, "");
}
}
private void PIDTextBox_Leave(object sender, EventArgs e)
{
uint PID = 0;
if (!uint.TryParse(PIDTextBox.Text, System.Globalization.NumberStyles.AllowHexSpecifier, new System.Globalization.CultureInfo("en-US"), out PID))
{
PIDTextBox.Focus();
ErrorProvider.SetError(PIDTextBox, "PID is expected to be an hexadecimal number, allowed characters: 0 to 9, A to F");
}
else
{
ErrorProvider.SetError(PIDTextBox, "");
}
}
private void UpdateButton_Click(object sender, EventArgs e)
{
USBTryMyDeviceConnection();
}
The above code show message connected all times..............