0

I have this code, what I want to do is constantly obtain the weight of a scale, this through the C# serial port, but it gives me an error when I use a serial to usb converter, the error it gives me is from a device connected to the system that It doesn't work well and when I try without the converter it connects to the serial port but the weight of the scale doesn't attract me.

  private SerialPort serialPort;

    public MainForm()
    {
        InitializeComponent();
        serialPort = new SerialPort();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        serialPort.PortName = "COM1"; 
        serialPort.BaudRate = 9600;
        serialPort.DataBits = 8;
        serialPort.Parity = Parity.None;
        serialPort.StopBits = StopBits.One;

        serialPort.DataReceived += SerialPort_DataReceived;
    }

    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string data = serialPort.ReadLine();
        decimal weight = decimal.Parse(data);
        numericUpDownWeight.Invoke((MethodInvoker)(() => numericUpDownWeight.Value = weight));
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        if (!serialPort.IsOpen)
            serialPort.Open();
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        if (serialPort.IsOpen)
            serialPort.Close();
    }
David
  • 208,112
  • 36
  • 198
  • 279
  • What do you mean with "Try without the converter"? Do you actually have a physical serial port on your computer you can connect to? Also note that usb to serial converters are somewhat infamous for being unreliable, so a failure does not necessarily mean anything wrong with the software. – JonasH Jun 02 '23 at 14:15
  • I tried it with a converter but it gives me the error the device does not work well, I also tried it with a serial port tester and the port works normally but when I try it with my code it gives me the aforementioned error and when I try it with a physical serial port connect the port but the weight doesn't appeal to me – Damian Uscapi Baez Jun 02 '23 at 14:23
  • RS-232 (9 pin connector on PC) is a +/-12 volt. Many devices today are 5V and need an adaptor like the ones from AdaFruit (SEE : https://www.adafruit.com/) to convert between 5V and 12V. There are also devices which convert between RS-232 and USB. The ICs in the adaptors are made by Siliconix and work well if you use the drivers from manufacture and do not work well if other drivers are installed. There are also Chinese clones of the IC that do not work well. I will look at code to see if there are errors. – jdweng Jun 02 '23 at 14:45
  • What is the error message exactly ? ... and why you hard coded `COM1` when you connect a serial port to your machine it can take any available port ... take a look on [How to get connected Port names](https://stackoverflow.com/questions/6046298/how-to-get-port-name-in-c), and you can display in drop down list and user can choose – Ibram Reda Jun 02 '23 at 14:45
  • Make sure that your device is connected correctly, go to `device Manager` and cheek that your USB converter appear under the `COM ports` section you will get com number from here – Ibram Reda Jun 02 '23 at 14:49
  • It look like you have scale set to continuous mode since you are not sending any message to get weight. So your code must flush the data. You need to remove any character from previous weighs. You are reading data using ReadLine() and must make sure the scale is set correctly to send the correct return character. – jdweng Jun 02 '23 at 14:50
  • The scale does send the data with another program, I tried it and it gives me the weight but not with this code. – Damian Uscapi Baez Jun 02 '23 at 15:10

0 Answers0