I finally figured out how to send and receive data to/from my RS232 Balance. It was a struggle to get the correct parameters etc. Now I have a perl script sort of working but there is a major problem. The code below is written as a TKX program. All it does is send hex strings to the scale to make it report back and also to operate the tare function. It works but only once every time I open the program. Multiple clicks on the read button (calls a sub to send characters) after the first return nothing. Tare button does nothing unless it is the first thing done after the program starts and then the scale does not respond until the program is closed. I have looked over win32::serial over and over and tried everything i could think of but nothing changes. The communication is working because the scale does report one value or does on tare operation when the program closes. sorry for all the TK stuff getting in the way. At the end is a bare bones version but it does not work for my testing because the script ends after each sent character.
#!/usr/bin/perl -w use strict; use Tkx; use Win32::SerialPort; use Cwd; use File::Copy; my $path = getcwd; my $read_scale; my $main_back_color = "#EAC9E0"; my $backcolor = "#E798FF"; my $active_color = "#D4D0C8"; my $scale; my $maintitle = "Read Sartorius"; my $version = " version 1.000"; Tkx::catch("console hide"); my $mw = Tkx::widget->new("."); $mw->g_wm_resizable(0,0); $mw-> g_wm_title($maintitle); $mw->g_grid_columnconfigure(0, -weight => 1); $mw->g_grid_rowconfigure(0, -weight => 1); Tkx::font_create("headtext_font",-size => 14); Tkx::font_create("scale_font",-size => 10); Tkx::font_create("bold_label_font", -size => 12, -weight=> 'bold'); Tkx::font_create("small_button_font", -size => 6,-weight=> 'normal'); my $menu = $mw->new_menu; $mw->configure(-menu => $menu); my $std_fncts = $menu->new_menu; my $test = $menu->new_menu; $menu->add_cascade(-menu => $test, -label => "Help"); $test->add_command(-label => "no Help yet", -command => sub {fill_help("F");do_help(25,100);}); $menu->add_command(-label => "Exit", -command => sub{\&clean_exit()}); my $frame0 = $mw-> new_frame(-relief=>'raised',-borderwidth=>2,-bd=>1, +-background=> "$backcolor"); $frame0->g_grid(-column =>0, -row => 0,-sticky => "ns"); my $buttons = $frame0-> new_frame(-bd=>2,-relief=>'raised',-relief=>'r +aised'); #,-background =>$check_button_color); $buttons->g_grid(-column =>0,-rowspan => 2, -row => 4,-sticky => "nsew +"); my $readbut = $buttons->new_button (-text => "Read", , -width=> 11,-co +mmand => sub {read_scale();}, -background => "green", -activebackgrou +nd => "red", -pady => 5, -padx => 6); $readbut -> g_grid(-column =>0, -row => 0, -sticky => "E"); my $tarebut = $buttons->new_button (-text => "Tare", -width=> 10, -com +mand => sub {tare_scale();}, -background => "red", -pady => 5, -padx +=> 3); $tarebut -> g_grid(-column =>1, -row => 0, -sticky => "E"); my $lblscale = $buttons->new_label (-text => "Scale"); $lblscale -> g_grid(-column => 0, -row => 1, -sticky => "w"); my $eread_scale = $buttons->new_entry (-width => 15, -textvariable => +\$read_scale); $eread_scale -> g_grid(-column => 1, -row => 1, -sticky => "w"); my $PORT = "COM1"; # port to watch my $ob = Win32::SerialPort->new ($PORT) || die "Can't Open $PORT: $!"; $ob->baudrate(1200) || die "failed setting baudrate"; $ob->parity("odd") || die "failed setting parity"; $ob->databits(7) || die "failed setting databits"; $ob->handshake("rts") || die "failed setting handshake"; $ob->stopbits(1) || die "failed setting stop bits";; $ob->write_settings || die "no settings"; sub read_scale{ $ob->transmit_char(0x1b); $ob->transmit_char(0x50); $ob->transmit_char(0x0d); $ob->transmit_char(0x0a); sleep .2; my $result = $ob->input; $read_scale = ($result); print "result = $result\n"; my $got_it = $ob->lookfor; } sub tare_scale{ $ob->transmit_char(0x1b); $ob->transmit_char(0x54); $ob->transmit_char(0x0d); $ob->transmit_char(0x0a); } sub clean_exit{ undef $ob; exit; } Tkx::MainLoop();
$PORT = "COM1"; # port to watch $ob = Win32::SerialPort->new ($PORT) || die "Can't Open $PORT: $!"; $ob->baudrate(1200) || die "failed setting baudrate"; $ob->parity("odd") || die "failed setting parity"; $ob->databits(7) || die "failed setting databits"; $ob->handshake("rts") || die "failed setting handshake"; $ob->stopbits(1) || die "failed setting stop bits";; $ob->write_settings || die "no settings"; # Send a hex string to the port my $sendP = pack('C*', 0x1b, 0x50); # the print command my $sendT = pack('C*', 0x1b, 0x54); # the tare command my $pass=$ob->write($sendP); sleep 1; my $result = $ob->input; print "result = $result\n"; undef $ob;

In reply to win32::serial problem by spencoid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.