Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Need example of serial session event handler in visual basic (2005)

New to both Visual Basic (.net 2005) and the Measurement Studio plugin with NI-Visa. I want to test the Visa serial (and later USB) I/O in a simple read com port app. Our device continually sends data out in a record format and our GUI needs to receive it but not spend all its time waiting. The data is sent at 4800 baud so it's pretty slow and an event handler that fires on a character received and then puts in a circular buffer is all I think we need. Any example code would be greatly appreciated. We are evaluating the Measurement Studio plugin so this would really help with our decision to go with it.
Thanks,
Alex
0 Kudos
Message 1 of 9
(5,669 Views)
ajs,

There is a simple example of using VISA in VB.NET located at C:\Program Files\National Instruments\MeasurementStudioVS2005\DotNET\Examples\Visa\SimpleReadWrite\vb as long as you installed VISA to the default directories. Also, Measurement Studio includes the Instrument I/O Assistant which is a graphical sort of wizard that helps create serial and GPIB calls using VISA.

Brandon Vasquez | Software Engineer | Integration Services | National Instruments
0 Kudos
Message 2 of 9
(5,658 Views)

Hi Brandon,

We tried the example code you mentioned (SimpleAcynchronousReadWrite) but haven't had a lot of success with it. It's not doing what we hoped it would do which is to sync up to the data records sent by our product. The data rate is slow (4800 baud) with 106 bytes of data sent every .4 seconds continuously.

Alex

0 Kudos
Message 3 of 9
(5,646 Views)
Hi Brandon,

Could you please give me a little more detail on what you mean by "sync up to the data records"? Are you able to receive the data from your instrument? Please let me know, and I will do my best to help out. Thanks!

Regards,
Ebele O.
National Instruments
0 Kudos
Message 4 of 9
(5,609 Views)

Hi Ebele,

Our product sends record data at the rate of 106 bytes (hex, not ascii) every .4 second without quierying so our com routine is just listens. We tried "ReadByteArray()" called at a rate to read every record and it keeps losing data. A data record starts with three 0's, which is how we sync to it. What happens is we keep losing the start of the data record and don't know why. We also tried "BeginRead" and "OnReadComplete" with similar unsuccessful results.

Thanks,

Alex

0 Kudos
Message 5 of 9
(5,608 Views)
Hi Alex (I am sorry I called you Brandon in my previous post. oops!),

Do you always lose the first part of the data, or is it random? How often are you reading data from the port, and how many bytes are you reading each time? ( I assume you are reading in some kind of loop). You can use the VISA "Number of bytes at serial port" property to determine the number of bytes to read. Could you give me an example of what data your device sends and what data you are reading back for a few iterations of your loop? Also, could you show me a snippet of the code you are using to read from the port. Thanks!

Regards,

Message Edited by _Belle on 03-22-2007 02:09 PM

Ebele O.
National Instruments
0 Kudos
Message 6 of 9
(5,563 Views)

Hi Ebele,

Thanks for your reply. We have a product called a "tool" that is used to locate the drill bit for oil/gas/coal drilling. The tool sends back data to a power supply that then sends data to our GUI running on a PC. The tool data is sent as a record of 106 bytes and it is sent every .4 seconds or so. The interface to the power supply can also be a USB connection but the data rates are the same. The serial port transmits at 4800 baud, no parity, 1 stop, and no handshaking.

The code I played with uses the "ReadByteArray" method and it is called after a timer event. I've played with the interval and timeout parameters and got it so it could read good but still would start missing records and sometimes hangup.

Thanks, Alex.

Here is a code snipet:

Private Sub RecPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecPort.TextChanged

Dim comstr As String

If RecPort.SelectedItem <> Nothing Then

comstr = RecPort.SelectedItem

If ComPort <> comstr Then

ComPort = comstr

If VisaResourceStr <> Nothing Then

mbSession.Dispose()

End If

Select Case ComPort

Case "COM1"

VisaResourceStr = "ASRL1::INSTR"

Case "COM2"

VisaResourceStr = "ASRL2::INSTR"

Case "COM3"

VisaResourceStr = "ASRL3::INSTR"

Case "COM4"

VisaResourceStr = "ASRL4::INSTR"

Case "COM5"

VisaResourceStr = "ASRL5::INSTR"

End Select

If VisaResourceStr <> Nothing Then

Dim SerSess As New VisaNS.SerialSession(VisaResourceStr)

mbSession = SerSess

'mbSession.Timeout = 10000

SerSess.BaudRate = 4800

SerSess.StopBits = StopBitType.One

SerSess.Parity = Parity.None

SerSess.FlowControl = FlowControlTypes.None

SerSess.Timeout = 100

SerSess.DefaultBufferSize = 2048

End If

End If

End If

End Sub

 

Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click

Timer1.Interval = 300

Timer1.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

read_tool()

End Sub

Private Sub read_tool()

Dim rbarray() As Byte

 

If VisaResourceStr <> Nothing Then

Try

rbarray = mbSession.ReadByteArray(106)

Catch ex As Exception

'MessageBox.Show("Exception: " & ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.None)

'Exit Sub

End Try

If (Not (rbarray Is Nothing)) Then

ib.Copy(rbarray, ib, rbarray.Length)

copy_records(ib, rbarray.Length)

End If

Else

'MessageBox.Show("Enter a valid COM port", "Error", MessageBoxButtons.OK, MessageBoxIcon.None)

End If

End Sub

0 Kudos
Message 7 of 9
(5,546 Views)
I tried the "AvailableNumber" and that worked! Still testing but so far no crashes
Thanks Ebele.
Alex
0 Kudos
Message 8 of 9
(5,530 Views)
Hi Alex,

I am glad the function worked for you, and you are able to get things working now.

Regards,
Ebele O.
National Instruments
0 Kudos
Message 9 of 9
(5,520 Views)