CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Writing to array to send to RS-232...Need Help with my code

 



SuperDude123
New User

Sep 22, 2013, 6:46 PM

Post #1 of 4 (387 views)
Writing to array to send to RS-232...Need Help with my code Can't Post

I'm trying to send and check for data from my microcontroller via RS-232 on my Linux box. My code is not doing too good around my array, and it could well be because I've been trying to write it like C code. Any help is welcomed:



Code
 
#!/usr/bin/perl

use strict;
use warnings;

print "\n ******* STARTS HERE ******* \n";

use Device::SerialPort;

# Set up the serial port
# 9600, 81N on the RS232 driver
my $port = Device::SerialPort->new("/dev/ttyS0");
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);

my $count = 0;
my $Data_Stream [18];

while (1) {
# Poll to see if any data is coming in
my $char = $port->lookfor();

# If we get data, then print it
# Send a number to the chip
if ($char) {
print "Recieved character: " . $char . " \n";
} else {
sleep(1);
$count++;
$Data_Stream [0] = "A";
$Data_Stream [1] = "$count";
$Data_Stream [2] = "$count+1";
$Data_Stream [3] = "$count+1";
$Data_Stream [4] = "$count+3";
$Data_Stream [5] = ",";
$Data_Stream [6] = "A";
$Data_Stream [7] = "A";
$Data_Stream [8] = "A";
$Data_Stream [9] = "A";
$Data_Stream [10] = "A";
$Data_Stream [11] = ",";
$Data_Stream [12] = "A";
$Data_Stream [13] = "A";
$Data_Stream [14] = "A";
$Data_Stream [15] = "A";
$Data_Stream [16] = "A";
$Data_Stream [17] = "A";
$Data_Stream [18] = "A";

#Example of expected output: A2345,AAAAA,AAAAA

my $count_out = $port->write("$Data_Stream\n");
print "Sent character: $Data_Stream \n";
}
}



BillKSmith
Veteran

Sep 22, 2013, 9:07 PM

Post #2 of 4 (382 views)
Re: [SuperDude123] Writing to array to send to RS-232...Need Help with my code [In reply to] Can't Post

I cannot help you with your use of the module. You are right that you are using C rather than perl syntax for your array declaration.

In perl, you must declare an array with an @ rather than $. In perl, we almost never specify the size of an array and you cannot do it the way to tried. Also note that your array has 19 elements.


Assuming that you need an array, Declare it:

Code
my @Data_Stream;


Later you can initialize it by assigning a list to it:

Code
@Data_stream =  
"A",
$count,
$count+1,
$count+2,
$count+3,
",",
("A")x5,
",",
("A")x6,
;



Also note: Unlike C, strings in perl have nothing to do with arrays. I do not know your requirement, but it is likely that you need a string rather than an array.
Good Luck,
Bill


SuperDude123
New User

Sep 22, 2013, 11:00 PM

Post #3 of 4 (379 views)
Re: [BillKSmith] Writing to array to send to RS-232...Need Help with my code [In reply to] Can't Post

I think your right that I could use a string instead.

Would this be a valid form for a string? :


Code
$Data_Stream = "A($count)($count+2)($count+3)($count+4),12345,12345";



BillKSmith
Veteran

Sep 23, 2013, 5:17 AM

Post #4 of 4 (374 views)
Re: [SuperDude123] Writing to array to send to RS-232...Need Help with my code [In reply to] Can't Post

Your string is valid, but you probably do not intend what it really contains.


Code
use strict; 
use warnings;
use Data::Dumper;
my $count = 7;
my $Data_Stream = "A($count)($count+2)($count+3)($count+4),12345,12345";
print Dumper $Data_Stream;


Output:

Code
$VAR1 = 'A(7)(7+2)(7+3)(7+4),12345,12345';

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives