Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How to get weight from weighing scale my code as follows..........
C#
 public Main()
 {
    InitializeComponent();
    //Initialize VID and PID TextBoxes
    VIDTextBox.Text = MyDeviceVID.ToString("X4");
    PIDTextBox.Text = MyDevicePID.ToString("X4");

    //USB Connection
    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
/// <summary>
/// Try to connect to the device.
/// </summary>
/// <returns>True if success, false otherwise</returns>
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))
    {
        //My Device is attached
        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))
    {
        //My Device is removed
        MyUSBDeviceConnected = false;
        Disconnect();
    }
}

protected override void WndProc(ref Message m)
{
    USBPort.ProcessWindowsMessage(ref m);

    base.WndProc(ref m);
}

private void Connect()
{
    //TO DO: Inset your connection code here
    MessageBox.Show("Connected!");
    ConnectionToolStripStatusLabel.Text = "Connected";
}

private void Disconnect()
{
    //TO DO: Insert your disconnection code here
    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..............
Posted
v4
Comments
ZurdoDev 6-Aug-13 7:48am    
What's wrong with your code?
[no name] 6-Aug-13 8:34am    
"The above code show message connected all times", probably because that is all you are doing, showing a message. You need to go read the documentation for whatever scale you are using or go search the manufacturers website.
What is the problem exactly?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900