wong
Member
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
'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
...
...
...
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()
'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)
...
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
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
Data source name not found and no default driver specified.
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
Dim conn as new ODBC.OdbcConnection(MyConnString)