I have a weighbridge application, where I am using a serial port to get the weights from the weighbridge.
I need to take the weights two times. One with the empty lorry and then with the loaded lorry. I just wonder if I can use two serial ports, one for weighing empty lorry and the other for weighing loaded one? something like
spWeighIn = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);
spWeighOut = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);
and use them simultaneously
Thanks
This is my code:
SerialPort spWeigh;
string strResponseWeigh;
private delegate void SetTextDeleg(string text);
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
spWeigh = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);
spWeigh.RtsEnable = false;
spWeigh.DtrEnable = false;
spWeigh.Handshake = Handshake.None;
spWeigh.ReadTimeout = 10000;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (!spWeigh.IsOpen)
spWeigh.Open();
spWeigh.DataReceived += new SerialDataReceivedEventHandler(spWeigh_DataInReceived);
spWeigh.Write("W");
}
void spWeigh_DataInReceived(object sender, SerialDataReceivedEventArgs e)
{
strResponseWeigh = spWeigh.ReadLine();
string wt = strResponseWeigh.Substring(5, 7);
this.TxtFrstWt.Dispatcher.BeginInvoke(new SetTextDeleg(sin_DataReceived), new object[] { wt });
}
private void sin_DataReceived(string data)
{
TxtFrstWt.Text = data.Trim();
TxtDateIn.Text = DateTime.Now.ToString("dd/MM/yyyy");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (!spWeigh.IsOpen)
spWeigh.Open();
spWeigh.DataReceived += new SerialDataReceivedEventHandler(spWeigh_DataOutReceived);
spWeigh.Write("W");
}
void spWeigh_DataOutReceived(object sender, SerialDataReceivedEventArgs e)
{
strResponseWeigh = spWeigh.ReadLine();
string wt = strResponseWeigh.Substring(5, 7);
this.TxtFrstWt.Dispatcher.BeginInvoke(new SetTextDeleg(sout_DataReceived), new object[] { wt });
}
private void sout_DataReceived(string data)
{
TxtScndryWt.Text = data.Trim();
TxtDateOut.Text = DateTime.Now.ToString("dd/MM/yyyy");
}
}
Let me see if I got this right.
You’re saying that you have a device connected to the serial port and that you need to read the information twice from there, right?
Your code is a bit convulted when dealing with that. I’d redo it in a bit more like this:
You see, you don’t need to attach event handlers every time you click the button, that’s why the line
appears to the
MainWindow_Loadedevent handler.Trying to open a serial connection more than once might cause some problems, that too went to the loaded event handler.
If I understand the process correctly, every time the device received a
Wthrough the serial interface, it spills out the weight in the scale through the same serial port, right?This way all you need to do is to send a ‘W’ when you click the button and the results should appear on the data received event handler.