Question How do you check if a GPIO is open or closed please?

dHeather

Active member
Joined
Jun 18, 2012
Messages
27
Programming Experience
Beginner
Hello,

I need to write a program which can detect whether an IO on a Numato 8 Channel USB GPIO Module is open or closed or on or off (apologies I'm not sure of the right terminology). When plugged in and set up the GPIO Module appears on my machine as a serial port (COM 4). I have tested the unit via HyperTerminal and it is all working correctly returning 0's and 1 for open and closed. I can't find a way of doing that in VB.NET though.

I imagine that this should be very easy, but everything I have read up on only seems to be interested in writing to and reading from whatever is at the other end of the serial port of which I have nothing, just a set of 8 switches.

In short I want to be able to detect that an IO has changed state and then act upon that change.

Can anybody point me in the right direction please? (Please note that I am not an experienced programmer, so the more basic the info the better please).

Apologies if this is posted in the wrong area.

Thanks for your help
 
Look up the SerialPort class.
 
Hello,

Thanks for your quick response. I have looked through the SerialPort class and have limped forward a little, but I am still struggling. If I write to the board "GPIO Read 0" the board tells me the state of I/O zero. This works perfectly in HyperTerminal but in VB when I SerialPort.ReadLine() I get my sent line back as well as the I/O state. I have fudged my way around that (probably very badly) by reading the board twice and ignoring the first response.

What I need help with is that I was hoping that when I flicked a switch VB would detect that change and respond accordingaly. I can't see any way of doing that though. Do I need to continually keep asking the I/O for thier state and then respond if changed or is there a better way?

This is my code so far. It works on a button press at the moment, but I really need it to respond live to the open/closed state of an I/O.

Imports
System

Imports
System.ComponentModel

Imports
System.Threading

Imports
System.IO.Ports


Public
Class Form1


Public CurrentPort As Integer



Private port As New SerialPort("COM4", 9600, Parity.None, 8, StopBits.One)



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


AddHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived)

port.Open()


End Sub



Private Sub All_Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click


Dim CurrentButton As Button = sender

CurrentPort = CurrentButton.Text

port.Write(
"gpio read " & CurrentPort & vbCr)


End Sub



Private Sub port_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)


Dim ClearLine As String = port.ReadLine()

MsgBox(
"Port " & CurrentPort & " state = " & port.ReadLine())


End Sub

End
Class

Any help greatly appreciated.

Thanks
 
I don't know the device and its protocol, but if it were supposed to send a message to the port when you use the switch you should have seen that. I have no idea if it is relevant, but I would check the PinChanged event, too.
 
Back
Top