# serialport_conf.pl

use Win32::SerialPort;
use Win32::SerialPort qw( :STAT 0.19 );
use Win32API::CommPort;
use Win32API::CommPort qw(:RAW :PARAM :STAT 0.19 );

my $WL = 600;
if($ARGV[0] > 0) {
	$WL = $ARGV[0];
}

my $conf_file = 'serialport.conf';
my $port_name = 'COM1';

if(1) {
my $com1 = new Win32::SerialPort($port_name) or die;

# この部分は通信機器と合わせてください。  
$com1->baudrate(9600);
$com1->parity('none');
$com1->databits(8);
$com1->stopbits(1);

# フロー制御の方法を選択します。rts（ハードウェア制御），
# xoff（ソフトウェア制御），dtr（rtsと同じことができるが，ピンが違う）
# があります。特に理由がない限りrtsにしてください。
$com1->handshake('rts');

# 読み込み，書き込みバッファです。
# バッファが小さすぎるとデータを途中までしか受け取れない場合があります。
# バイナリ転送などを行う場合は多めに設定してください。
$com1->buffers(
  1024 * 20, # 読み込みバッファ
  1024 * 4   # 書き込みバッファ
);

# 読み込み時の時間間隔の設定です。
# 小さすぎると計測機器の反応速度が
# パソコンに追いつけなくなります。
$com1->read_interval(800);
$com1->read_char_time(400);

$com1->error_msg(1);
$com1->user_msg(1);

$com1->write_settings();
$com1->save($conf_file);

undef $com1;
}


if(0) {
my $quiet = 0;
my $PortName = "COM1";
my $PortObj = new Win32API::CommPort($PortName, $quiet)
       || die "Can't open $PortName: $^E\n";
$PortObj->is_handshake("rts");           # set parameter
$PortObj->is_baudrate(9600);
$PortObj->is_parity("none");
$PortObj->is_databits(8);
$PortObj->is_stopbits(1);
$PortObj->debug_comm(0);
$PortObj->is_buffers(4096, 4096);  # read, write
$PortObj->is_read_interval(800);    # max time between read char (millisec)
$PortObj->is_read_char_time(100);     # avg time between read char
$PortObj->is_read_const_time(1000);  # total = (avg * bytes) + const 
$PortObj->is_write_char_time(100);
$PortObj->is_write_const_time(1000);
my @required = qw( BAUD DATA STOP );
my $faults = $PortObj->initialize(@required);
if ($faults) { die "Required parameters not set before initialize\n"; }



my $output_string = "GOWAVE $WL\n";
my $count_out = $PortObj->write_bg($output_string);      # background write
print "count_out: $count_out\n";
my ($done, $count_out) = $PortObj->write_done(0);
print "done: $done  count_out: $count_out  s: $output_string\n";

$PortObj->close || die;
      # "undef $PortObj" preferred unless reopening port
      # "close" should precede "undef" if both used


}





if(1) {
# シリアルポートを普通のファイルハンドルに見せかけます。
my $tie_obj = tie *PORT, 'Win32::SerialPort', $conf_file  or die "Can't open serialport";

print PORT "GOWAVE $WL\n";

print PORT "WAVE?\n"; # 測定装置に文字を送信します。
                      # コマンドの後には必ず改行文字を入れてください。

my $response = getc PORT; # シリアルポートの場合，通信を受信したことを
                          # 示す信号が1バイト帰ってきます。
                          # getc関数で読み込むことができます。

if ($response eq NAK) {
  # エラーが発生
  warn "serialport error";
} elsif ($response eq ACK) {
  # 通信成功
  my $text = <PORT>; # 装置から送られてきた文字は通常のファイルハンドルと
                     # 同様に受け取ることができます。
  print $text;
} else {
  # ポートからの応答がありません。
  # 設定ファイルの read_char_time が小さすぎる可能性があります。
  warn "no response";
}

close PORT or warn "Can't close serialport";

#undef $PortObj;
undef $tie_obj;
untie *PORT;                                 ## DESTROY   ##
}



sub ACK () {chr 0x06} # 通信成功信号
sub NAK () {chr 0x15} # 通信失敗信号

