views:

619

answers:

4

So, to open up a serial port and successfully transmit data from the balance through the serial port, i need to make sure that the settings on the serialPort object match the actual settings of the balance.

Now, the question is how do i detect that the connection hasn't been established due to the settings being different? No exception is thrown by serialPort.Open to indicate that the connection has been established. Yes, the settings are valid, but if they don't match the device (balance) settings; I am in the dark as to why the weight off the balance is not being captured.

Any input here?

+1  A: 

Without knowing any more information on the format of the data you expect from your balance, only general serial port settings mismatch detection techniques are applicable.

If the UART settings are significantly incorrect, you'll likely see a lot of framing errors: when the UART is expecting a 1 stop bit, it will in fact see a 0. You can detect this with the ErrorReceived event on the port.

private void OnErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
    if ((e.EventType & SerialError.Frame) == SerialError.Frame)
    {
        // your settings don't match, try something else
    }
}
Jeffrey Hantin
this unfortunately doesn't detect the error.
gnomixa
What is the nature of the mismatch? Are the bit rates different, or just a difference in parity (7E1 vs 8N1)?
Jeffrey Hantin
currently i keep the default settings (baud, parity, stop bits etc) in the config file. We generally have all the balances programmed the same way, however some odd balance may have different settings. In this case, i want to notify the user nicely that the port wasn't open properly.
gnomixa
so I need a reliable way to track down the mismatch between the settings. Doesn't look like it exists though
gnomixa
A: 

If things are close, but still incorrect, the .NET serial port object may not even give you an error (that is, until something catastrophic occurs).

My most common serial port communication failure occurs due to mismatched baud rates. If you have a message that you know you can get an 'echo' for, try that as part of a handshaking effort. Perhaps the device you're connecting to has a 'status' message. No harm will come from requesting it, and you will find out if communication is flowing correctly.

For software handshaking (xon xoff) There's very little you can do to detect whether or not it's configured right. The serial port object can do anything from ignore it completely to have thread exception errors, depending on the underlying serial port driver implementation. I've had serial port drivers that completely ignore xon/xoff, and pass the characters straight into the program - yikes!

For hardware handshaking, the basic echo strategy for baud rate may work, depending on how your device works. If you know that it will do hardware handshaking, you may be able to detect it and turn it on. If the device requires hardware handshaking and it's not on, you may get nothing, and vice versa.

Another setting that's more rarely used is the DTR pin - data terminal ready. Some serial devices require that this be asserted (ie, set to true) to indicate that it's time to start sending data. It's set to false by default; give toggling it a whirl.

Note that the serial port object is ... finicky. While not necessarily required, I would consider closing the port before you make any changes.

Edit:

Thanks to your comments, it looks like this is your device. It says the default settings should be:

  • 1200 baud
  • Odd parity
  • 1 stop bit
  • Hardware handshaking

It doesn't specify how many data bits, but the device says it supports 7 and 8. I'd try both of those. It also says it supports 600, 1200, 2400, 4800, 9600, and 19200 baud.

If you've turned on hardware handshaking, enabled DTR (different things) and cycled through all the different baud rates, there's a good chance that it's not your settings. It could be that the serial cable that's being used may be wired incorrectly for your device. Some serial cables are 'passthrough' cables, where the 1-9 pins on one side match exactly with the 1-9 pins on the other. Then, you have 'crossover' cables, where the "TX" and "RX" cables are switched (so that when one side transmits, the other side receives, a very handy cable.)

Consider looking at the command table in the back of the manual there; there's a "print software version" command you could issue to get some type of echo back.

Robert P
I use handshake = none
gnomixa
If the handshaking matches with what your device is set to, then you should be fine.Another one that may be important to assert is DTR - data terminal ready. I'll update my answer.
Robert P
Can all balances communicate through the serial port? What should one look for to make sure it works with .net serial port control?
gnomixa
the problem I having is that connection with COM1 cant be established with .net serial port control(my app) but it works with Terminal Emulator (TeemTalk CE). Aside from the serial port settings, is there anything else to look for?
gnomixa
Define "cant be established" : what exact, precise errors are you getting?
Robert P
Also, you have to be much more specific when you say 'balance'. What brand, model, and/or device number are you working with?
Robert P
I am working with Sartorius ED224S balance and T5540 HP thin client with Windows CE 5.0 and CF 2.0. However, I am starting to think that the problem lies with mismatching serial port settings - I am trying to debug the problem remotely and can't vouch for correct balance settings.
gnomixa
By "can't be established" I mean, nothing happens. No exception is thrown when the port is opened, but no data is being read. That's why I think that the balance settings are different from the serial port settings used in the application. Remote debugging is no fun
gnomixa
I updated my answer. Also, FYI, if you edit/update your question with more info, it gets bumped to the top of SO. That way other people might be able to pitch in their ideas too.
Robert P
thanks! that helps. Where do you get the default settings from? Is there any web page that lists them (I couldn't find them on the link you provided). Again, thanks!
gnomixa
The web page there had the manual for your balance. I couldn't give a link to the manual itself because it had a javascript popup for the manual. Once it was open I just searched the pdf for "serial" until it got to the part with the information. :)
Robert P
Thanks Robert, it turned out to be a mismatch between the balance settings and the properties that were set when the port was being opened. All good now.
gnomixa
A: 

Serial ports use a very, very old communications technology that use a very, very old protocol called RS-232. This is pretty much as simple as it gets... the two end points have synchronized clocks and they test the line voltage every clock cycle to see if it is high or low (with high meaning 0 and low meaing 1, which is the opposite of most conventions... again an artifact of the protocol's age). The clock synchronization is accomplished through the use of stop bits, which are really just rest time in between bytes. There are also a few other things thrown into the more advanced uses of the protocol such as parity bits, XON/XOFF, etc, but those all ride on top of this very basic communication layer. Detecting a mismatch of the clocks on each end of the serial line is going to be nearly impossible -- you'll just get incorrect data on the recieving end. The protocol itself has no way built in to identify this situation. I am unaware of any serial driver that is smart enough to notice the input data being clocked an an inappropriate frequency. If you're using one of the error detection schemes such as parity bits, probabilistically every byte will be declared an error. In short, the best you can do is check the incoming data for errors (parity errors should be detected by your driver/software layer, whereas errors in the data received by your app from that layer will need to be checked by your program -- the latter can be assisted by the use of checksums).

rmeador
By "parity errors detected by the software layer" you mean at the time of initialization?
gnomixa
No, the parity bits are sent along with every byte transmitted over the serial link. The receiver will decode these parity bits before the data gets handed off to your application, so the error will be reported only when data is received.
rmeador
Oh, by the way, just in case I wasn't clear, there is no data transmitted at initialization time with RS-232. Handshaking or any other data exchange done as setting up the connection is part of the application-level protocol, not the underlying serial link.
rmeador
ok, so if I use ErrorReceived event will cover it?you are being a bit vague here - I would appreciate an example of how this is done.
gnomixa
My intent was not to be vague, but language-agnostic. I don't know the Compact Framework (nor any .NET, nor C#...), so I can't give you an example that will be directly relevant. I may be able to dig up a C example. Most of the time I do this low-level stuff, I'm on a microcontroller (I'm a EE)
rmeador
oh I see:) thanks for the input anyway.
gnomixa
A: 

Basically, I can conclude that there is no 100% accurate way to test whether the connection has been established with .net serial port control. Is this correct?

gnomixa
Not with just the .NET control - there is no way to determine it based on serial protocols at all (.NET or otherwise).
ctacke
You should edit your original question with this information instead of adding an answer. :)
Robert P