Subject: | [Patch] Set baud rate |
Date: | Wed, 05 Mar 2014 21:34:24 +0100 |
To: | bug-device-elm327 [...] rt.cpan.org |
From: | Thomas Kaiser <thomas [...] kaiser-linux.li> |
Hello Alister
I just discovered your perl lib for the ELM327 device. Thank you for
providing this library.
My OBDLink device which uses a ELM 327 chip is set to the baud rate
115200 as default. Therefor, I have to change the baud rate to speak to
the ELM chip. I modified the $port_name parameter of the new() call in a
way that it is possible to add the baud rate to the port name or set
only the required baud rate.
Change the default baud rate only:
my $obd = Device::ELM327->new('115200');
Set the interface and baud rate:
my $obd = Device::ELM327->new('/dev/ttyUSB0:115200');
Please add the patch below or a similar function to set the baud rate.
Thank you.
Regards, Thomas
--- Device-ELM327-0.09/lib/Device/ELM327.pm 2013-10-16 19:37:17.000000000 +0200
+++ Device-ELM327-0.09-tk/lib/Device/ELM327.pm 2014-03-05 19:56:32.710448243 +0100
@@ -22,7 +22,7 @@
else
{
require Device::SerialPort;
- Device::SerialPort->import qw( :PARAM :STAT 0.07 );
+ Device::SerialPort->import (qw( :PARAM :STAT 0.07 ));
}
}
@@ -76,7 +76,8 @@
my $obd = Device::ELM327->new();
If you know the port name (e.g. 'COM5', '/dev/ttyUSB7' etc) it may be
-quicker to pass it into the function:
+quicker to pass it into the function. The baud rate can be specified,
+too (e.g. '115200', '/dev/ttyUSB7:115200', 'COM5:57600' etc):
my $obd = Device::ELM327->new($port_name);
@@ -114,11 +115,11 @@
$self->{'debug_level'} = $debug_level;
}
- $self->{'ELM_type'} = "NONE";
- $self->{'bus_type'} = "unknown";
+ $self->{'ELM_type'} = "NONE";
+ $self->{'bus_type'} = "unknown";
$self->{'replay_file'} = 0;
$self->{'replay_response'} = ();
-
+
$self->{'last_command'} = 0;
$self->{'last_sub_command'} = 0;
$self->{'response'} = (); # Array of strings, one per line of the response.
@@ -3366,32 +3367,43 @@
my $quiet = 0;
my $port = -1;
my $port_count = 0;
+ my $baudrate = 38400;
- if (!defined($port_name) || $port_name eq "")
+ if (!defined($port_name) || $port_name eq "" || $port_name =~ /^\d+$/)
{
+ if ($port_name =~ /^\d+$/)
+ {
+ $baudrate = $port_name;
+ }
if ($^O eq "MSWin32")
{
- $port_name = "COM1";
+ $port_name = "COM1:$baudrate";
}
else
{
- $port_name = "/dev/ttyUSB0";
+ $port_name = "/dev/ttyUSB0:$baudrate";
}
}
+ ($port_name, $baudrate) = split(':', $port_name);
+
my $port_number = $port_name;
$port_number =~ s/[^0-9]//g; # Strip everything that isn't numeric
my $port_text = $port_name;
$port_text =~ s/[0-9]//g; # Strip everything that is numeric
+ if ($port_text eq '') { # Only numeric, looks like baudrate
+ $baudrate = $port_number;
+ }
+
do
{
$port_name = $port_text.$port_number;
if ($^O eq "MSWin32")
{
- $port = Win32::SerialPort->new ($port_name);
+ $port = Win32::SerialPort->new($port_name);
}
else
{
@@ -3403,7 +3415,7 @@
$port->user_msg(1); # misc. warnings
$port->error_msg(1); # hardware and data errors
- $port->baudrate(38400);
+ $port->baudrate($baudrate);
$port->parity("none");
$port->parity_enable(0); # for any parity except "none"
$port->databits(8);