【发布时间】:2020-06-04 07:55:57
【问题描述】:
我用Neo-6M GPS moduledatasheet 做了一些小项目,在Windows 10 中跟踪GPS 卫星。
下面的一段代码 sn-p 演示了程序的communication core。
我发现有时在程序启动时它不与 GPS 模块 通信,我必须通过按几次 Ctrl+C 来中断它,才能重新-启动它并在程序和GPS模块之间建立通信。
有时可能需要几次尝试,程序才能从 GPS 模块读取数据。
GPS模块通过USB转串口模块cp2102,cp2102 datasheet连接到电脑。
GPS 模块和驱动程序正常工作 -- 已通过u-center 软件确认。
有人可以发现程序和GPS模块之间的交互问题的原因吗?
use strict;
use warnings;
use feature 'say';
use Time::HiRes qw(usleep);
use Win32::SerialPort;
use Win32::Process;
use Win32::Console::ANSI qw/Cls Title Cursor/;
use Term::ANSIScreen qw/:color :cursor :screen/;
use sigtrap 'handler' => \&sig_handler, qw(INT TERM KILL QUIT);
use Data::Dumper;
my $debug = 1;
my $port_name = 'COM4';
my $baudrate = 9600;
my $databits = 8;
my $parity = 'none';
my $stopbits = 1;
my $portObj = new Win32::SerialPort($port_name)
|| die "Can't open $port_name: $^E\n"; # $quiet is optional
$portObj->baudrate($baudrate);
$portObj->databits($databits);
$portObj->parity($parity);
$portObj->stopbits($stopbits);
sub sig_handler {
Cls();
cursor_mode('on');
$portObj->lookclear();
$portObj->close();
exit 0;
}
cursor_mode('off');
while(1) {
my $line = $portObj->lookfor();
if( $line ) {
{
local $/ = "\r";
chomp $line;
}
say "[$line]" if $debug;
# Some data processing takes place
} else {
usleep(50); # Allocate time for other processes to run
}
}
# Data processing subroutines
# Positioned output to terminal window
sub cursor_mode {
my $mode = shift;
print "\e[?25l" if $mode eq 'off';
print "\e[?25h" if $mode eq 'on';
}
【问题讨论】:
标签: windows perl serial-port gps