I have prepared the below coding to receive data from rs232 serial port. - Option Explicit
-
-
Dim Port As String
-
Dim BRate As String
-
Dim DBits As String
-
Dim Parity As String
-
Dim SBits As String
-
Dim Flow As String
-
-
Private Sub cboBRate_Change()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboBRate_Click()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboBRate_KeyDown(KeyCode As Integer, Shift As Integer)
-
On Error Resume Next
-
If KeyCode = 38 Then
-
Let cboPort.SelStart = 0
-
cboPort.SetFocus
-
ElseIf KeyCode = 40 Then
-
Let cboDBits.SelStart = 0
-
cboDBits.SetFocus
-
End If
-
End Sub
-
-
Private Sub cboBRate_KeyPress(KeyAscii As Integer)
-
On Error Resume Next
-
If KeyAscii = 8 Or KeyAscii = 48 Or KeyAscii = 49 Or KeyAscii = 51 Or KeyAscii = 52 Or KeyAscii = 53 Or KeyAscii = 54 Or KeyAscii = 55 Or KeyAscii = 56 Or KeyAscii = 57 Then
-
MsgBox "Please enter number only!", vbExclamation
-
Let cboBRate.SelStart = 0
-
Let cboBRate.SelLength = 20
-
cboBRate.SetFocus
-
End If
-
End Sub
-
-
Private Sub cboDBits_Change()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboDBits_Click()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboDBits_KeyDown(KeyCode As Integer, Shift As Integer)
-
On Error Resume Next
-
If KeyCode = 38 Then
-
Let cboDBits.SelStart = 0
-
cboDBits.SetFocus
-
ElseIf KeyCode = 40 Then
-
Let cboParity.SelStart = 0
-
cboParity.SetFocus
-
End If
-
End Sub
-
-
Private Sub cboDBits_KeyPress(KeyAscii As Integer)
-
On Error Resume Next
-
If KeyAscii = 8 Or KeyAscii = 48 Or KeyAscii = 49 Or KeyAscii = 51 Or KeyAscii = 52 Or KeyAscii = 53 Or KeyAscii = 54 Or KeyAscii = 55 Or KeyAscii = 56 Or KeyAscii = 57 Then
-
MsgBox "Please enter number only!", vbExclamation
-
Let cboDBits.SelStart = 0
-
Let cboDBits.SelLength = 20
-
cboDBits.SetFocus
-
End If
-
End Sub
-
-
Private Sub cboParity_Change()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboParity_Click()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboParity_KeyDown(KeyCode As Integer, Shift As Integer)
-
On Error Resume Next
-
If KeyCode = 38 Then
-
Let cboDBits.SelStart = 0
-
cboDBits.SetFocus
-
ElseIf KeyCode = 40 Then
-
Let cboSBits.SelStart = 0
-
cboSBits.SetFocus
-
End If
-
End Sub
-
-
Private Sub cboPort_Change()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboPort_Click()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboPort_KeyDown(KeyCode As Integer, Shift As Integer)
-
On Error Resume Next
-
If KeyCode = 40 Then
-
Let cboBRate.SelStart = 0
-
cboBRate.SetFocus
-
End If
-
End Sub
-
-
Private Sub cboSBits_Change()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboSBits_Click()
-
Let cmdApply.Enabled = True
-
End Sub
-
-
Private Sub cboSBits_KeyDown(KeyCode As Integer, Shift As Integer)
-
On Error Resume Next
-
If KeyCode = 38 Then
-
Let cboParity.SelStart = 0
-
cboParity.SetFocus
-
End If
-
End Sub
-
-
Private Sub cboSBits_KeyPress(KeyAscii As Integer)
-
On Error Resume Next
-
If KeyAscii = 8 Or KeyAscii = 48 Or KeyAscii = 49 Or KeyAscii = 51 Or KeyAscii = 52 Or KeyAscii = 53 Or KeyAscii = 54 Or KeyAscii = 55 Or KeyAscii = 56 Or KeyAscii = 57 Then
-
MsgBox "Please enter number only!", vbExclamation
-
Let cboSBits.SelStart = 0
-
Let cboSBits.SelLength = 20
-
cboSBits.SetFocus
-
End If
-
End Sub
-
-
Private Sub cmdApply_Click()
-
On Error Resume Next
-
If MSComm1.PortOpen Then
-
MSComm1.PortOpen = Not MSComm1.PortOpen
-
MSComm1.CommPort = Val(Mid(cboPort.Text, 4, 1))
-
MSComm1.Settings = Val(cboBRate.Text) & "," & Left(cboParity.Text, 1) & "," & Val(cboDBits.Text) & "," & Val(cboSBits.Text)
-
With cboFControl
-
If .Text = "None" Then
-
MSComm1.Handshaking = comNone
-
ElseIf .Text = "Xon / Xoff" Then
-
MSComm1.Handshaking = comXOnXoff
-
ElseIf .Text = "RTS" Then
-
MSComm1.Handshaking = comRTS
-
ElseIf .Text = "RTS Xon / Xoff" Then
-
MSComm1.Handshaking = comRTSXOnXOff
-
End If
-
End With
-
End If
-
Let cmdApply.Enabled = False
-
cmdOK.SetFocus
-
End Sub
-
-
Private Sub cmdCancel_Click()
-
Let cboPort.Text = Port
-
Let cboBRate.Text = BRate
-
Let cboDBits.Text = DBits
-
Let cboParity.Text = Parity
-
Let cboSBits.Text = SBits
-
Let cboFControl.Text = Flow
-
Hide
-
End Sub
-
-
Private Sub cmdOK_Click()
-
On Error GoTo No_Receive
-
If MSComm1.PortOpen Then
-
MSComm1.PortOpen = Not MSComm1.PortOpen
-
MSComm1.CommPort = Val(Mid(cboPort.Text, 4, 1))
-
MSComm1.Settings = Val(cboBRate.Text) & "," & Left(cboParity.Text, 1) & "," & Val(cboDBits.Text) & "," & Val(cboSBits.Text)
-
With cboFControl
-
If .Text = "None" Then
-
MSComm1.Handshaking = comNone
-
ElseIf .Text = "Xon / Xoff" Then
-
MSComm1.Handshaking = comXOnXoff
-
ElseIf .Text = "RTS" Then
-
MSComm1.Handshaking = comRTS
-
ElseIf .Text = "RTS Xon / Xoff" Then
-
MSComm1.Handshaking = comRTSXOnXOff
-
End If
-
End With
-
End If
-
No_Receive:
-
MsgBox "Error receiving the data. Check your comm. port.", vbExclamation
-
End Sub
-
-
Private Sub Form_Activate()
-
On Error Resume Next
-
Let cmdOK.Default = True
-
Let cmdApply.Enabled = False
-
Let Port = cboPort.Text
-
Let BRate = cboBRate.Text
-
Let DBits = cboDBits.Text
-
Let Parity = cboParity.Text
-
Let SBits = cboSBits.Text
-
Let Flow = cboFControl.Text
-
cboPort.SetFocus
-
End Sub
-
-
Private Sub Form_Load()
-
Dim i As Integer, j As Integer, k As Double
-
Let Caption = "Real-time elevation via comm. port"
-
Let cmdCancel.Cancel = True
-
Let cboPort.Text = "COM1"
-
For i = 1 To 4
-
cboPort.AddItem "COM" & i
-
Next i
-
With cboBRate
-
.Text = 9600
-
.AddItem "110"
-
.AddItem "300"
-
.AddItem "600"
-
.AddItem "1200"
-
.AddItem "2400"
-
.AddItem "4800"
-
.AddItem "9600"
-
.AddItem "14400"
-
.AddItem "19200"
-
.AddItem "28800"
-
.AddItem "38400"
-
.AddItem "56000"
-
.AddItem "57600"
-
.AddItem "115200"
-
.AddItem "128000"
-
.AddItem "256000"
-
End With
-
Let cboDBits.Text = 8
-
For j = 4 To 8
-
cboDBits.AddItem j
-
Next j
-
With cboParity
-
.Text = "None"
-
.AddItem "Even"
-
.AddItem "Odd"
-
.AddItem "Mark"
-
.AddItem "Space"
-
End With
-
Let cboSBits.Text = 1
-
For k = 1 To 2 Step 0.5
-
cboSBits.AddItem k
-
Next k
-
With cboFControl
-
.Text = "None"
-
.AddItem "RTS"
-
.AddItem "RTS Xon / Xoff"
-
.AddItem "Xon / Xoff"
-
End With
-
-
End Sub
-
-
-
Private Sub MSComm1_OnComm()
-
Dim strRTL As String
-
'If MSComm1.CommEvent = comEvReceive Then
-
' RTData = MSComm1.Input
-
' lstDataLog.List = RTData
-
'End If
-
-
' Returns communication event or error
-
Select Case MSComm1.CommEvent
-
Case comBreak
-
Case comEventFrame
-
Case comEventOverrun
-
Case comEventRxOver
-
Case comEventRxParity
-
Case comEventTxFull
-
Case comEventDCB
-
-
Case comEvCD
-
Case comEvCTS
-
Case comEvDSR
-
Case comEvRing
-
Case comEvReceive
-
Case comEvSend
-
Case comEvEOF
-
End Select
-
' Store fetched data in a variable
-
strRTL = MSComm1.Input
-
txtDataLog.Text = strRTL
-
-
End Sub
-
There are two tabs in the form (see attachment) where the 'Comm port properties' tab is for the setting and the 'Data logger' tab is for data display. I planned to develop a hyperterminal-like program that can receive and display the transmitted data from a tide gauge device. But somehow my codes don't work. I don't have idea to fix the code so that the data can be received and displayed in a text box (txtDataLog) on data logger tab. I tried using Hyperterminal to see whether the device itself not transmitting the data, but the data are received and displayed accordingly in the program window. Please anyone can tell me what is missing in my codes...
Thank YOU in advance.
9 3665
I have no experience in COM ports but here are some questions:
- What device is it ? company, type, ...
- Do you have documentation on how to connect the device and communicate with it?
- can you contact someone of that company who can give you advice on the protocols for establishing a communication?
Maybe these are stupid remarks but:
- why are the settings like Bit-rate and parity in a Combobox?
If this is a program for only one device, then there is no need for multiple selection of a parameter.
Or is this a program for different types of devices?
PS:
Just a suggestion:
When you have to test a program with trails and errors,
keep the code as simple a possible.
Just use one button and write the code directly in the variable like:
MSComm1.Settings = "9600,n,8,1"
If this works, you know you are on the right track and then you can expand your code with features like comboboxes with multiple possibilities.
Thank you for the reply. The device is for measuring the water level, called as Portable Tide Gauge, Valeport Model 780. I wrote the coding for serial communication referred to internet search and previous papers related to it. I once read that this MSComm can receive any device signal as long as the parameters are the same. I use combo boxes for all the parameters since the program may receive data from other tide gauge devices with different model or brand from the one I am using for testing. I have tried using MSComm1.Settings = "4800,n,8,1" (same parameters setting with the device) but still, I get the same result; error MsgBox of No_Receive appears as I click OK. Is the line position of On Error GoTo No_Receive has something to do with this? Normally where do you put the error debugging line?
Is it possible to attach the Form in Bytes so we can work on the same code ?
Attached is the form for the serial port communication. Thanks for your time.
I have some questions:
Q1 - You set comboboxes with predefined values.
Why must the user enter a value? - With cboBRate
-
.Text = 9600
-
.AddItem "110"
-
.AddItem "300"
-
.AddItem "600"
-
.....
-
Private Sub cboBRate_KeyPress(KeyAscii As Integer)
-
On Error Resume Next
-
If KeyAscii = 8 Or KeyAscii = 48 Or KeyAscii = 49 Or KeyAscii = 51 Or _
-
KeyAscii = 52 Or KeyAscii = 53 Or KeyAscii = 54 Or KeyAscii = 55 Or _
-
KeyAscii = 56 Or KeyAscii = 57 Then
-
MsgBox "Please enter number only!", vbExclamation
Q2 - why deactivating the "Apply" button?
Even if the values of the comboboxes are set to a default value for a communication (can be saved to a "Settings.ini" file) then the user has to click on a combobox to activate the button.
Q3 - "KeyAscii = 8" is nothing !
Q4 - there is a difference between Keycode and keyAscii !
keycode is a code like = vbKeyLButton, ...
KeyAscii is the ASCII value of a ASCII charset like 40="(" and 41=")", ...
Q5 - The select case has no meaning - Select Case MSComm1.CommEvent
-
Case comBreak
-
Case comEventFrame
-
Case comEventOverrun
-
Case comEventRxOver
-
Case comEventRxParity...
-
PS:
To make a communication with the port You have to initialise it like the example in the VB help= - ' Buffer to hold input string
-
Dim Instring As String
-
' Use COM1.
-
MSComm1.CommPort = 1
-
' 9600 baud, no parity, 8 data, and 1 stop bit.
-
MSComm1.Settings = "9600,N,8,1"
-
' Tell the control to read entire buffer when Input
-
' is used.
-
MSComm1.InputLen = 0
-
' Open the port.
-
MSComm1.PortOpen = True
-
' Send the attention command to the modem.
-
MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
-
' the modem responds with "OK".
-
' Wait for data to come back to the serial port.
-
Do
-
DoEvents
-
Buffer$ = Buffer$ & MSComm1.Input
-
Loop Until InStr(Buffer$, "OK" & vbCRLF)
-
' Read the "OK" response data in the serial port.
-
' Close the serial port.
-
MSComm1.PortOpen = False
I don't think it's a good idea to put values for the comboboxes in the code because when you want to add or change a value, you have to open the code and compile it again.
Also you must upgrade each PC with that program.
It's better to fill the comboboxes with there values from an extern file like "settings.ini" or something like that.
It gives also the user the possibility to change values or delete or add values.
It's also more user friendly if you set the most used values as default values in an file which is loaded at startup of the program.
I have visited the site of Valeport "http://valeport.co.uk/Support/DownloadsandFAQs.aspx" but can't find the "Model 780" ! is this correct?
On the site you can find software and manuals for the products:
################################
Product Category FAQs Software Download Manuals / Documentation Download
Tide Gauges General Tide Gauge FAQs
TideMaster
Model 740
MIDAS WLR
Model 710
Model 730T
Pressure Sensor
TideMaster
Model 740
MIDAS WLR
Model 710
Model 730T
TideMaster
Model 740
MIDAS WLR (Hardware)
MIDAS WLR (Software)
Model 710 (Hardware)
Model 710 (Software)
Model 730T
MiniTide (Hardware)
MiniTide (Software)
####################################
Sorry, my mistake. It is Model 740. Portable Tide Gauge Model 740.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rotem925 |
last post by:
Im using socket object, and im sending data from first socket to the other,
How can i know when the other socket has recevied my Data ?
Thanx,
--
Rotem
Software & Management Engineer
|
by: Jesper Stocholm |
last post by:
I am using the example as the basis for an application I am
currently writing. The setup consists of two applications (client and
server) that talk to each other thru TCP.
I have no problems...
|
by: Alexander Arlievsky |
last post by:
We are developing an application that involves a PCI driver to a DSP card
that generates data at high rates (over 40 interrupts/sec, about 3MB/sec).
We use WinDriver for the driver (no kernel...
|
by: CHRISM |
last post by:
Hiya,
Been trying to figure this one out. I've written a very simple server
in VB.NET that listens for client connections, then spits out a bunch
of text and then closes the connection. Much...
|
by: Winger |
last post by:
Hi,
I'm having a weird problem... Part of code first:
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Dim DataChunk as String
Dim FullData as String
Do While EOF = False
If...
|
by: Uma - Chellasoft |
last post by:
Hai,
I am new to VB.Net programming, directly doing socket programming. In C, I
will be able to map the message arrived in a socket directly to a structure.
Is this possible in VB.Net. Can...
|
by: Jimmy611 |
last post by:
Help me out on storing the data that i receive on tcp/ip port. Its multithreaded and the data is supposed to come from GSM device. I want to store the data in the db(sql 2k) as soon as it is...
|
by: Lou |
last post by:
I have a class that uses the serial port class
Private SerialPort as New SerialPort
When I receive the asyncronous serial port response it appears that data is
on a different thread than my...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |