By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

Serial communication problem with Framework 2.0

P: n/a
Hi,

I've been able to communicate using HyperTerminal with my device via
serial port COM1. I just send a command and device switches on. I just
need that to start playing with it. This way, I understand device and
serial communication work wihout problems.

Now I want to do the same with C# code. As simple as that.

I open the serial port with the configuration manufacturer gives me,
send a command with Write() method and wait for data received event to
fire. I understand command is sent (no errors), but device is not
switched on and inmediatelly I data received event is fired. Here I
always get a "?" character, which seems to be an error character.
I'm using "Write", because in HyperTerminal I just type "DM" and device
responds. I also have tried with WriteLine() and so on.

This is my code, I would be very grateful if anybody could take a look
at it:
------------------------------------------------------------------------------------------------------------------
private void ReceiveData(object sender, SerialDataReceivedEventArgs e)
{
try
{
MessageBox.Show(serialPort.ReadExisting());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void ErrorInSerialCommunication(object sender,
SerialErrorReceivedEventArgs e)
{
MessageBox.Show("ERROR!");
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
serialPort.BaudRate = 9600;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.PortName = "COM1";
serialPort.Handshake = Handshake.None;
serialPort.DataReceived += new
SerialDataReceivedEventHandler(ReceiveData);
serialPort.ErrorReceived += new
SerialErrorReceivedEventHandler(ErrorInSerialCommu nication);
serialPort.Open();
serialPort.RtsEnable = true;
serialPort.DtrEnable = true;
serialPort.Write("DM");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}

Manufacturer tells me about FlowControl = None, I understand that
matches with Handshake = None.

Thanks very much for all your help. It's really urgent.

Jan 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Try adding a "\r" to the Write command like this:

serialPort.Write("DM\r");

We used to have to do with with some serial processing. Perhaps it
will help you.

Also, if you can find out the value of the ? that you receive back.
Perhaps it is some sort of Ack/Nak code. 6 = ack and 21 = nak
(negative ack).

I have always believed that RS232 serial communications was a 'black
art'.

Jan 18 '06 #2

P: n/a
I've read in this previous post
http://groups.google.es/group/micros...aracter&rnum=1
that could be because of the Encoding I'm using. When serial port
returns "?" character seems to mean that the serial port is not able to
decode correctly some character.

The default Encoding of the serial port is "us-ascii", I've changed
this encoding to Encoding.Default, which is in my case Western European
Encoding. Does not work again!

The specifications of the manufacturer are the following:

------------------------------------------------------------------------------------
Serial commands and responses are encoded as ASCII characters. A
checksum is generated for all messages and the
hexadecimal representation of the least significant 16 bits of the
checksum (a blank followed by 4 characters) is
placed at the end of each response message, just before the carriage
return (<CR>), line feed (<LF>) pair.

The following conditions must be met:
1. The computer must assert (apply a positive RS-232 voltage to) RTS
and/or DTR. Either or
both of these signals supply power to the cable circuitry.
2. The computer may leave RTS "open" but may not drive it to a negative
RS-232 level.
3. The computer communications port must be set to 9600 baud, 8 data
bits, no parity, and one
stop bit.

Baud Rate = 9600 bps Data Bits = 8
Stop Bits = 1 Parity = none
Flow Control = None Com Port = port # utilized
----------------------------------------------------------------------------------------------------------

Please could anyone help me? I'm sure it's a very simple thing, and
almost sure it's related to encoding and the way I'm writing in the
port. I would need a clear explanation of the steps I must follow,
always based on the manufacturer's specifications. I want to avoid
speaking with manufacturer at the moment.

With the encoding of the port changed, the character returned was not
"?"; it was a strange character, similar to a "Y". I suppose it means
the same but with different encoding.

Thanks very much.

Jan 19 '06 #3

P: n/a
Hi, it finally was a delay issue.
For a "DM" command, I was trying to send "DM" string with Write() and
WriteLine() method.

I finally achieved success in the serial communication by sending a
"D", sleeping 200 miliseconds and then sending "M" character and
sleeping again for 200 miliseconds.
It seems like the electronical engineering of the device I'm using is
quite slow and the communication must be performed this way.

That' all, just to let you know. Thanks very much.

Jan 30 '06 #4

P: n/a
ZS

Hi Lonifasiko
I'm trying to develop an application similar to yours but it does not
seem to work. I found a code on msdn that uses kernel32.dll etc... is it
necessary?

How did u instantiate your serial port.

Thank you .
-zelma
"Lonifasiko" wrote:
Hi,

I've been able to communicate using HyperTerminal with my device via
serial port COM1. I just send a command and device switches on. I just
need that to start playing with it. This way, I understand device and
serial communication work wihout problems.

Now I want to do the same with C# code. As simple as that.

I open the serial port with the configuration manufacturer gives me,
send a command with Write() method and wait for data received event to
fire. I understand command is sent (no errors), but device is not
switched on and inmediatelly I data received event is fired. Here I
always get a "?" character, which seems to be an error character.
I'm using "Write", because in HyperTerminal I just type "DM" and device
responds. I also have tried with WriteLine() and so on.

This is my code, I would be very grateful if anybody could take a look
at it:
------------------------------------------------------------------------------------------------------------------
private void ReceiveData(object sender, SerialDataReceivedEventArgs e)
{
try
{
MessageBox.Show(serialPort.ReadExisting());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void ErrorInSerialCommunication(object sender,
SerialErrorReceivedEventArgs e)
{
MessageBox.Show("ERROR!");
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
serialPort.BaudRate = 9600;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.PortName = "COM1";
serialPort.Handshake = Handshake.None;
serialPort.DataReceived += new
SerialDataReceivedEventHandler(ReceiveData);
serialPort.ErrorReceived += new
SerialErrorReceivedEventHandler(ErrorInSerialCommu nication);
serialPort.Open();
serialPort.RtsEnable = true;
serialPort.DtrEnable = true;
serialPort.Write("DM");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}

Manufacturer tells me about FlowControl = None, I understand that
matches with Handshake = None.

Thanks very much for all your help. It's really urgent.

Feb 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.