If you want to be able to read utf data from your serial port, then you probably need to be using at least perl 5.8, and using the new "IO layer", ':utf8' on the open to set the filehandle to expect utf-8 data.
If your using Device::Serial, you probably aren't doing the open yourself, in which case you need to get hold of the filehandle being used (which appears to be available via $PortObj->{FD}) and use binmode to set the IOlayer to utf8
use Device::SerialPort;
my $PortObj = Device::SerialPort( ... );
binmode $PortObj->{FD}, ':utf8';
...
Note: This is an educated guess. It's based upon my use of Win32::SerialPort (of which Device::SerialPort is an emulation), but I've no way to verify the speculation.
HTH.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.
| [reply] [d/l] [select] |
I have this code that i got from here.
I´ve used as you said the binmode $ob->{LOG}, ':UTF8'; but the problem is that it can´t catch the correct format!
How can i change the format in order to save the data from the serial port to the file correctly?
#!/usr/bin/perl
use warnings;
use Device::SerialPort 0.22;
$LOGDIR = "/home/neo/telex_lusa/log";
$LOGFILE = "geiger.log";
$PORT = "/dev/ttyS0";
$ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!";
$ob->baudrate(2400) || die "failed setting baudrate";
$ob->parity("none") || die "failed setting parity";
$ob->databits(8) || die "failed setting databits";
$ob->stty_icrnl(1) || die "failed setting convert cr to new line";
$ob->handshake("none") || die "failed setting handshake";
$ob->write_settings || die "no settings";
open(LOG,">>${LOGDIR}/${LOGFILE}")||die "can't open smdr file $LOGDIR/
+$LOGFILE for append: $SUB $!\n";
binmode $ob->{LOG}, ':UTF8';
select(LOG), $| = 1; # set nonbuffered mode, gets the chars out NOW
open(DEV, "<$PORT") || die "Cannot open $PORT: $_";
while($_ = <DEV>){
print LOG $_;
}
undef $ob;
Thank you very much!
Nuno Fernandes | [reply] [d/l] |
Your applying binmode to the output file, by which time I think the damage has already been done.
What I was suggesting was to apply binmode to the input file. Whilst you don't open this directly, looking at the source of Device::SerialPort, I noticed that the filehandle it use for the serial port is accessable via the object as $obj->{FD}, hence it should be possible to enable the :utf8 IO layer on it using the snippet I included. I've modified your snippet
#!/usr/bin/perl
use warnings;
use Device::SerialPort 0.22;
$LOGDIR = "/home/neo/telex_lusa/log";
$LOGFILE = "geiger.log";
$PORT = "/dev/ttyS0";
$ob = Device::SerialPort->new ($PORT)
|| die "Can't Open $PORT: $!";
## MODIFIED HERE!!
## FD is the key used internally by D::SP for the file handle
## NOTE: The IOLayer is ':utf8' not ':UTF8'!
binmode $ob->{FD}, ':utf8';
$ob->baudrate(2400) || die "failed setting baudrate";
$ob->parity("none") || die "failed setting parity";
$ob->databits(8) || die "failed setting databits";
$ob->stty_icrnl(1) || die "failed setting convert cr to new line";
$ob->handshake("none") || die "failed setting handshake";
$ob->write_settings || die "no settings";
open(LOG,">>${LOGDIR}/${LOGFILE}")
||die "can't open smdr file $LOGDIR/$LOGFILE for append: $SUB $!\n";
select(LOG), $| = 1; # set nonbuffered mode, gets the chars out NOW
open(DEV, "<$PORT") || die "Cannot open $PORT: $_";
while($_ = <DEV>){
print LOG $_;
}
undef $ob;
As I said, I've no way to verify this. For the record and those that come after you, please post a reply stating one way or another whether this works.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.
| [reply] [d/l] [select] |