Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: serial port hex only

by Marshall (Abbot)
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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967472]
[Lady_Aleena]: [pad://]
[james28909]: my scratchpad
[james28909]: and like i say, is just an example, not working code, but hopefully you understand what i mean. :)
[james28909]: and like i say, it would be alot of work to get around renaming 3 subs haha
[Lady_Aleena]: Well, you would have to add the file munging and a lot more.
[james28909]: yeah but it would rename those subs xD
[Lady_Aleena]: I'd say, go back to my scratchpad, and really look at what the 3 subs are doing, then play. Meanwhile, I am going afk for about an hour +/- a few minutes.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-05-24 05:22 GMT
Find Nodes?
    Voting Booth?