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.