0

Iam developing a WPF application to interact with ELM327. The communication medium between myApp and ELM327 is USB protocol. ELM327 is connected to vehicle through OBD port. My app able to establish USB communication successfully. Whatever command Iam sending from my App, Iam getting ? as reply. I set baud rate as 9600. For example, I sent ATZ, iam getting ? as reply. I sent 010D, I got ?. I tried with the app comes along with ELM327, that app can successfully able to extract data.

MyApp USB communication connect code:

public SerialPort sport;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int baudValue=0;
            if (cbBaud.Text == null)
                MessageBox.Show("select a baud rate");
            else
            {
                if (cbBaud.Text != null)
                {
                    baudValue = int.Parse(cbBaud.Text);
                }
                serialConnect(baudValue);
            }




        }

        public void serialConnect(int baudRate)
        {
            try
            {
                if (tbCOM.Text != null)
                {
                    sport = new System.IO.Ports.SerialPort(tbCOM.Text,
                                                          baudRate);
                    if (!sport.IsOpen)
                    {
                        sport.Open();
                        sport.DataReceived += new SerialDataReceivedEventHandler(serialDataReceived);
                        elIndicator.Fill = Brushes.Green;
                    }
                    else
                    {
                        MessageBox.Show("Connection already Opened");
                    }
                }
            }
            catch(Exception EX)
            {
                MessageBox.Show("Connection Error");
            }

        }

MyApp data sent code:

private void BtSent_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                sport.WriteLine(tbSend.Text);
            }
            catch(Exception EX)
            {
                MessageBox.Show("Write Error");
            }
        }

MyApp data receive code:

private void serialDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    this.Dispatcher.Invoke((Action)(() =>
    {
        recData.Text = "\n";
        recData.Text = System.Environment.NewLine + sport.ReadExisting();
    }));

}

Is there any initialization code amI have to sent ?

deepan muthusamy
  • 321
  • 2
  • 14
  • Try appending `\r` instead of `\n` to your commands. Does that change anything? – DrMickeyLauer Oct 08 '19 at 10:34
  • I am not appending anything with the command. "/n" you are seeing in serielDataRecieved method is to put new line for the newly coming data. "sport.WriteLine(tbSend.Text);" this line of code sends the command to ELM327. – deepan muthusamy Nov 21 '19 at 09:53
  • I see, well, ELM327 commands are to be terminated with `\r`, so you should add that and see whether it makes a difference. – DrMickeyLauer Nov 21 '19 at 13:51

0 Answers0