Problem lendo a porta serial C # .net 2.0 para obter a saída da máquina de pesagem
Estou tentando ler o peso do modelo Sartorius Weighing Scale no BS2202S usando o código a seguir em C # .net 2.0 em uma máquina Windows XP:
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 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;
}
Consigo ler o peso usando o aplicativo Hyperterminal (fornecido com o Windows XP) com os mesmos parâmetros de porta serial dados acima para abrir a porta. Mas, a partir do snippet de código acima, posso abrir a porta e cada vez que ela retorna dados vazio
Tentei abrir a porta usando o código fornecido esta thread de estouro de pilha, ainda retorna dados vazios.
Kindly me ajudem.