开发者

Problem reading Serial Port C#.net 2.0 to get Weighing machine output

开发者 https://www.devze.com 2023-03-11 01:07 出处:网络
I\'m trying to read weight from Sartorius Weighing Scale model No BS2202S using the following code in C#.net 2.0 on a Windows XP machine:

I'm trying to read weight from Sartorius Weighing Scale model No BS2202S using the following code in C#.net 2.0 on a Windows XP machine:

public string readWeight()
{
    string lastError = "";
    string weightData = "";
    SerialPort port = new SerialPort();
    port.PortName = "COM1";
    port.BaudRate = 9600;
    port.Parity = Parity.Even;
    port.DataBits = 7;
    port.StopBits = StopBits.One;
    port.Handshake = Handshake.RequestToSend;
    try {
        port.Open();
        weightData = port.ReadExisting();
        if(weightData == null || weightData.Length == 0) {
            lastError = "Unable to开发者_StackOverflow read weight. The data returned form weighing machine is empty or null.";
            return lastError;
        }
    }
    catch(TimeoutException) {
        lastError = "Operation timed out while reading weight";
        return lastError;
    }
    catch(Exception ex) {
        lastError = "The following exception occurred while reading data." + Environment.NewLine + ex.Message;
        return lastError;
    }
    finally {
        if(port.IsOpen == true) {
            port.Close();
            port.Dispose();
        }
    }
    return weightData;
}

I'm able to read the weight using Hyperterminal application (supplied with Windows XP) with the same serial port parameters given above for opening the port. But from the above code snippet, I can open the port and each time it is returning empty data.

I tried opening port using the code given this Stack Overflow thread, still it returns empty data.

Kindly assist me.


I know this is probably old now ... but for future reference ...

Look at the handshaking. There is both hardware handshaking and software handshaking. Your problem could be either - so you need to try both.

For hardware handshaking you can try:

        mySerialPort.DtrEnable = True
        mySerialPort.RtsEnable = True

Note that

        mySerialPort.Handshake = Handshake.RequestToSend

I do not think sets the DTR line which some serial devices might require

Software handshaking is also known as XON/XOFF and can be set with

        mySerialPort.Handshake = Handshake.XOnXOff

OR

        mySerialPort.Handshake = Handshake.RequestToSendXOnXOff

You may still need to enable DTR

When all else fails - dont forget to check all of these combinations of handshaking.


Since someone else will probably have trouble with this in the future, hand shaking is a selectable option.

In most of the balances you will see the options for Software, Hardware 2 char, Hardware 1 char. The default setting for the Sartorius balances is Hardware 2 Char. I usually recommend changing to Software.

Also if it stops working all together it can often be fixed by defaulting the unit using the 9 1 1 parameter. And then resetting the communication settings.

An example of how to change the settings can be found on the manual on this page:

http://www.dataweigh.com/products/sartorius/cpa-analytical-balances/

0

精彩评论

暂无评论...
验证码 换一张
取 消