serial com

wong

Member
Joined
Jul 17, 2008
Messages
13
Location
Johor,Malaysia
Programming Experience
Beginner
Can anybody teach me how to use the serial com function in the toolbox.I want to retrieve data from the serial com and store in access database.Thanks
 
Hello.

That's easy.
All you have to do is have a look at it. Drag it onto your form, set it's properties and after that it's ready to be used in the code.

VB.NET:
'set the line end...different for each device!
Me.NewLine = vbNewLine

'set the timout
Me.YourSerialPort.WriteTimeout = 1500
Me.YourSerialPort.ReadTimeout = 1500

'send something...
Me.YourSerialPort.Write(byteBuffer, 0, 1024)
Me.YourSerialPort.Write(charBuffer, 0, 1024)
Me.YourSerialPort.Write("Hello World!")

'this will be automatically closed with the defined NewLine Symbol
Me.YourSerialPort.WriteLine("Hello World!")

'read something
...
...
...

Hope this helps,
Bobby
 
Thanks for the reply,
I can set the timeout in the properties right?I don' have to write the code=Me.YourSerialPort.WriteTimeout = 1500.

By the way,what does the Me. in front of the line for?Do i have to call any resources?(Because it is highlighted as syntax error)

Can you show me how to store the data from the serial com in to access
based on the user's keyed in name?(textbox)
 
If there's a timeOut Property then it is so.
Me means nothing more then what This is for all C like languages. With Me you're calling the actual object, f.e. the form, or the class in which you're writing code.
Why is it marked? What tells the error message? Me is only legal within Objects, not within Modules and similar.

To store something into the Access Database you can use the SQL or ODBC Namespace. For the connection string have a look here: ConnectionStrings.com - How to connect to Access 2007

VB.NET:
Dim conn as New ODBC.ODBCConnection(yourConnectionStringHere)

'open the connection
conn.Open()

'declare the command
Dim insertCmd as New ODBC.ODBCCommand(yourSQLQuery, conn)

'execute the command and retrieve the number of effected lines
If insertCmd.ExecuteNonQuery() > 0 Then
      'everything is okay
Else
      'insertion failed
End If

'close the conn
conn.Close()

Bobby
 
Sorry if i'm troubling you, but you never show me how to read from serial and store in variable.
Also what is SQL query?Is it the query in MS Access(2003)?How do i put it in the code?

Thanks
 
Right, Somehow I managed to miss at least one line with reading code...odd.

However, if you have a look at the SerialPort Object you'll find different version of Read().
VB.NET:
'serialPort opened and so on.
'reads until it hits the defined NewLine Symbol.
myvariable = Me.YourSerialPort.ReadLine()

Me.YourSerialPort.Read(byteBuffer, 0, 1024)
Me.yourSerialPort.Read(charBuffer, 0, 1024)
...

If you use the last two Read-Methods you'll have go through a loop until it reads nothing anymore.

yourSQLQuery is the SQL query you want to execute as String.

Bobby
 
I still can't get it.Can you help me correct my program.

VB.NET:
Public Class Form1

    Dim WithEvents serialPort As New IO.Ports.SerialPort

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        serialPort.Open()
        Dim conn As New Odbc.OdbcConnection
        Dim int As Char

        conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = D:\FYP\db2.mdb"
        conn.Open()

        'Read the corresponding value and store
        int = Me.serialPort.ReadLine()

        Me.serialPort.Read(byteBuffer, 0, 1024)
        Me.serialPort.Read(charBuffer, 0, 1024)
        conn.Close()

    End Sub
 
You're missing some things, f.e. telling the class which Serial Port you want. Or closing the Port so that it can be reused.

VB.NET:
Public Class Form1

      Private WithEvents serialPort As New IO.Ports.SerialPort("COM1")

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                  serialPort.Open()
                  Dim conn As New Odbc.OdbcConnection
                  Dim int As String = ""

                  conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\FYP\db2.mdb"
                  conn.Open()

                  'Read the corresponding value and store it
                  int = Me.serialPort.ReadLine()

                  serialPort.Close()

                  'here should be something what executes the NonQuery like in my second post

                  conn.Close()
            Catch ex as Exception
                  MsgBox(ex.Message)
            Finalize
                  If conn IsNot Nothing Then conn.Close()
                  serialPort.Close()
            End Try
End Sub

Bobby
 
Database

I know this is out of topic but can you tell me why i got this error.My connection string is correct.

Can you check this code:I/ve got ERROR[IM002]
Data source name not found and no default driver specified.

VB.NET:
Imports System.Data.OleDb

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim int As String = 15
        Dim conn As New Odbc.OdbcConnection
        Dim SqlString As String = "INSERT INTO data(chinups,date,ID) Values('" & txtname.Text & "',#" & Today.Date & "#," & CStr(int) & ")"

        conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = D:\FYP\db2.mdb"
        'open the connection
        conn.Open()

        'declare the command
        Dim insertCmd As New Odbc.OdbcCommand(SqlString, conn)

        'execute the command and retrieve the number of effected lines
        If insertCmd.ExecuteNonQuery() > 0 Then
            'everything is okay
        Else
            'insertion failed
        End If

        'close the conn
        conn.Close()
    End Sub
End Class
 
When i debug it the form is able to load.The error occurs when i click the button(Button event).I think my sql string is wrong because the code i'm using is for one table only while my database have two linked tables.
 
Back
Top