Serial communication problem with Framework 2.0

L

Lonifasiko

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(ErrorInSerialCommunication);
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.
 
C

Chris Dunaway

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'.
 
L

Lonifasiko

I've read in this previous post
http://groups.google.es/group/micro.../997fb4a0db0ac325?q=serial+?+character&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.
 
L

Lonifasiko

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top