473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MSComm: data not received

Mas Juliza Alias
I have prepared the below coding to receive data from rs232 serial port.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim Port As String
  4. Dim BRate As String
  5. Dim DBits As String
  6. Dim Parity As String
  7. Dim SBits As String
  8. Dim Flow As String
  9.  
  10. Private Sub cboBRate_Change()
  11.     Let cmdApply.Enabled = True
  12. End Sub
  13.  
  14. Private Sub cboBRate_Click()
  15.     Let cmdApply.Enabled = True
  16. End Sub
  17.  
  18. Private Sub cboBRate_KeyDown(KeyCode As Integer, Shift As Integer)
  19.     On Error Resume Next
  20.     If KeyCode = 38 Then
  21.         Let cboPort.SelStart = 0
  22.         cboPort.SetFocus
  23.     ElseIf KeyCode = 40 Then
  24.         Let cboDBits.SelStart = 0
  25.         cboDBits.SetFocus
  26.     End If
  27. End Sub
  28.  
  29. Private Sub cboBRate_KeyPress(KeyAscii As Integer)
  30.     On Error Resume Next
  31.     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
  32.         MsgBox "Please enter number only!", vbExclamation
  33.         Let cboBRate.SelStart = 0
  34.         Let cboBRate.SelLength = 20
  35.         cboBRate.SetFocus
  36.     End If
  37. End Sub
  38.  
  39. Private Sub cboDBits_Change()
  40.     Let cmdApply.Enabled = True
  41. End Sub
  42.  
  43. Private Sub cboDBits_Click()
  44.     Let cmdApply.Enabled = True
  45. End Sub
  46.  
  47. Private Sub cboDBits_KeyDown(KeyCode As Integer, Shift As Integer)
  48.     On Error Resume Next
  49.     If KeyCode = 38 Then
  50.         Let cboDBits.SelStart = 0
  51.         cboDBits.SetFocus
  52.     ElseIf KeyCode = 40 Then
  53.         Let cboParity.SelStart = 0
  54.         cboParity.SetFocus
  55.     End If
  56. End Sub
  57.  
  58. Private Sub cboDBits_KeyPress(KeyAscii As Integer)
  59.     On Error Resume Next
  60.     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
  61.         MsgBox "Please enter number only!", vbExclamation
  62.         Let cboDBits.SelStart = 0
  63.         Let cboDBits.SelLength = 20
  64.         cboDBits.SetFocus
  65.     End If
  66. End Sub
  67.  
  68. Private Sub cboParity_Change()
  69.     Let cmdApply.Enabled = True
  70. End Sub
  71.  
  72. Private Sub cboParity_Click()
  73.     Let cmdApply.Enabled = True
  74. End Sub
  75.  
  76. Private Sub cboParity_KeyDown(KeyCode As Integer, Shift As Integer)
  77.     On Error Resume Next
  78.     If KeyCode = 38 Then
  79.         Let cboDBits.SelStart = 0
  80.         cboDBits.SetFocus
  81.     ElseIf KeyCode = 40 Then
  82.         Let cboSBits.SelStart = 0
  83.         cboSBits.SetFocus
  84.     End If
  85. End Sub
  86.  
  87. Private Sub cboPort_Change()
  88.     Let cmdApply.Enabled = True
  89. End Sub
  90.  
  91. Private Sub cboPort_Click()
  92.     Let cmdApply.Enabled = True
  93. End Sub
  94.  
  95. Private Sub cboPort_KeyDown(KeyCode As Integer, Shift As Integer)
  96.     On Error Resume Next
  97.     If KeyCode = 40 Then
  98.         Let cboBRate.SelStart = 0
  99.         cboBRate.SetFocus
  100.     End If
  101. End Sub
  102.  
  103. Private Sub cboSBits_Change()
  104.     Let cmdApply.Enabled = True
  105. End Sub
  106.  
  107. Private Sub cboSBits_Click()
  108.     Let cmdApply.Enabled = True
  109. End Sub
  110.  
  111. Private Sub cboSBits_KeyDown(KeyCode As Integer, Shift As Integer)
  112.     On Error Resume Next
  113.     If KeyCode = 38 Then
  114.         Let cboParity.SelStart = 0
  115.         cboParity.SetFocus
  116.     End If
  117. End Sub
  118.  
  119. Private Sub cboSBits_KeyPress(KeyAscii As Integer)
  120.     On Error Resume Next
  121.     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
  122.         MsgBox "Please enter number only!", vbExclamation
  123.         Let cboSBits.SelStart = 0
  124.         Let cboSBits.SelLength = 20
  125.         cboSBits.SetFocus
  126.     End If
  127. End Sub
  128.  
  129. Private Sub cmdApply_Click()
  130.     On Error Resume Next
  131.     If MSComm1.PortOpen Then
  132.         MSComm1.PortOpen = Not MSComm1.PortOpen
  133.         MSComm1.CommPort = Val(Mid(cboPort.Text, 4, 1))
  134.         MSComm1.Settings = Val(cboBRate.Text) & "," & Left(cboParity.Text, 1) & "," & Val(cboDBits.Text) & "," & Val(cboSBits.Text)
  135.         With cboFControl
  136.             If .Text = "None" Then
  137.                 MSComm1.Handshaking = comNone
  138.             ElseIf .Text = "Xon / Xoff" Then
  139.                 MSComm1.Handshaking = comXOnXoff
  140.             ElseIf .Text = "RTS" Then
  141.                 MSComm1.Handshaking = comRTS
  142.             ElseIf .Text = "RTS Xon / Xoff" Then
  143.                 MSComm1.Handshaking = comRTSXOnXOff
  144.             End If
  145.         End With
  146.     End If
  147.     Let cmdApply.Enabled = False
  148.     cmdOK.SetFocus
  149. End Sub
  150.  
  151. Private Sub cmdCancel_Click()
  152.     Let cboPort.Text = Port
  153.     Let cboBRate.Text = BRate
  154.     Let cboDBits.Text = DBits
  155.     Let cboParity.Text = Parity
  156.     Let cboSBits.Text = SBits
  157.     Let cboFControl.Text = Flow
  158.     Hide
  159. End Sub
  160.  
  161. Private Sub cmdOK_Click()
  162.     On Error GoTo No_Receive
  163.     If MSComm1.PortOpen Then
  164.         MSComm1.PortOpen = Not MSComm1.PortOpen
  165.         MSComm1.CommPort = Val(Mid(cboPort.Text, 4, 1))
  166.         MSComm1.Settings = Val(cboBRate.Text) & "," & Left(cboParity.Text, 1) & "," & Val(cboDBits.Text) & "," & Val(cboSBits.Text)
  167.         With cboFControl
  168.             If .Text = "None" Then
  169.                 MSComm1.Handshaking = comNone
  170.             ElseIf .Text = "Xon / Xoff" Then
  171.                 MSComm1.Handshaking = comXOnXoff
  172.             ElseIf .Text = "RTS" Then
  173.                 MSComm1.Handshaking = comRTS
  174.             ElseIf .Text = "RTS Xon / Xoff" Then
  175.                 MSComm1.Handshaking = comRTSXOnXOff
  176.             End If
  177.         End With
  178.     End If
  179. No_Receive:
  180.     MsgBox "Error receiving the data. Check your comm. port.", vbExclamation
  181. End Sub
  182.  
  183. Private Sub Form_Activate()
  184.     On Error Resume Next
  185.     Let cmdOK.Default = True
  186.     Let cmdApply.Enabled = False
  187.     Let Port = cboPort.Text
  188.     Let BRate = cboBRate.Text
  189.     Let DBits = cboDBits.Text
  190.     Let Parity = cboParity.Text
  191.     Let SBits = cboSBits.Text
  192.     Let Flow = cboFControl.Text
  193.     cboPort.SetFocus
  194. End Sub
  195.  
  196. Private Sub Form_Load()
  197.     Dim i As Integer, j As Integer, k As Double
  198.     Let Caption = "Real-time elevation via comm. port"
  199.     Let cmdCancel.Cancel = True
  200.     Let cboPort.Text = "COM1"
  201.     For i = 1 To 4
  202.         cboPort.AddItem "COM" & i
  203.     Next i
  204.     With cboBRate
  205.         .Text = 9600
  206.         .AddItem "110"
  207.         .AddItem "300"
  208.         .AddItem "600"
  209.         .AddItem "1200"
  210.         .AddItem "2400"
  211.         .AddItem "4800"
  212.         .AddItem "9600"
  213.         .AddItem "14400"
  214.         .AddItem "19200"
  215.         .AddItem "28800"
  216.         .AddItem "38400"
  217.         .AddItem "56000"
  218.         .AddItem "57600"
  219.         .AddItem "115200"
  220.         .AddItem "128000"
  221.         .AddItem "256000"
  222.     End With
  223.     Let cboDBits.Text = 8
  224.     For j = 4 To 8
  225.         cboDBits.AddItem j
  226.     Next j
  227.     With cboParity
  228.         .Text = "None"
  229.         .AddItem "Even"
  230.         .AddItem "Odd"
  231.         .AddItem "Mark"
  232.         .AddItem "Space"
  233.     End With
  234.     Let cboSBits.Text = 1
  235.     For k = 1 To 2 Step 0.5
  236.         cboSBits.AddItem k
  237.     Next k
  238.     With cboFControl
  239.         .Text = "None"
  240.         .AddItem "RTS"
  241.         .AddItem "RTS Xon / Xoff"
  242.         .AddItem "Xon / Xoff"
  243.     End With
  244.  
  245. End Sub
  246.  
  247.  
  248. Private Sub MSComm1_OnComm()
  249.     Dim strRTL As String
  250.     'If MSComm1.CommEvent = comEvReceive Then
  251.     '    RTData = MSComm1.Input
  252.     '    lstDataLog.List = RTData
  253.     'End If
  254.  
  255.     ' Returns communication event or error
  256.     Select Case MSComm1.CommEvent
  257.         Case comBreak
  258.         Case comEventFrame
  259.         Case comEventOverrun
  260.         Case comEventRxOver
  261.         Case comEventRxParity
  262.         Case comEventTxFull
  263.         Case comEventDCB
  264.  
  265.         Case comEvCD
  266.         Case comEvCTS
  267.         Case comEvDSR
  268.         Case comEvRing
  269.         Case comEvReceive
  270.         Case comEvSend
  271.         Case comEvEOF
  272.     End Select
  273.     ' Store fetched data in a variable
  274.     strRTL = MSComm1.Input
  275.     txtDataLog.Text = strRTL
  276.  
  277. End Sub
  278.  

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.
Attached Images
File Type: jpg frmRealTimeTide.jpg (59.5 KB, 1778 views)
Apr 21 '11 #1
9 3665
Guido Geurs
767 Expert 512MB
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?
Apr 21 '11 #2
Guido Geurs
767 Expert 512MB
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.
Apr 21 '11 #3
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?
Apr 22 '11 #4
Guido Geurs
767 Expert 512MB
Is it possible to attach the Form in Bytes so we can work on the same code ?
Apr 23 '11 #5
Attached is the form for the serial port communication. Thanks for your time.
Attached Files
File Type: zip frmCommPort.zip (1.9 KB, 135 views)
Apr 25 '11 #6
Guido Geurs
767 Expert 512MB
I have some questions:
Q1 - You set comboboxes with predefined values.
Why must the user enter a value?

Expand|Select|Wrap|Line Numbers
  1.     With cboBRate
  2.         .Text = 9600
  3.         .AddItem "110"
  4.         .AddItem "300"
  5.         .AddItem "600"
  6. .....
  7. Private Sub cboBRate_KeyPress(KeyAscii As Integer)
  8.     On Error Resume Next
  9.     If KeyAscii = 8 Or KeyAscii = 48 Or KeyAscii = 49 Or KeyAscii = 51 Or _
  10.                   KeyAscii = 52 Or KeyAscii = 53 Or KeyAscii = 54 Or KeyAscii = 55 Or _
  11.                   KeyAscii = 56 Or KeyAscii = 57 Then
  12.         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
Expand|Select|Wrap|Line Numbers
  1.     Select Case MSComm1.CommEvent
  2.         Case comBreak
  3.         Case comEventFrame
  4.         Case comEventOverrun
  5.         Case comEventRxOver
  6.         Case comEventRxParity...
  7.  
PS:
To make a communication with the port You have to initialise it like the example in the VB help=

Expand|Select|Wrap|Line Numbers
  1.    ' Buffer to hold input string
  2.    Dim Instring As String
  3.    ' Use COM1.
  4.    MSComm1.CommPort = 1
  5.    ' 9600 baud, no parity, 8 data, and 1 stop bit.
  6.    MSComm1.Settings = "9600,N,8,1"
  7.    ' Tell the control to read entire buffer when Input
  8.    ' is used.
  9.    MSComm1.InputLen = 0
  10.    ' Open the port.
  11.    MSComm1.PortOpen = True
  12.    ' Send the attention command to the modem.
  13.    MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that 
  14.    ' the modem responds with "OK".
  15.    ' Wait for data to come back to the serial port.
  16.    Do
  17.       DoEvents
  18.    Buffer$ = Buffer$ & MSComm1.Input
  19.    Loop Until InStr(Buffer$, "OK" & vbCRLF)
  20.    ' Read the "OK" response data in the serial port.
  21.    ' Close the serial port.
  22.    MSComm1.PortOpen = False
Apr 26 '11 #7
Guido Geurs
767 Expert 512MB
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.
Apr 27 '11 #8
Guido Geurs
767 Expert 512MB
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)
####################################
May 1 '11 #9
Sorry, my mistake. It is Model 740. Portable Tide Gauge Model 740.
May 2 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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
0
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...
4
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...
2
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...
4
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...
10
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...
0
Jimmy611
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.