Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl: the Markov chain saw
 
PerlMonks  

Re: serial port hex only

by Marshall (Prior)
on Apr 26, 2012 at 21:59 UTC ( #967472=note: print w/ replies, xml ) Need Help??


in reply to serial port hex only

I keep putting off a project to use this thing. But I did look at Win32::SerialPort docs.

I have written binary data to disk files before. One way, open file, set binmode, use syswrite() to write a binary buffer. Looks like something similar can be done here. You don't say how you are doing the write, but if you are using tied file handles, I would : binmode FH; and then use syswrite(). There are some examples in the docs. The buffer that syswrite uses are bytes, so you need to think about pack and unpack if you are say trying to send something that is not already a binary byte.

Update: This makes a binary file on my machine. I always have to stare at pack a long time to get it right - so I did something simple here - however it did work. I'd suggest using a disk file like this sort of thing to get the "right incantation" to generate the binary write sequence that you need based on the values that you want to send for testing, then see if you can do the same thing on the serial port.

#!/usr/bin/perl -w use strict; open FH, ">", "testingbin" or die "unable to open testingbin $!"; binmode FH; foreach (1,2,3,4,5,6,7,8,9) { my $buf = chr($_); syswrite FH,$buf; } close FH; __END__ my text editor says: 0102030405060708 09 as binary bytes
My Update to your Update:

Yes, this transmit one char at a time thing looks fine and is similar to the above test code. The pack and unpack functions can generate/decode a whole buffer (maybe many KB) of stuff that can be sent/received at one time. This makes a difference when mucking around with big binary files like maybe .WAV files, but sounds like your serial port application will do just fine reading or writing a character at a time - computers are so fast nowadays that it will keep up with the serial port at even a high baud rate.

PS: Welcome to Perl! This stuff is just amazing!


Comment on Re: serial port hex only
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (17)
As of 2014-04-17 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls