Author |
Topic: Com port code works in lb but not lbb (Read 1097 times) |
|
mmiscool
New Member
member is offline
Posts: 19
|
|
Com port code works in lb but not lbb
« Thread started on: Mar 28th, 2015, 5:04pm » |
|
Hello,
The code at the following page for liberty basic works under LB. http://lbpe.wikispaces.com/Using+an+Arduino+as+a+slave+board%2C+Arduino+serial+interfacing+and+basic+function+library
However when I attempt to use it in LBB it dose not work in the same way. I don't get the return message passed back to the text box and t arduino dose not seem to receive the command.
Any ideas.
I am attempting to take advantage of the fact that LBB can produce a true command line program that can be accessed from the shell function under run basic and return values from the arduino board.
This would be very useful for home automation.
|
|
Logged
|
|
|
|
Rod
Full Member
member is offline
Gender:
Posts: 110
|
|
Re: Com port code works in lb but not lbb
« Reply #1 on: Mar 28th, 2015, 7:01pm » |
|
The com port open statement is different, read section 9 of the booster documentation linked at the top of the forum
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Com port code works in lb but not lbb
« Reply #2 on: Mar 28th, 2015, 8:15pm » |
|
on Mar 28th, 2015, 5:04pm, mmiscool wrote:The ... arduino dose not seem to receive the command. |
|
Check that you are asserting the CTS and DSR inputs to the serial port. My understanding is that Windows enables hardware handshaking by default, unless you explicitly include octs=off odsr=off in the config string, so if you are not holding those inputs high (typically by looping back the RTS and DTR outputs) nothing may be sent.
Edit: No, I was wrong, the default is to disable handshaking as it says here.
Richard.
|
|
|
|
Rod
Full Member
member is offline
Gender:
Posts: 110
|
|
Re: Com port code works in lb but not lbb
« Reply #3 on: Mar 29th, 2015, 3:13pm » |
|
I ran the example sketch SerialReadAnalog but set the delay to 1000ms then ran this code without any problem.
I just dropped all of the handshaking as Richard shows in his notes.
Code:'open "com4:9600,n,8,1,ds0,cs0,rs" for random as #commHandle
open "com4:9600,n,8,1" for random as #commHandle
timer 500,[read]
wait
[read]
dataRead$ = input$(#commHandle, lof(#commHandle))
l=len(dataRead$)
print l,
for n=1 to l
print asc(mid$(dataRead$,n,1));",";
next
print
wait
So what error message are you getting?
|
« Last Edit: Mar 29th, 2015, 3:14pm by Rod » |
Logged
|
|
|
|
mmiscool
New Member
member is offline
Posts: 19
|
|
Re: Com port code works in lb but not lbb
« Reply #4 on: Mar 29th, 2015, 3:52pm » |
|
Updated the code.
Still not getting a good response. ' Scratches head???
Code:dim pins$(100)
'Populate pin list
for x = 0 to 13
pins$(x) = str$(x)
next x
nomainwin
WindowWidth = 360
WindowHeight = 445
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
listbox #main.PinSelector, pins$(, [listbox1DoubleClick], 10, 82, 130, 320
statictext #main.statictext2, "Pin Selector", 10, 62, 130, 20
textbox #main.ValToSend, 165, 32, 170, 25
statictext #main.statictext4, "Value To Send", 165, 12, 170, 20
statictext #main.statictext5, "Return Value", 165, 62, 175, 20
textbox #main.ReturnValue, 165, 82, 170, 25
button #main.button7,"Set Pin High/Low Status",[Set.Pin], UL, 165, 117, 170, 25
button #main.button8,"Get Pin High/Low Status",[Get.Pin], UL, 165, 152, 170, 25
button #main.button9,"PWM Output (Servos)",[PWM.Out], UL, 165, 187, 170, 25
button #main.button10,"PWM Input",[PWM.In], UL, 165, 222, 170, 25
statictext #main.statictext11, "Com Port Number", 10, 12, 145, 20
textbox #main.ComPortNo, 10, 32, 130, 25
open "LB Ultra Simple Arduino Demo" for dialog as #main
print #main, "trapclose [quit.main]"
wait
[Set.Pin]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.Set(Com.port.Selection, Pin.Selection, Val.To.Send)
print #main.ReturnValue, return.value
wait
[Get.Pin]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.Get(Com.port.Selection, Pin.Selection)
print #main.ReturnValue, return.value
wait
[PWM.Out]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.PWM.Out(Com.port.Selection, Pin.Selection, Val.To.Send)
print #main.ReturnValue, return.value
wait
[PWM.In]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.PWM.In(Com.port.Selection, Pin.Selection)
print #main.ReturnValue, return.value
wait
[quit.main]
close #main
end
[Get.The.Selctions.And.Textboxes.From.Main.Win]
print #main.ValToSend, "!contents? Val.To.Send";
print #main.ComPortNo, "!contents? Com.port.Selection";
print #main.PinSelector, "selection? Pin.Selection"
return
'Begin Arduino Code here -------------------------------------------------------
'These functions can be added to any program to provide arduini power input and output
'Descriptions for functions are in there comments
function AD.Set(com.port, PinNo, value)
'Set an Arduino pin high or low,
'Will return 1 one if successful, 0 if not
AD.Set = val(AD.talk.to.device$(com.port,"set ";PinNo;" ";value))
end function
function AD.Get(com.port, PinNo)
'Will return the current state of the input pin
'1 for high, 0 for low
AD.Get = val(AD.talk.to.device$(com.port,"get ";PinNo))
end function
function AD.PWM.Out(com.port, PinNo, value)
'Set the PWM output on the select pin to the value
'Will return a 1 for success and 0 for failure.
AD.PWM.Out = val(AD.talk.to.device$(com.port,"pwm.out ";PinNo;" ";value))
end function
function AD.PWM.In(com.port, PinNo)
'Get the PWM input from the pin
'Will return a value of 0 to 1023
AD.PWM.In = val(AD.talk.to.device$(com.port,"pwm.in ";PinNo))
end function
Function AD.talk.to.device$(port,msg$)
'Send a Message to the arduino and return the result sent back by the device
if msg$ <> "" then
'Open the com port
open "COM" ; port ; ":baud=9600 parity=n data=8 stop=1 odsr=off octs=off" for random as #ArduinoCOMPort
'open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort
'Send message to device
print #ArduinoCOMPort, msg$
'Wait until there is a carriage return
while foundLF = 0
while lof(#ArduinoCOMPort) > 0
temp$ = input$(#ArduinoCOMPort, 1)
buffer$ = buffer$ + temp$
if temp$ = chr$(13) then
foundLF = 1
exit while
end if
wend
wend
'Place the returned text in to AD.talk.to.device$
AD.talk.to.device$ = buffer$
close #ArduinoCOMPort
else
Notice "Improper Message Received, Must contain a command"
end if
End function
|
« Last Edit: Mar 29th, 2015, 3:58pm by mmiscool » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: Com port code works in lb but not lbb
« Reply #5 on: Apr 15th, 2015, 08:51am » |
|
on Mar 29th, 2015, 3:52pm, mmiscool wrote:Updated the code. Still not getting a good response. Scratches head |
|
MSDN confirms that the default is no hardware handshaking, so that explains why adding the 'odsr=off octs=off' makes no difference.
So where do we currently stand on this one? Rod, if you are monitoring this thread, does Michael's code work for you using LBB?
Richard.
|
|
Logged
|
|
|
|
Rod
Full Member
member is offline
Gender:
Posts: 110
|
|
Re: Com port code works in lb but not lbb
« Reply #6 on: Apr 15th, 2015, 3:51pm » |
|
Not had any time to test but I will, should be perfectly straightforwards. I don't think LBB needs any handshaking, Liberty BASIC has always been happier with the handshaking syntax set. Carl has explained why some time in the past but I can't find it to quote.
|
|
Logged
|
|
|
|
mmiscool
New Member
member is offline
Posts: 19
|
|
Re: Com port code works in lb but not lbb
« Reply #7 on: Apr 16th, 2015, 02:23am » |
|
Any help on this would be greatly appreciated.
I would like to take advantage of the speed that lbb provides. I have some old image recognition code written in lb that I want to use but it would take approximately 30 seconds to anelise a frame. Lbb should be able to do it faster.
Working on a couple of robot projects now that this would be extremely useful with.
|
|
Logged
|
|
|
|
Rod
Full Member
member is offline
Gender:
Posts: 110
|
|
Re: Com port code works in lb but not lbb
« Reply #8 on: Apr 20th, 2015, 08:52am » |
|
OK, it is a simple speed issue and program structure fault.
Typically you will open a serial port once, give it a little time to establish and then use it. A serial port can take a good few hundred milliseconds to establish. Once completely finished close it once.
My test code gave the port 500ms before attempting to use it.
So that's all that is required to fix the problem. In this code I open the port first thing, by the time the user gets round to pressing keys the port is established and responds without the need of any handshaking.
Code:port=4 'your com port number
'Open the com port
open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort
dim pins$(100)
'Populate pin list
for x = 0 to 13
pins$(x) = str$(x)
next x
'nomainwin
WindowWidth = 360
WindowHeight = 445
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
listbox #main.PinSelector, pins$(, [listbox1DoubleClick], 10, 82, 130, 320
statictext #main.statictext2, "Pin Selector", 10, 62, 130, 20
textbox #main.ValToSend, 165, 32, 170, 25
statictext #main.statictext4, "Value To Send", 165, 12, 170, 20
statictext #main.statictext5, "Return Value", 165, 62, 175, 20
textbox #main.ReturnValue, 165, 82, 170, 25
button #main.button7,"Set Pin High/Low Status",[Set.Pin], UL, 165, 117, 170, 25
button #main.button8,"Get Pin High/Low Status",[Get.Pin], UL, 165, 152, 170, 25
button #main.button9,"PWM Output (Servos)",[PWM.Out], UL, 165, 187, 170, 25
button #main.button10,"PWM Input",[PWM.In], UL, 165, 222, 170, 25
statictext #main.statictext11, "Com Port Number", 10, 12, 145, 20
textbox #main.ComPortNo, 10, 32, 130, 25
open "LB Ultra Simple Arduino Demo" for dialog as #main
print #main, "trapclose [quit.main]"
wait
[Set.Pin]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.Set(Com.port.Selection, Pin.Selection, Val.To.Send)
print #main.ReturnValue, return.value
wait
[Get.Pin]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.Get(Com.port.Selection, Pin.Selection)
print #main.ReturnValue, return.value
wait
[PWM.Out]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.PWM.Out(Com.port.Selection, Pin.Selection, Val.To.Send)
print #main.ReturnValue, return.value
wait
[PWM.In]
gosub [Get.The.Selctions.And.Textboxes.From.Main.Win]
return.value = AD.PWM.In(Com.port.Selection, Pin.Selection)
print #main.ReturnValue, return.value
wait
[quit.main]
close #main
close #ArduinoCOMPort
end
[Get.The.Selctions.And.Textboxes.From.Main.Win]
print #main.ValToSend, "!contents? Val.To.Send";
print #main.ComPortNo, "!contents? Com.port.Selection";
print #main.PinSelector, "selection? Pin.Selection"
return
'Begin Arduino Code here -------------------------------------------------------
'These functions can be added to any program to provide arduini power input and output
'Descriptions for functions are in there comments
function AD.Set(com.port, PinNo, value)
'Set an Arduino pin high or low,
'Will return 1 one if successful, 0 if not
AD.Set = val(AD.talk.to.device$(com.port,"set ";PinNo;" ";value))
end function
function AD.Get(com.port, PinNo)
'Will return the current state of the input pin
'1 for high, 0 for low
AD.Get = val(AD.talk.to.device$(com.port,"get ";PinNo))
end function
function AD.PWM.Out(com.port, PinNo, value)
'Set the PWM output on the select pin to the value
'Will return a 1 for success and 0 for failure.
AD.PWM.Out = val(AD.talk.to.device$(com.port,"pwm.out ";PinNo;" ";value))
end function
function AD.PWM.In(com.port, PinNo)
'Get the PWM input from the pin
'Will return a value of 0 to 1023
AD.PWM.In = val(AD.talk.to.device$(com.port,"pwm.in ";PinNo))
end function
Function AD.talk.to.device$(port,msg$)
'Send a Message to the arduino and return the result sent back by the device
if msg$ <> "" then
'Send message to device
print #ArduinoCOMPort, msg$
'Wait until there is a carriage return
while foundLF = 0
while lof(#ArduinoCOMPort) > 0
temp$ = input$(#ArduinoCOMPort, 1)
buffer$ = buffer$ + temp$
if temp$ = chr$(13) then
foundLF = 1
exit while
end if
wend
wend
'Place the returned text in to AD.talk.to.device$
AD.talk.to.device$ = buffer$
else
Notice "Improper Message Received, Must contain a command"
end if
End function
|
|
Logged
|
|
|
|
|