0

I am reading data from a scale Mettler toledo using USB to RS232 I am successfully getting data with the following setting using C#.NET

Port.BaudRate = (int)baudRate;
Port.Handshake = Handshake.XOnXOff;Port.Parity = Parity.Even; //Even,None,Odd supported
Port.DataBits = 7;

Port.StopBits = StopBits.One;
Port.ReadTimeout = 2000;
Port.WriteTimeout = 500;
Port.ReadBufferSize = 2048;
Port.ReceivedBytesThreshold = 1;
Port.WriteBufferSize = 1024;
Port.DtrEnable = true; //enable Data Terminal Ready
Port.RtsEnable = false; //enable Request to send

the setting on my display toledo is set to continuous output

I am able to get the weight in real-time, but it comes with a bunch of data as follows

A*0 15 00

or

A*0 15 0

or

enter image description here

The first set may vary but the middle one which is the weight changes according to what is on the scale

I did try to remove special characters using regex, reducing the white spaces and splitting the data, to get only the middle one but it wasn't successful

What I want to achieve is only to get the weight [middle value]

ASh
  • 34,632
  • 9
  • 60
  • 82
  • The following may be helpful: https://stackoverflow.com/a/70614758/10024425 – Tu deschizi eu inchid Dec 21 '22 at 19:41
  • Those are most likely control characters. – Tu deschizi eu inchid Dec 21 '22 at 19:44
  • The following may be helpful as well: https://stackoverflow.com/a/69946343/10024425, [SerialPort.DataReceived Event](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=dotnet-plat-ext-7.0#remarks), [SerialDataReceivedEventArgs.EventType](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialdatareceivedeventargs.eventtype?view=dotnet-plat-ext-7.0), [SerialData Enum](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialdata?view=dotnet-plat-ext-7.0), – Tu deschizi eu inchid Dec 21 '22 at 19:57
  • I will try that and let u know – Master Tesh Dec 22 '22 at 06:34
  • I tried but not working – Master Tesh Dec 22 '22 at 13:39
  • A*0 15 00 or A*0 15 0 same result – Master Tesh Dec 22 '22 at 13:42
  • Please add the model number of the scale and/or a reference to the documentation for the scale. – Tu deschizi eu inchid Dec 22 '22 at 16:13
  • mettler teledo IND331 – Master Tesh Dec 23 '22 at 10:02
  • Please add the model number information to the original post. – Tu deschizi eu inchid Dec 23 '22 at 16:57
  • [IND331 User's Guide](https://www.mt.com/dam/product_organizations/industry/Load_Cells/Downloads/Transmitter/ind131-331/manuals/IND131-331_MAN_64067481_08_EN_UG.pdf), p. 112 (C-1) states _no handshaking_ for both COM1 and COM2. Unless you've changed the values in the scale setup screen, the defaults are `Handshake.None`, `Parity.None`, `DataBits= 8`, `BaudRate = 9600`. – Tu deschizi eu inchid Dec 23 '22 at 17:47
  • p. 114 (C-3) shows the format of the message under heading C.3.1. It starts with `STX` which is `0x02` (hex: 02) and ends with a carriage return. Therefore, you may need to set the [SerialPort.NewLine](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.newline?view=dotnet-plat-ext-7.0) property to `NewLine= "\r"`. Then in `DataReceived` event handler: `if (e.EventType != SerialData.Chars) { return; } SerialPort sp = (SerialPort)sender; string data = sp.ReadLine(); System.Diagnostics.Debug.WriteLine("data: " + data);`. Also see [ASCII Table](https://www.asciitable.com/). – Tu deschizi eu inchid Dec 23 '22 at 18:02
  • Elsewhere in the documentation, it's mentioned that `CRLF` (`\r\n`) is used, so you may also try setting `NewLine= "\r\n"` as `A` in the ASCII table is LF (line feed). `*` in the ASCII table is `0x2A` (hex: 2A) which is `00101010` in binary. One can use the Calculator app in Windows. Switch the calculator to "Programmer" mode, click "HEX", and enter `2A`. The binary value will be displayed. Then refer to page C-3 to see what each bit represents. – Tu deschizi eu inchid Dec 23 '22 at 18:15
  • You may also consider reviewing `Little Endian` and `Big Endian` byte ordering. – Tu deschizi eu inchid Dec 23 '22 at 18:18
  • Thank you very much, my code is working fine now – Master Tesh Dec 25 '22 at 18:50
  • What did you do to fix the code? – Tu deschizi eu inchid Dec 25 '22 at 18:53
  • I did an unconventional trick based on your advice, I removed the ASCII data at the beginning of the line, I was left with the weight and the 00 at the end I created a list where I added all decimals only if (Decimal.TryParse(aux[i].Trim(), out p)) list_poids.Add(p); then from the list I grouped them based on the most recurring one, it seemed to work fine list_poids.GroupBy(i => i).OrderByDescending(grp => grp.Count()).Select(grp => grp.Key).First(); for now it seems to work, but not sure on the long run – Master Tesh Jan 05 '23 at 07:26
  • An _unconventional trick_ shouldn't be necessary. Here are some other posts of mine that contain some pieces of code that one may find useful: https://stackoverflow.com/a/68807066/10024425 and https://stackoverflow.com/a/69946343/10024425. The following may be useful for getting a bit from a byte which according to the documentation one needs to do in order to determine what the data represents: https://stackoverflow.com/a/71388371/10024425 and https://stackoverflow.com/a/72651487/10024425. – Tu deschizi eu inchid Jan 06 '23 at 17:12

0 Answers0