Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sun Jul 23, 2017 7:39 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: going round in circles!! trying to make 24 serial ports
PostPosted: Mon Apr 15, 2013 4:00 pm 
Offline

Joined: Mon Apr 15, 2013 2:58 pm
Posts: 1
i want to make an app which connects via serialports to approx 20 devices

i have got the hardware, an edgeport 8 usb, and a brainbox 8 card, and a couple of extra rs232 cards / usb adaptors

i , so far, can make an app which shows the info about each port
the problem is realbasic wont compile my program when i try to add more code

i add 1 serial port in the editor, called serial1 ,to a window called ports, then duplicate it as many times as ports exist

with this sort of code
if spcount >1 then
dim serial2 as serial
serial2 = new serial
Serial2.baud=serialrates(1)
srlportbaud.append str(Serial2.baud)
spopen=2
end if

srlportbaud is to be an array i can refer to without having to replicate slow sytem calls

then i populate some popup menus in another window with the possible baud rates
i put this code in one popups change event
dim retval as string
ports.Serial1.close
ports.Serial1.baud= pbpm1.ListIndex
txpnameb1.text=str(ports.serialrates( pbpm1.ListIndex))
retval=str(ports.Serial1.Open)
ports.srlportbaud(0) =str(ports.Serial1.baud)
txpnamea1.text=ports.srlportbaud(0)

but then if i add the following code to the change event of the popup for the next serial port , the compiler says my method is unknown, yet ive got code which creates serial2

dim retval as string
>> ports.Serial2.close ( compliler says no!! unknown method or property)
ports.Serial2.baud= pbpm2.ListIndex
txpnameb2.text=str(ports.serialrates( pbpm2.ListIndex))
retval=str(ports.Serial2.Open)
ports.srlportbaud(1) =str(ports.Serial2.baud)
txpnamea2.text=ports.srlportbaud(1)


-------------------------------------------------------------------------
ive tried several methods , heres the full code

in a window called ports i add this code

dim i , count as Integer

serialrates(0)=300
serialrates(1)=600
serialrates(2)=1200
serialrates(3)=1800
serialrates(4)=2400
serialrates(5)=3600
serialrates(6)=4800
serialrates(7)=7200
serialrates(8)=9600
serialrates(9)=14400
serialrates(10)=19200
serialrates(11)=28800
serialrates(12)=38400
serialrates(13)=57600
serialrates(14)=115200
serialrates(15)=230400

serialpname(0)="COM1"
serialpname(1)="COM2"
serialpname(2)="COM3"
serialpname(3)="COM4"
serialpname(4)="COM5"
serialpname(5)="COM6"
serialpname(6)="COM7"
serialpname(7)="COM8"
serialpname(8)="COM9"
serialpname(9)="COM10"
serialpname(10)="COM11"
serialpname(11)="COM12"
serialpname(12)="COM13"
serialpname(13)="COM14"
serialpname(14)="COM15"
serialpname(15)="COM16"
serialpname(16)="COM17"
serialpname(17)="COM18"
serialpname(18)="COM19"
serialpname(19)="COM20"
serialpname(20)="COM21"
serialpname(21)="COM22"
serialpname(22)="COM23"
serialpname(23)="COM24"
serialpname(24)="COM25"

count = System.SerialPortCount
spcount =count
tportsmax.text=str(count)

Serial1.baud=serialrates(0)
srlportbaud.append str(Serial1.baud)
spopen=1

If Serial1.Open then
serial=1
Else
serial=0
End if

if spcount >1 then
dim serial2 as serial
serial2 = new serial
Serial2.baud=serialrates(1)
srlportbaud.append str(Serial2.baud)
spopen=2
end if
if spcount >2 then
dim serial3 as serial
serial3 = new serial
Serial3.baud=serialrates(2)
srlportbaud.append str(Serial3.baud)
spopen=3
end if
if spcount >3 then
dim serial4 as serial
serial4 = new serial
Serial4.baud=serialrates(3)
srlportbaud.append str(Serial4.baud)
spopen=4
end if

if spcount >4 then
dim serial5 as serial
serial5 = new serial
Serial5.baud=serialrates(4)
srlportbaud.append str(Serial5.baud)
spopen=5
end if
if spcount >5 then
dim serial6 as serial
serial6 = new serial
Serial6.baud=serialrates(5)
srlportbaud.append str(Serial6.baud)
spopen=6
end if
if spcount >6then
dim serial7 as serial
serial7 = new serial
Serial7.baud=serialrates(6)
srlportbaud.append str(Serial7.baud)
spopen=7
end if

if spcount >7 then
dim serial8 as serial
serial8 = new serial
Serial8.baud=serialrates(7)
srlportbaud.append str(Serial8.baud)
spopen=8
end if
if spcount >8 then
dim serial9 as serial
serial9 = new serial
Serial9.baud=serialrates(8)
srlportbaud.append str(Serial9.baud)
spopen=9
end if
if spcount >9 then
dim serial10 as serial
serial10 = new serial
Serial10.baud=serialrates(8)
srlportbaud.append str(Serial10.baud)
spopen=10

end if

if spcount >10 then
dim serial11 as serial
serial11 = new serial
Serial11.baud=serialrates(8)
srlportbaud.append str(Serial11.baud)
spopen=11
end if

if spcount >11 then
dim serial12 as serial
serial12 = new serial
Serial12.baud=serialrates(8)
srlportbaud.append str(Serial12.baud)
spopen=12
end if

if spcount >12 then
dim serial13 as serial
serial13 = new serial
Serial13.baud=serialrates(8)
srlportbaud.append str(Serial13.baud)
spopen=13
end if
if spcount >13 then
dim serial14 as serial
serial14 = new serial
Serial14.baud=serialrates(8)
srlportbaud.append str(Serial14.baud)
spopen=14
end if


if spcount >14 then
dim serial15 as serial
serial15 = new serial
Serial15.baud=serialrates(8)
srlportbaud.append str(Serial15.baud)
spopen=15
end if

if spcount >15 then
dim serial16 as serial
serial16 = new serial
Serial16.baud=serialrates(8)
srlportbaud.append str(Serial16.baud)
spopen=16
end if

if spcount >16 then
dim serial17 as serial
serial17 = new serial
Serial17.baud=serialrates(8)
srlportbaud.append str(Serial17.baud)
spopen=17
end if

if spcount >17 then
dim serial18 as serial
serial18 = new serial
srlportbaud.append str(Serial18.baud)
spopen=18
end if
if spcount >18 then
dim serial19 as serial
serial19 = new serial
srlportbaud.append str(Serial19.baud)
spopen=19
end if
if spcount >19 then
dim serial20 as serial
serial20 = new serial
srlportbaud.append str(Serial20.baud)
spopen=20
end if

if spcount >20 then
dim serial21 as serial
serial21 = new serial
srlportbaud.append str(Serial21.baud)
spopen=21
end if

if spcount >21 then
dim serial22 as serial
serial22 = new serial
srlportbaud.append str(Serial22.baud)
spopen=22
end if

if spcount >22 then
dim serial23 as serial
serial23 = new serial
srlportbaud.append str(Serial23.baud)
spopen=23
end if
tf1.text=str(spopen)


---------------------------------------------------------------------------------
then in another window called port settings

i have some text fields and popup menus for baud and com port
and add this code

dim i , ii, count as Integer
count = ports.spcount
tportsmax.text=str(count)

if count >0 then
for i = 0 to count -1

' srlportindex.append System.SerialPort(i ).inputdrivername
srlportname.append System.SerialPort(i ).Name
'srlportrated.append str(System.SerialPort(i ).ratedspeed)
' srlportmaxspeed.append str(System.SerialPort(i ).maximumspeed)
'srlportdriver.append System.SerialPort(i ).outputdrivername


next i

for ii=0 to 25
ppm1.AddRow(ports.serialpname( ii ))
if srlportname(0)= str(ports.serialpname( ii )) then
ppm1.listindex=ii

end if
next ii

for i=0 to 15
pbpm1.AddRow str((ports.serialrates( i )))
if ports.srlportbaud(0 ) = str(ports.serialrates( i )) then
pbpm1.listindex=i

end if
next i


txpname1.text=srlportname(0 )
txpnamea1.text=ports.srlportbaud(0 )
'txpnameb1.text= srlportrated(0 )
end if

if count >1 then

for ii=0 to 25
ppm2.AddRow(ports.serialpname( ii ))
if srlportname(1)= str(ports.serialpname( ii )) then
ppm2.listindex=ii

end if
next ii
for i=0 to 15
pbpm2.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(1) = str(ports.serialrates( i )) then
pbpm2.listindex=i

end if
next i

txpname2.text=srlportname(1)
txpnamea2.text=ports.srlportbaud(1 )
'txpnameb2.text= srlportrated(1 )
end if

if count >2 then

for ii=0 to 25
ppm3.AddRow(ports.serialpname( ii ))
if srlportname(2)= str(ports.serialpname( ii )) then
ppm3.listindex=ii

end if
next ii
for i=0 to 15
pbpm3.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(2) = str(ports.serialrates( i )) then
pbpm3.listindex=i

end if
next i

txpnamea3.text=ports.srlportbaud(2 )
txpname3.text=srlportname(2 )
'txpnameb3.text= srlportrated(2 )
end if


if count >3 then

for ii=0 to 25
ppm4.AddRow(ports.serialpname( ii ))
if srlportname(3)= str(ports.serialpname( ii )) then
ppm4.listindex=ii
end if
next ii
for i=0 to 15
pbpm4.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(3) = str(ports.serialrates( i )) then
pbpm4.listindex=i
end if
next i
pbpm4.initialvalue=str(ports.srlportbaud( 3))

txpnamea4.text=ports.srlportbaud(3 )

txpname4.text=srlportname(3)
txpnameb4.text=str(count)
end if

if count >4 then
for ii=0 to 25
ppm5.AddRow(ports.serialpname( ii ))
if srlportname(4)= str(ports.serialpname( ii )) then
ppm5.listindex=ii

end if
next ii
for i=0 to 15
pbpm5.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(4) = str(ports.serialrates( i )) then
pbpm5.listindex=i

end if
next i
pbpm5.initialvalue=str(ports.srlportbaud( 4))

txpnamea5.text=ports.srlportbaud(4 )
txpname5.text=srlportname(4)
txpnameb5.text=str(count)
end if

if count >5 then

for ii=0 to 25
ppm6.AddRow(ports.serialpname( ii ))
if srlportname(5)= str(ports.serialpname( ii )) then
ppm6.listindex=ii

end if
next ii
for i=0 to 15
pbpm6.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(5) = str(ports.serialrates( i )) then
pbpm6.listindex=i

end if
next i
pbpm6.initialvalue=str(ports.srlportbaud( 5))

txpnamea6.text=ports.srlportbaud(5 )

txpname6.text=srlportname(5)
txpnameb6.text=str(count)
end if

if count >6 then

for ii=0 to 25
ppm7.AddRow(ports.serialpname( ii ))
if srlportname(6)= str(ports.serialpname( ii )) then
ppm7.listindex=ii

end if
next ii
for i=0 to 15
pbpm7.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(6) = str(ports.serialrates( i )) then
pbpm7.listindex=i
end if
next i
pbpm7.initialvalue=str(ports.srlportbaud( 6))

txpnamea7.text=ports.srlportbaud(6 )
txpname7.text=srlportname(6)
txpnameb7.text=str(count)
end if

if count >7 then

for ii=0 to 25
ppm8.AddRow(ports.serialpname( ii ))
if srlportname(7)= str(ports.serialpname( ii )) then
ppm8.listindex=ii
end if
next ii
for i=0 to 15
pbpm8.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(7) = str(ports.serialrates( i )) then
pbpm8.listindex=i
end if
next i
pbpm8.initialvalue=str(ports.srlportbaud( 7))

txpnamea8.text=ports.srlportbaud(7 )
txpname8.text=srlportname(7)
txpnameb8.text=str(count)
end if
if count >8 then

for ii=0 to 25
ppm9.AddRow(ports.serialpname( ii ))
if srlportname(8)= str(ports.serialpname( ii )) then
ppm9.listindex=ii
end if
next ii
for i=0 to 15
pbpm9.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(8) = str(ports.serialrates( i )) then
pbpm9.listindex=i
end if
next i
pbpm9.initialvalue=str(ports.srlportbaud( 8))

txpnamea9.text=ports.srlportbaud(8 )
txpname9.text=srlportname(8)
txpnameb9.text=str(count)
end if


if count >9 then
'ETC
end if

--------------------------------------------------------------

all fine till i try to make the popup value changes active
popup menu 1 is fine
popups 2 onwards cause a problem


popup 1 s code

dim retval as string
ports.Serial1.close
ports.Serial1.baud= pbpm1.ListIndex
txpnameb1.text=str(ports.serialrates( pbpm1.ListIndex))
retval=str(ports.Serial1.Open)
ports.srlportbaud(0) =str(ports.Serial1.baud)
txpnamea1.text=ports.srlportbaud(0)



popup2 s code

dim retval as string
ports.Serial2.close
ports.Serial2.baud= pbpm2.ListIndex
txpnameb2.text=str(ports.serialrates( pbpm2.ListIndex))
retval=str(ports.Serial2.Open)
ports.srlportbaud(1) =str(ports.Serial2.baud)
txpnamea2.text=ports.srlportbaud(1)

the compiler wont let me refer to object names which only exist during runtime
serial1 is created by dragging the serial icon in the editor
serial2 is the result of

ports window

if spcount >1 then
dim serial2 as serial
serial2 = new serial
Serial2.baud=serialrates(1)
srlportbaud.append str(Serial2.baud)
spopen=2
end if

---------------------------------------
i have also tried another approach

in my ports window, i drag a serial object onto it in the editor
then i change its index param to 25

then my code looks like this ( i am just sequentially setting the baudrates just for testing to check the code etc)
ports window

dim i , count as Integer

serialrates(0)=300
serialrates(1)=600
serialrates(2)=1200
serialrates(3)=1800
serialrates(4)=2400
serialrates(5)=3600
serialrates(6)=4800
serialrates(7)=7200
serialrates(8)=9600
serialrates(9)=14400
serialrates(10)=19200
serialrates(11)=28800
serialrates(12)=38400
serialrates(13)=57600
serialrates(14)=115200
serialrates(15)=230400

serialpname(0)="COM1"
serialpname(1)="COM2"
serialpname(2)="COM3"
serialpname(3)="COM4"
serialpname(4)="COM5"
serialpname(5)="COM6"
serialpname(6)="COM7"
serialpname(7)="COM8"
serialpname(8)="COM9"
serialpname(9)="COM10"
serialpname(10)="COM11"
serialpname(11)="COM12"
serialpname(12)="COM13"
serialpname(13)="COM14"
serialpname(14)="COM15"
serialpname(15)="COM16"
serialpname(16)="COM17"
serialpname(17)="COM18"
serialpname(18)="COM19"
serialpname(19)="COM20"
serialpname(20)="COM21"
serialpname(21)="COM22"
serialpname(22)="COM23"
serialpname(23)="COM24"
serialpname(24)="COM25"







count = System.SerialPortCount

spcount =count

tportsmax.text=str(count)

dim serial1(25) as serial
serial1(1) = new serial


for i = 0 to count - 1
'PopupMenu1.AddRow( System.SerialPort( i ).Name )
next

'Serial1(1).close
'Serial1(1).SerialPort=System.SerialPort("COM1")

'Textfield1.Text=str(Serial1(1).Open)
If Serial1(1).Open then
serial=1
Else
serial=0
End if


Serial1(1).baud=serialrates(0)
srlportbaud.append str(Serial1(1).baud)
spopen=1

If Serial1(1).Open then
serial=1
Else
serial=0
End if






if spcount >1 then
serial1(2) = new serial
Serial1(2).baud=serialrates(1)
srlportbaud.append str(Serial1(2).baud)
spopen=2
end if
if spcount >2 then
serial1(3) = new serial
Serial1(3).baud=serialrates(2)
srlportbaud.append str(Serial1(3).baud)
spopen=3
end if
if spcount >3 then

serial1(4) = new serial
Serial1(4).baud=serialrates(3)
srlportbaud.append str(Serial1(4).baud)
spopen=4
end if

if spcount >4 then
serial1(5) = new serial
Serial1(5).baud=serialrates(4)
srlportbaud.append str(Serial1(5).baud)
spopen=5
end if
if spcount >5 then

serial1(6) = new serial
Serial1(6).baud=serialrates(5)
srlportbaud.append str(Serial1(6).baud)
spopen=6
end if
if spcount >6then

serial1(7) = new serial
Serial1(7).baud=serialrates(6)
srlportbaud.append str(Serial1(7).baud)
spopen=7
end if

if spcount >7 then

serial1(8) = new serial
Serial1(8).baud=serialrates(7)
srlportbaud.append str(Serial1(8).baud)
spopen=8
end if
if spcount >8 then

serial1(9) = new serial
Serial1(9).baud=serialrates(8)
srlportbaud.append str(Serial1(9).baud)
spopen=9
end if
if spcount >9 then

serial1(10) = new serial
Serial1(10).baud=serialrates(8)
srlportbaud.append str(Serial1(10).baud)
spopen=10

end if

if spcount >10 then

serial1(11) = new serial
Serial1(11).baud=serialrates(8)
srlportbaud.append str(Serial1(11).baud)
spopen=11
end if


--------------------------------------------------------------

and in my port settings window

dim i , ii, count as Integer

count = ports.spcount

tportsmax.text=str(count)


if count >0 then
for i = 0 to count -1

' srlportindex.append System.SerialPort(i ).inputdrivername
srlportname.append System.SerialPort(i ).Name
'srlportrated.append str(System.SerialPort(i ).ratedspeed)
' srlportmaxspeed.append str(System.SerialPort(i ).maximumspeed)
'srlportdriver.append System.SerialPort(i ).outputdrivername




next

for ii=0 to 25
ppm1.AddRow(ports.serialpname( ii ))
if srlportname(0)= str(ports.serialpname( ii )) then
ppm1.listindex=ii

end if
next ii

for i=0 to 15
pbpm1.AddRow str((ports.serialrates( i )))
if ports.srlportbaud(0 ) = str(ports.serialrates( i )) then
pbpm1.listindex=i

end if
next i


txpname1.text=srlportname(0 )
txpnamea1.text=ports.srlportbaud(0 )
'txpnameb1.text= srlportrated(0 )
end if

if count >1 then
for i = 0 to count -1



next
for ii=0 to 25
ppm2.AddRow(ports.serialpname( ii ))
if srlportname(1)= str(ports.serialpname( ii )) then
ppm2.listindex=ii

end if
next ii
for i=0 to 15
pbpm2.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(1) = str(ports.serialrates( i )) then
pbpm2.listindex=i

end if
next i

txpname2.text=srlportname(1)
txpnamea2.text=ports.srlportbaud(1 )
'txpnameb2.text= srlportrated(1 )
end if

if count >2 then

for ii=0 to 25
ppm3.AddRow(ports.serialpname( ii ))
if srlportname(2)= str(ports.serialpname( ii )) then
ppm3.listindex=ii

end if
next ii
for i=0 to 15
pbpm3.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(2) = str(ports.serialrates( i )) then
pbpm3.listindex=i

end if
next i

txpnamea3.text=ports.srlportbaud(2 )
txpname3.text=srlportname(2 )
'txpnameb3.text= srlportrated(2 )
end if


if count >3 then


for ii=0 to 25
ppm4.AddRow(ports.serialpname( ii ))
if srlportname(3)= str(ports.serialpname( ii )) then
ppm4.listindex=ii

end if
next ii
for i=0 to 15
pbpm4.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(3) = str(ports.serialrates( i )) then
pbpm4.listindex=i

end if
next i
pbpm4.initialvalue=str(ports.srlportbaud( 3))

txpnamea4.text=ports.srlportbaud(3 )

txpname4.text=srlportname(3)
txpnameb4.text=str(count)
end if


if count >4 then

for ii=0 to 25
ppm5.AddRow(ports.serialpname( ii ))
if srlportname(4)= str(ports.serialpname( ii )) then
ppm5.listindex=ii

end if
next ii
for i=0 to 15
pbpm5.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(4) = str(ports.serialrates( i )) then
pbpm5.listindex=i

end if
next i
pbpm5.initialvalue=str(ports.srlportbaud( 4))

txpnamea5.text=ports.srlportbaud(4 )
txpname5.text=srlportname(4)
txpnameb5.text=str(count)
end if

if count >5 then

for ii=0 to 25
ppm6.AddRow(ports.serialpname( ii ))
if srlportname(5)= str(ports.serialpname( ii )) then
ppm6.listindex=ii

end if
next ii
for i=0 to 15
pbpm6.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(5) = str(ports.serialrates( i )) then
pbpm6.listindex=i

end if
next i
pbpm6.initialvalue=str(ports.srlportbaud( 5))

txpnamea6.text=ports.srlportbaud(5 )

txpname6.text=srlportname(5)
txpnameb6.text=str(count)
end if

if count >6 then

for ii=0 to 25
ppm7.AddRow(ports.serialpname( ii ))
if srlportname(6)= str(ports.serialpname( ii )) then
ppm7.listindex=ii

end if
next ii
for i=0 to 15
pbpm7.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(6) = str(ports.serialrates( i )) then
pbpm7.listindex=i
end if
next i
pbpm7.initialvalue=str(ports.srlportbaud( 6))

txpnamea7.text=ports.srlportbaud(6 )
txpname7.text=srlportname(6)
txpnameb7.text=str(count)
end if

if count >7 then

for ii=0 to 25
ppm8.AddRow(ports.serialpname( ii ))
if srlportname(7)= str(ports.serialpname( ii )) then
ppm8.listindex=ii
end if
next ii
for i=0 to 15
pbpm8.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(7) = str(ports.serialrates( i )) then
pbpm8.listindex=i
end if
next i
pbpm8.initialvalue=str(ports.srlportbaud( 7))

txpnamea8.text=ports.srlportbaud(7 )
txpname8.text=srlportname(7)
txpnameb8.text=str(count)
end if
if count >8 then

for ii=0 to 25
ppm9.AddRow(ports.serialpname( ii ))
if srlportname(8)= str(ports.serialpname( ii )) then
ppm9.listindex=ii
end if
next ii
for i=0 to 15
pbpm9.AddRow(str(ports.serialrates( i )))
if ports.srlportbaud(8) = str(ports.serialrates( i )) then
pbpm9.listindex=i
end if
next i
pbpm9.initialvalue=str(ports.srlportbaud( 8))

txpnamea9.text=ports.srlportbaud(8 )
txpname9.text=srlportname(8)
txpnameb9.text=str(count)
end if


if count >9 then
for i = 0 to count -1
'ppm10.AddRow(srlportname( i ))
next
for i=0 to 15
'pbpm10.AddRow(str(ports.serialrates( i )))
next i
'pbpm4.initialvalue=str(ports.srlportbaud( 9))

'txpnamea10.text=ports.srlportbaud(9 )
txpname10.text=str(count)
txpnameb10.text=str(count)
end if
//

--------------------------------------------------------------------------
this all works so far, until i start to add code to the popup menus change event

dim retval as string
ports.Serial1(1).close
ports.Serial1(1).baud= pbpm2.ListIndex
txpnameb1.text=str(ports.serialrates( pbpm1.ListIndex))
retval=str(ports.Serial1(1).Open)
ports.srlportbaud(0) =str(ports.Serial1(1).baud)
txpnamea1.text=ports.srlportbaud(0)


now the compiler says NilObjectException
on this line > ports.Serial1(1).close

---------------------------------------------------------------


i hope ive explained the problem clearly, basically, how do i make an app which can access as many serial ports as are existing, and control the port settings etc, and send and receive data on them all.

do i have to drag about 25 serial controls onto my ports window using the editor
what happens if i write the program based on there being say 16 ports, and it works with all the rs232cards / usb attached, will it function with some usb rs232 or rs232 cards not installed, how do i write a program that sometimes will have many ports and sometimes only a few..

i am a bit lost with the NEW operator , and how the index function works as a parameter of a serial object.

oh and i need to make all the data sending and receiving run in a seperate threads to the user interface...! heaven help me!

i like flash actionscript because that lets me refer to a variable called "my33data" by code like this "my"+counter+"data" = anumber....


Top
 Profile  
Reply with quote  
 Post subject: Re: going round in circles!! trying to make 24 serial ports
PostPosted: Tue Apr 16, 2013 3:59 pm 
Offline
Site Admin
User avatar

Joined: Fri Sep 30, 2005 9:35 am
Posts: 987
Location: South Portland, Maine
That seems like a bit too much code for anyone to wade through :-)

Anyway, if you want a bunch of serial objects, then you can create them and put them in an array. You start to do that at the beginning, but then you seem to stop using the array.

Property mSerialControllers() As Serial

This create 25 serials objects:

Dim s As Serial
For i As Integer = 1 To 25
s = New Serial
mSerialControllers.Append(s)
Next


Now that you have created the serial objects, you just use the array to refer to them. You don't to keep recreating them using New:

mSerialController(10).Baud = 9600


You can check how many you have created using the Ubound function:

Dim serialCount As Integer = mSerialControllers.Ubound+1 // 0-based, so add 1 to the Ubound

_________________
Paul Lefebvre
Developer Evangelist
Xojo, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: going round in circles!! trying to make 24 serial ports
PostPosted: Wed Apr 17, 2013 3:10 am 
Offline
User avatar

Joined: Fri Oct 28, 2005 7:05 am
Posts: 565
Location: Emsworth, UK
eqdnb wrote:
]ive tried several methods , heres the full code

See Paul's comments. Further...

...
serialrates(0)=300
...
Serial port rates are defined by an enumerated selector, provided by class constants of the serial control. Serial.Baud300 etc. The actual values of the constants are 0, 1, 2 etc (an enumeration).

...
serialpname(0)="COM1"
...
Is probably redundant, because the port name is held within a property of the SerialPort object.

,,,
Serial1.baud=serialrates(0)
...
Is faulty. You earlier defined serialrates(0) = 300, but 300 is not a valid value for the .Baud property of a Serial control. I suspect what you meant was...
Serial1.Baud = Serial.Baud300
...which assigns the value 0 to the Serial1.Baud property, which sets the physical port to 300 Baud.

Tip.
Dim serialRateNames() as String = Array("300", "600", "1200", "1800", "2400", "3600", "4800", "7200", "9600", "14400", "19200", "28800", "38400", "57600", "115200", "230400")
Creates an array of names, whereby the array index corresponds to the Serial control's Baud rate class constants

Quote:
i hope ive explained the problem clearly, basically, how do i make an app which can access as many serial ports as are existing, and control the port settings etc, and send and receive data on them all.
do i have to drag about 25 serial controls onto my ports window using the editor
See Paul's earlier comment!

The way to use 25 serial ports simultaneously is, learn to write scalable code which can address two serial ports simultaneously. Real Studio is pretty good for crafting abstracted 'atomic' code, which is easily scaled up to meet the available hardware resources.

Examples speak better than words. Try this

//Create a new desktop project
// Add a property to a form and call it mPorts
// Add a PopupMenu, call it mnuPorts
// Add another PopupMenu, call it mnuRates

Window1.Open
dim port as Serial
for i as integer= 0 to System.SerialPortCount -1
port = new Serial
mPorts.Append port
next

mnuPorts.ListIndex = 0
End

mnuPorts.Open
dim port as SerialPort
//load up the UI with serial port names
for i as integer =0 to System.SerialPortCount -1
port = System.SerialPort(i)
me.AddRow port.Name
next
End

mnuPorts.Change
mnuRates.ListIndex = app.mPorts(me.ListIndex).Baud
End

mnuRates.Open
Dim serialRateNames() as String = Array("300", "600", "1200", "1800", "2400", "3600", "4800", "7200", _
"9600", "14400", "19200", "28800", "38400", "57600", "115200", "230400")

For Each r as string in serialRateNames
me.AddRow r
next
End

mnuRates.Change
mPorts(mnuPorts.ListIndex).Baud = me.ListIndex
End

So there we have ourselves a dialog box, able to show and set the individual Baud rate of however many Serial Ports are available. It took < 5 minutes and there isn't even one Serial control on the form; let alone 25.

All very impressive but you may have spotted a potential problem. How the heck can you populate the DataAvailable event if you don't drop a Serial control on the form? Answer, no problem...

Go back and modify the example form.
// Add a TextArea to the form, to display input from our serial ports.  The default name TextArea1 will do.
// Add a new method to the form, call it evtPortDataAvailable with the parameters defined below.
Function evtPortDataAvailable( port as Serial )
//this is the common event handler for all serial ports
TextArea1.AppendText "Port " + port.Name + " Rxd >" + port.ReadAll + EndOfLine
End

//Edit the Window Open event as follows
Window1.Open
dim port as Serial
for i as integer= 0 to System.SerialPortCount -1
port = new Serial
AddHandler port.DataAvailable, AddressOf evtPortDataAvailable
mPorts.Append port
next

mnuPorts.ListIndex = 0
End

That wasn't too difficult either. Our window is now able to display input from any serial port.

Hooking events with AddHandler is one way to do it. Subclassing the Serial control is another way to do it, probably the one I would choose, as it's a more flexible way to do things. Subclassing is difficult to cover from first principles on a forum though - The User Guide and Language Reference are your friends!

Quote:
i am a bit lost with the NEW operator ,

Understanding the difference between a reference (pointer) to an object and an instance (the actual object in memory) is absolutely vital to using Object Oriented Programming tools, such as Real Studio.

Very simply.
Dim port As Serial  //declares a reference to a Serial control object.  The reference is a pointer and occupies one memory address.

Dim port As New Serial //creates an instance of a Serial control object. The object occupies many memory addresses.


If you drop a Serial control onto a form and look at it's properties, you will see one of them is called SerialPort - That there is a reference. You can assign any SerialPort instance to the Serial1.SerialPort property, subsequently accessing the properties of the assigned SerialPort using Serial1.SerialPort.Name etc. If you don't assign an instance, Serial1.SerialPort.Name will raise a NilObject exception, because the reference is pointing at nothing. References are, in effect, (empty) placeholders for objects which will be allocated and assigned at run time.

Quote:
oh and i need to make all the data sending and receiving run in a seperate threads to the user interface...! heaven help me!
No you don't! There is no point. The threading model in RS is imposed within the RS framework, above the OS and the hardware layers. RS threads run co-operatively, with most (all) interrupt driven events being handled by the same thread as the UI. It's the price we pay for cross platform compatibility and the reason why synchronous calls to Serial Ports and Network Interfaces, cause the UI to become unresponsive.

Quote:
i like flash actionscript because that lets me refer to a variable called "my33data" by code like this "my"+counter+"data" = anumber....
:smile: I can't think of scenario in which I would ever want to do that. It might be convenient but it is also ambiguos. MyData(counter) would serve the same purpose without the ambiguity.

Yes, I realise you are having to change your mind set and I know how frustrating a task that can be. I hope this helps.

_________________
Yes it's me in the avatar


Top
 Profile  
Reply with quote  
 Post subject: Re: going round in circles!! trying to make 24 serial ports
PostPosted: Wed Apr 17, 2013 1:40 pm 
Offline

Joined: Wed Feb 16, 2011 1:59 pm
Posts: 74
Location: Arizona, USA
msssltd - Very good explanation. I learned (and refreshed) a bit from reading this.

_________________
Windows XP SP3
RS 2012r2


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group