# serial communication in C#

This topic is 4077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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


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

//read serial port and displayed the data in text box
}
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...

##### Share on other sites
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).

##### Share on other sites
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.

##### Share on other sites
I found my problem :

A carriage return is "\r\n"

##### Share on other sites
Hmm.. Weird. CR is \r and LF is \n. So the documentation should say ID<CRLF>... Oh well :)

##### Share on other sites
yeah, I know it's weird, but I tried "\n" and "\r" and that didn't work, but "\r\n" did, so.... :D

• 10
• 19
• 14
• 19
• 15