-1

I had an application that retrieves data from an EXA brand scale, from the serial port c #. I would like to do the same thing, but this time by another brand, KERN PFB, the problem is that the SerialDataReceivedEventHandler event never executes. I do not know what I missing here .

I tried with this : How do I use dataReceived event of the SerialPort Port Object in C#?

    public static  SerialPort mySerialPort = new SerialPort("COM1");
    public static string indata = string.Empty;

    public static void SetSetting()
    {
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.RtsEnable = true;

        mySerialPort.DataReceived += MySerialPort_DataReceived;
    }

    public static void MySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        indata += sp.ReadExisting();
    }

    public static float GetPesage()
    {
        indata = string.Empty;
        mySerialPort.Open();
        Thread.Sleep(1000);

        string[] dataSplited = indata.Trim().Split(' ');            
        float poids = 0;
        //
        ...
        //

        mySerialPort.Close();
        return poids;
    }

I expect data is received but isn't.

Thank you.

mlamsarf
  • 24
  • 6
  • So it works perfectly with device X but not with device Y? More likely a baud/parity/stop/data/handshake/rts config problem? – Caius Jard Jan 09 '19 at 12:19
  • Where are you calling the SetSetting() method? – jdweng Jan 09 '19 at 12:20
  • Yes ,it works perfectly with device X but not with device Y.@CaiusJard – mlamsarf Jan 09 '19 at 14:01
  • I called the SetString() method in the sbPeser_Click like this: private void sbPeser_Click(object sender, EventArgs e) { SetSetting(); float poids = GetPesage(); // ... // } @jdweng – mlamsarf Jan 09 '19 at 14:07
  • The problem is the event handler never gets called even though data is being received. – mlamsarf Jan 09 '19 at 14:24
  • What criteria are you using to determine that data is being received? The only way you can tell is if you put a break point in the event and you are saying the event is not be called. – jdweng Jan 09 '19 at 14:39
  • @jdweng : Yes i used the breakpoint,into the event for that i said the event is not be called. – mlamsarf Jan 10 '19 at 08:15
  • I was think the problem in the precision scale,but when i use **'putty'** [link](https://www.putty.org/) i managed to get data but when i use my application it does not work – mlamsarf Jan 10 '19 at 08:26
  • So the real problem is you get data with putty but not with serial port. Putty and serial port have lots of options and you did not set them the same. The two common issues are 1) You first have to send data before receiving and you are not sending (send missing the return at end) 2) The return character in putty is different. – jdweng Jan 10 '19 at 11:49
  • @jdweng : i'm agree with you but i use **Putty** to make sure the problem is not about the **precision scale** or the cable **rs232**,the problem is about the configuration the **Serial Port** or i miss something – mlamsarf Jan 10 '19 at 16:01
  • @Hans Passant. Any help here! – mlamsarf Jan 10 '19 at 16:04
  • No!!! Putty is probably sending a return when it opens and your code isn't sending anything. – jdweng Jan 10 '19 at 16:26
  • @jdweng : I forget something or what – mlamsarf Jan 10 '19 at 16:30
  • Most scales require sending a command before they constantly send the weight. Some just require a return to be sent. You have to read the manual to see what is required. Also check the settings on the scale to make sure they are set to 9600, no parity. 8 bits, 1 stop and the return character being used. When you open putty you may just be automatically hitting the return character and not think about it. I think after the Port is opened you need to add a send message. The first message you receive code may get only a partial message and the code has to handle the short message. – jdweng Jan 10 '19 at 16:51
  • @jdweng : I use a application 'KERN.BalanceConnection.4' [link](https://balance-express.com/fr/logiciels/2525-logiciel-balance-connection-scd-40-4045761126435.html) i managed to get data.Note : for 'Putty' and 'KERN.Balance Connection.4' i use the same configuration in my own code but it still not work. – mlamsarf Jan 10 '19 at 17:07
  • I need the model of scale and manufacturer. You may be buying the scale through a distributor who puts his own name on the scale. I just need to read the English software manual to figure out what the Send Command is to start the scale. The link you provided looks like a software house that wells software to work with any brand scale. I few times a year people post similar questions about using scales (or serial port issues) and have to read the manual to get the answer. – jdweng Jan 10 '19 at 17:31
  • @jdweng : My own code it's work with brand **EXA** but when i use another brand **KERN PFB** it's still not work – mlamsarf Jan 10 '19 at 17:41
  • Try adding after port is open and a CR and LF : mySerialPort.Write(new byte[] {0x0D, 0x0A},0,2); I really think you are getting an exception in the GetPesage(). Add an exception handler (try/catch) and see what happens. – jdweng Jan 10 '19 at 18:09
  • Finally I found the solution.The trick is to add 'mySerialPort.Write("s");' after 'mySerialPort.Open();' Thank you @jdweng for your help. – mlamsarf Jan 15 '19 at 08:28

1 Answers1

0
 Command S : Stable weighing value for the weight is sent via the interface
public static float GetPesage()
{
    indata = string.Empty;
    mySerialPort.Open();
    mySerialPort.Write("s");
    Thread.Sleep(1000);
    string[] dataSplited = indata.Trim().Split(' ');            
    float poids = 0;
    //
    ...
    //
    mySerialPort.Close();
    return poids;
}
mlamsarf
  • 24
  • 6