0

I am trying to get only weight from the scale to my application I try some codes but its doesn't work

I am using this code :

namespace WS_POS
{
    public partial class Test1 : XtraForm
    {
        static SerialPort _serialPort;        
        private const int BaudRate = 9600;

        public Test1()
        {
            InitializeComponent();
        }

        private void Test1_Load(object sender, EventArgs e)
        {
            string[] portNames = SerialPort.GetPortNames();     

            foreach (var portName in portNames)
            {
                comboBox1.Items.Add(portName);                  
            }

            comboBox1.SelectedIndex = 0;
        }

        private delegate void Closure();

        private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
        {
            if (InvokeRequired)     
                BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));    
            else
            {
                int dataLength = _serialPort.BytesToRead;
                byte[] data = new byte[dataLength];
                int nbrDataRead = _serialPort.Read(data, 0, dataLength);

                if (nbrDataRead == 0)
                    return;

                string str = System.Text.Encoding.UTF8.GetString(data);
                T2.Text = str.ToString();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (_serialPort != null && _serialPort.IsOpen)
                _serialPort.Close();

            if (_serialPort != null)
                _serialPort.Dispose();
   
            _serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One);       //<-- Creates new SerialPort using the name selected in the combobox
            _serialPort.DataReceived += SerialPortOnDataReceived;       //<-- this event happens everytime when new data is received by the ComPort
            _serialPort.Open();     //<-- make the comport listen
            T1.Text = "Listening on " + _serialPort.PortName + "...\r\n";
        }
    }
}

I need only get weight to text box because I needed to used for POS program

I try various snippets of code, but nothing works for me; I need to send weight from scale and receive data in my app in textbox.

Can anyone help me with this situation?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • is event ```SerialPortOnDataReceived``` being called? – Akash Kansara Jul 23 '23 at 08:29
  • No the event not calling that's my problem – Ayman Alhaj Jul 23 '23 at 08:47
  • Put break point at beginning of simpleButton1_Click. Then Step through the simpleButton1_Click() and see what happens. Serial port may already be open or you are opening a serial port that doesn't exist. Open Device Manager and check which serial ports are installed. Your cable may be wired wrong or the scale may be set to a different baud rate. Scales have two modes 1) Continuous 2) Send message to receive a weight. You may need to send a message before receiving any data. – jdweng Jul 23 '23 at 10:23
  • I am sure about the port its COM6 I checked from device manager I used before another way by using Ip address but same problem – Ayman Alhaj Jul 23 '23 at 11:06
  • Then either the cable is bad or scale is not configured properly. Is scale configured for ethernet of serial port? What baud rate? Usually I recommend using PUTTY manually to get hardware configure correctly before trying in code. Once you get PUTTY working code should be simple to debug. – jdweng Jul 23 '23 at 13:34
  • I have a connect with port but I don't receive any data I test the connection by PUTTY its works – Ayman Alhaj Jul 23 '23 at 14:00
  • The following may be of interest: https://stackoverflow.com/a/65971845/10024425 – Tu deschizi eu inchid Jul 23 '23 at 15:45
  • I try it but same problem no data receive – Ayman Alhaj Jul 24 '23 at 10:08

0 Answers0