serial communication in C#

Started by
4 comments, last by da_cobra 17 years ago
I'm trying to communicate with a digital voltmeter through IR communication which failes for now : All I have is this document : specification for Fluke 189 Voltmeter I can open my connection :

SerialPort sp = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);

// open serial port
sp.Open();
                
// set read time out to 500 ms
sp.ReadTimeout = 500;        



Sending works, without any errors, but when I try to receive I get a timeout error.

private void btnGetID_Click(object sender, EventArgs e)
        {
            try
            {
                //write line to serial port
                //sp.WriteLine(txtText.Text);
                sp.WriteLine("ID\r");
                
                //clear the text box
                txtText.Text = "";
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            try
            {
                //clear the text box
                txtTextReceive.Text = "";
                                
                //read serial port and displayed the data in text box
                txtTextReceive.Text = sp.ReadLine();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



I'm 100% sure that the cable and the settings are correct, because I have an official application that can connect to my Voltmeter on the COM3 port with those settings. Maybe it has something to do with the carriage return? => <CR> or \r ?!? I hope someone can help me out here. Thanx in advance...
Advertisement
What happens if you use sp.Write("ID\r") instead of sp.WriteLine("ID\r")? I don't know if it makes a difference but I suppose sp.WriteLine("ID\r") sends "ID\r\r\n" on windows (the documentation states that it appends Environment.NewLine).
Try connecting to the device with HyperTerminal (connect directly to Com3, with the right parameters), then type "ID"[enter] and see what you get.

If it works with HyperTerminal, probably the .WriteLine is your problem.
If it doesn't work with HyperTerminal you have some problem with the hardware.

Also, if HyperTerminal doesn't work the first time, you might want to go in device manager, select the com port under 'ports (com & lpt) goto 'port settings', click 'advanced' and uncheck the box where it says "Use FIFO buffers (requires 16550 compatible UART)", some devices don't use a 16550 UART but a 8250 and I found it very hard to connect to this kind of devices with that option checked.
Q: How many programmers does it take to write a nice piece of software?A: MORE.
I found my problem :

A carriage return is "\r\n"
Hmm.. Weird. CR is \r and LF is \n. So the documentation should say ID<CRLF>... Oh well :)
yeah, I know it's weird, but I tried "\n" and "\r" and that didn't work, but "\r\n" did, so.... :D

This topic is closed to new replies.

Advertisement