stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
Hi Ladies and Gents,

I am new to VS2012, and used VB6 eons ago.

I have been asked to create a program to record serial data and audio data (from Multiple sources).

I was wondering if a DLL is the way to go and if it can do what i need it to.

Firstly i need a DLL to record the serial data to hard drive, with methods to set Baudrate, com port and data bits etc
i created a DLL see code below:

PHP:
Public Class SerialData

    Dim WithEvents SerialPort1 As New System.IO.Ports.SerialPort

    Public Function UpdateSettings(ByVal Port As String, ByVal baudSpeed As Integer, ByVal dataBits As Integer, _
                                   ByVal parity As System.IO.Ports.Parity, ByVal stopBits As System.IO.Ports.StopBits)
On Error GoTo notSet
        Dim result As String = "Error"
        SerialPort1.PortName = Port
        SerialPort1.BaudRate = baudSpeed
        SerialPort1.DataBits = dataBits
        SerialPort1.StopBits = stopBits
        SerialPort1.Parity = parity
        result = "Complete"
notset:
        Return result
    End Function
    Public Function getSettings()
        Dim result As String
        result = SerialPort1.BaudRate
        Return result
    End Function
End Class

i found when i send the updateSettings from a Windows Forms program the response comes back Complete, but when i use the getSettings method the response is always 9600 no matter what i change it to with the updateSettings method.

Does the DLL not retain the settings for the serialPort1???

Also i have to record audio from multiple sources and wondered if i could create a DLL to record audio to hard drive and then run multiple instances of it depending on the number of microphones fitted???

Any help would be much appreciated

Regards

Stu
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
I haven't used the SerialPort class much so I don't know all the ins and outs but I'm guessing that this from the documentation for the BaudRate property might be relevant:
The baud rate must be supported by the user's serial driver. The default value is 9600 bits per second (bps).
By the way, your VB6 heritage is showing as that code's a bit dodgy. Firstly, you should pretty much never use On Error in VB.NET. Also, all functions should specify their return type. I strongly recommend that you turn Option Strict On and it will enforce that last. That first function should be returning a Boolean rather than a String and, as it stands, that second function should be returning an Integer.
Imports System.IO.Ports

Public Class SerialData

    Dim WithEvents SerialPort1 As New SerialPort

    Public Function UpdateSettings(ByVal Port As String,
                                   ByVal baudSpeed As Integer,
                                   ByVal dataBits As Integer, _
                                   ByVal parity As Parity,
                                   ByVal stopBits As StopBits) As Boolean
        Dim result As Boolean

        Try
            SerialPort1.PortName = Port
            SerialPort1.BaudRate = baudSpeed
            SerialPort1.DataBits = dataBits
            SerialPort1.StopBits = stopBits
            SerialPort1.Parity = parity
            result = True
        Catch
            result = False
        End Try

        Return result
    End Function

    Public Function GetSettings() As Integer        
        Return SerialPort1.BaudRate
    End Function

End Class
 

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
Thanks for the reply jmcilhinney, yeah i am old school trying to get my self up to date :), i changed the code to your example but changed the following so i could see what was happening, The updateSettings i changed the return to an integer and returned the Baudrate to see if it actually did change. and the baudrate returned was 4800 (this is what i sent), but then if i use the getSettings directly after the DLL returns 9600 again. So it seems the DLL doesn't hold on the the values set???

Any ideas?

The changed code is below:

PHP:
Imports System.IO.Ports

Public Class SerialData

    Dim WithEvents SerialPort1 As New SerialPort

    Public Function UpdateSettings(ByVal Port As String, ByVal baudSpeed As Integer, ByVal dataBits As Integer, ByVal parity As Parity, ByVal stopBits As StopBits) As Integer
        Dim result As Integer

        Try
            SerialPort1.PortName = Port
            SerialPort1.BaudRate = baudSpeed
            SerialPort1.DataBits = dataBits
            SerialPort1.StopBits = stopBits
            SerialPort1.Parity = parity
            '   result = True
        Catch ex As Exception
            '  result = False
        End Try
        Return SerialPort1.BaudRate
    End Function
    Public Function getSettings() As Integer

        Return SerialPort1.BaudRate

    End Function

End Class

Thanks

Stu
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
You're definitely using the same instance of SerialData, aren't you? Is there any other code in your class that could be messing things up?

By the way, if it's not already, that class should be implementing IDisposable and disposing the SerialPort object.
 

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
jmcilhinney,

I thought i was a NOOB before but, i was declaring an instance for one button in the Windows Forms Application to set the attributes and then setting another one in the getsettings so as you said i wan't using the same instance of SerialData.

Thanks for pointing this Stupid error out, one day i may actually get to grips with this new VB2012, but this will probably be when VB2030 is out and everything has changed.

Thanks

Stu
 

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
I expect this is another numpty question about VB.NET DLL classes.

I have

PHP:
    Imports System.Timers
Public Class DoData

Dim WithEvents SecondMarker As New Timer

Private Sub SecondMarker_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles SecondMarker.Tick
        Write_Data("%" & NumSecPastMidnight())
    End Sub

End Class

as part of a class, i have imported System.timers and declared the timer SecondMarker as in the code above.

But in the 'error list' window there is:

'Error 6 Event 'Tick' cannot be found.'

so to me it looks like VS is not recognising the Handles SecondMarker.Tick event.

I am pretty sure i have some small thing wrong that is causing this?

any ideas on where i am going wrong

Thanks

Stu
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

By adding the Imports statement to System.Timers you have accidentally ended up declaring and using a variable of the wrong type of Timer. You need to be using the System.Windows.Forms Timer class instead. To solve this, just get rid of the Imports statement. For more information on the different Types of Timers in .NET have a read of this:-

Comparing the Timer Classes in the .NET Framework Class Library

Hope that helps.

Cheers,

Ian
 

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
I removed the Imports System.Timers and now when i declare the SecondMarker as Timer the error list shows 'Error 1 Type 'Timer' is not defined.

I then tried using Imports System.Windows.Forms.Timers but after typing in System.Windows there was no intellisense option for Forms or timers.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
If you want to use members of the System.Windows.Forms namespace then you need to reference the System.Windows.Forms.dll assembly. You should not do that unless you intend this library to be used only in WinForms apps. There's no specific reason not to use the System.Timers.Timer, but it has an Elapsed event rather than Tick. It's also important to note that the WinForms Timer always raises its Tick event on the UI thread, while the Timers.Timer raises its Elapsed event on a thread pool thread by default. If you want the Elapsed event raised on the UI thread then you can set the SynchronizingObject property.
 

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
Thanks for all the help so far guys, sorry to be asking some noob questions, hopefully it wont take me too long to find my way around the new Visual Studio

:)
 
Top Bottom