|
Post by angelo2449 on Jun 2, 2019 14:54:15 GMT -5
I'm writing a little program that uses the Arduino Uno card to drive a series of relays.
To drive the relays I use this code:
if Test = 0 then num$ = '111' gosub [Porta] end if
.......... ..........
[Porta]
open ComPort $; ': 9600,8, N, 1, N' for random as serial print serial, num$ close serial
this causes the relay number 1 to energize.
How can I detect the presence of the Arduino Uno card or that it is actually connected to the serial port (USB)?
This is to prevent the system from crashing due to the error generated by the lack of Arduino Uno.
Thank you very much!
|
|
|
Post by Rod on Jun 3, 2019 3:18:02 GMT -5
Well it depends how sophisticated you want to be. A simple method is to test all ports and put up a list of those available to be opened. Ports in use wont be listed. Note in the code how the oncomerror is set and reset as each port is tested.
Most often users will plug in just one usb serial port connector. If this code is run after that has been done it will find and list the port.
To get more sophistication you need to have a conversation with your Arduino, if it responds you have the right connection.
maxPorts=40 dim port$(maxPorts) statictext #main.txt, "Select Port", 75, 55, 100, 20 combobox #main.portcb, port$(), [portDoubleClick], 75, 75, 100, 100 open "Get port example" for window as #main print #main, "trapclose [quit]"
' Populate the drop down list of available COM ports gosub [getPorts] wait
' Handle the combobox doubleclick event [portDoubleClick] print #main.portcb, "contents? Com$" open Com$;":9600,n,8,1,ds0,cs0,rs" for random as #com wait
[getPorts] for port = 1 to maxPorts port$(port)="" next index=1 ' now find all active ports for port = 1 to maxPorts oncomerror [trap] open "Com";str$(port);":9600,n,8,1,ds0,cs0,rs" for random as #com port$(index)="Com";str$(port) index=index+1 close #com
[trap] oncomerror next print #main.portcb, "reload" print #main.portcb, "selectindex 1" return
[quit] close #main end
|
|