reading data using serial port

Reply

Join Date: Apr 2006
Posts: 36
Reputation: Shefali is an unknown quantity at this point 
Solved Threads: 0
Shefali Shefali is offline Offline
Light Poster

reading data using serial port

 
0
  #1
Oct 3rd, 2006
hello
i need to read and store data from a device through serial port. i have searched the forum but havent found something which is really helping me. can anyone give me some resources or sample codes where i can figure out how to do it?
thanks in advance.
regards shefali.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 32
Reputation: Marikanna is an unknown quantity at this point 
Solved Threads: 1
Marikanna Marikanna is offline Offline
Light Poster

Re: reading data using serial port

 
0
  #2
Oct 5th, 2006
Shefali,

Once you connect try to send some hex data and check whether u recieve any data.

Connection eg:
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True


Public Sub SendHexData()
On Error Resume Next

Dim bytes(5) As Byte

bytes(0) = "&H" & 2
bytes(1) = "&H" & 20
bytes(2) = "&H" & 30
bytes(3) = "&H" & 33
bytes(4) = "&H" & 3
bytes(5) = "&H" & 20

MSComm1.Output = bytes


End Sub


Private Sub MSComm1_OnComm()

On Error Resume Next

If MSComm1.CommEvent = comEvReceive Then
msgbox MSComm1.Input
End If

End Sub

If you want to monitor the port download the following or a one similar to that so that you can see what hex data is sent back from the device.

http://www.hhdsoftware.com/Family/serial-monitor.html

Hope it helps
Marikanna
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 54
Reputation: royaloba is an unknown quantity at this point 
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Re: reading data using serial port

 
0
  #3
Oct 16th, 2006
Marikanna,
how about reading or sending file thru LAN port...
thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 8
Reputation: sugarboy rider is an unknown quantity at this point 
Solved Threads: 0
sugarboy rider sugarboy rider is offline Offline
Newbie Poster

Re: reading data using serial port

 
0
  #4
Oct 17th, 2006
Marikanna,

I have to say that you help me a lot starting this kind of communication but you are not completely right.
Writing bytes(N) = "&H" & XX you aren't able to introduce characters from A to F, you should do it in a very similar way : bytes(N) = &HXX and you'll succes.
Once again thanks marikanna'cos this evolution is from your original code.

Sugarboy rider
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: udaywali is an unknown quantity at this point 
Solved Threads: 0
udaywali udaywali is offline Offline
Newbie Poster

Re: reading data using serial port

 
0
  #5
Oct 18th, 2006
Shefali,

Here is some code (live copy) from a small RS232 tester I use. To use it, create a form with all controls used in this code and associate the sub wit a command button. If you have problems, I can send the full app.

[code]
Private Sub Command1_Click()

On Error Resume Next
MousePointer = vbHourglass
MSComm1.CommPort = Val(txtPortNumber)
If Err > 0 Then
MsgBox Err.Description & vbCrLf & "Try Again"
MSComm1.PortOpen = False
Exit Sub
End If

If Check1.Value = vbChecked Then
MSComm1.Handshaking = comRTS
MSComm1.RTSEnable = True
Else
MSComm1.Handshaking = comXOnXoff
MSComm1.RTSEnable = False
End If

MSComm1.Settings = txtPortSetting
If MSComm1.PortOpen Then
MSComm1.PortOpen = False
End If
MSComm1.PortOpen = True
If Err > 0 Then
MsgBox Err.Description & vbCrLf & "Try Again"
MSComm1.PortOpen = False
Err.Clear
MousePointer = vbDefault
'txtRecieved = ""
Exit Sub
End If

Dim s As String
Dim i As Integer

s = txtOutString
txtSent = ""
Do While s <> ""
If Left(s, 1) = "%" Then
i = Mid(s, 2, 3)
MSComm1.Output = Chr(i)
txtSent = txtSent & "<" & CStr(i) & ">"
s = Right(s, Len(s) - 4)
Else
MSComm1.Output = Left(s, 1)
txtSent = txtSent & Left(s, 1)
s = Right(s, Len(s) - 1)
End If
DoEvents
Loop

Sleep Val(txtActivationDelay)

s = ""
i = 0
Do While s = ""
i = i + 1
If i > 100 Then
txtReceived = ""
Exit Do
End If
s = MSComm1.Input
DoEvents
txtReceived = CStr(i)
Loop

Sleep Val(txtTransmissionDelay)
DoEvents
s = s & MSComm1.Input

Sleep Val(txtTransmissionDelay)
s = s & MSComm1.Input

txtReceived = ""
Do While s <> ""

i = Asc(Left(s, 1))
If Len(s) > 1 Then
s = Mid(s, 2)
Else
s = ""
End If
If i >= Asc(" ") And i < Asc("Z") Then
txtReceived = txtReceived & Chr(i)
Else
txtReceived = txtReceived & "0x"
txtReceived = txtReceived & Chr(&H30 + (i / 16))
txtReceived = txtReceived & Chr(&H30 + (i Mod 16))
End If

Loop

'txtRecieved = Format(CLng(s) / 1000, "00.000")

MSComm1.PortOpen = False
MousePointer = vbDefault

End Sub
[code]
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 36
Reputation: Shefali is an unknown quantity at this point 
Solved Threads: 0
Shefali Shefali is offline Offline
Light Poster

Re: reading data using serial port

 
0
  #6
Nov 23rd, 2006
thank u all for pouring in the suggestions and codes. that was real helpful. i am now able to get data from the serial port and also can save it. my ultimate goal is to plot the data i m getting from the port as it is recieved. any suggestions/sample codes about how i can do it.
any help would be greatly appreciated.
thanks very much.
shefali.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: udaywali is an unknown quantity at this point 
Solved Threads: 0
udaywali udaywali is offline Offline
Newbie Poster

Re: reading data using serial port

 
0
  #7
Nov 26th, 2006
Use a timer to poll the serial port. Read the port and if any data is available, you can process the data. Or else, just wait till you get data (on next timer interrupt)

If you have not used timer before, try this-

create a new project. On the form, add timer control. select and press F4 to get properties. Enable the timer. Set interval to 1000.
Double click on timer. In the event call back, write some code like printing a message or time of day.

Experiment. Get back if you need help.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 36
Reputation: Shefali is an unknown quantity at this point 
Solved Threads: 0
Shefali Shefali is offline Offline
Light Poster

Re: reading data using serial port

 
0
  #8
Nov 26th, 2006
thanks udaywali for ur response. in my code i have used timer to poll the port. i know how to display the data in a text box and to save it in a file. what i need is to know how to plot the data (i mean charting the data graphically, say in x axis time and in y axis the data).
the data i m getting from the device is 4 different fields seperated by a comma. i need to plot one of that field with respect to time.
any thoughts how i can do it?
thanks again
regards.
shefali.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 14
Reputation: udaywali is an unknown quantity at this point 
Solved Threads: 0
udaywali udaywali is offline Offline
Newbie Poster

Re: reading data using serial port

 
0
  #9
Nov 28th, 2006
Shefali,

Create a picture box on the form you are using. We will call it pic1.
Scale the picture box to the required coordinates. Say, your data has a range of -100 to 100, and you wish to display next 1000 readings, you can initialize the picture box in the form load event.

Also, place a command button named cmdRefresh. We will call the com control as comm1.

As you get more readings, you may simply change the x-axis scale. I am sure you will get it.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. 'declarations
  3. private count as integer
  4. private lastValue as single
  5.  
  6.  
  7. 'event call backs
  8. public sub form_load()
  9.  
  10. pic1.autoredraw = true 'you can set it in properties as well
  11. pic1.scale (0,100)-(1000,-100)
  12. drawGrid
  13.  
  14. 'rest of initialization code, like opening com port etc. can go here
  15.  
  16. end sub
  17.  
  18. public sub timer1_timer()
  19.  
  20.  
  21. dim i as integer
  22.  
  23. i = val(comm1.input)
  24.  
  25. 'if you need a vertical bar use this
  26. pic1.line (count, 0)-(count, i)
  27.  
  28. 'if you need to draw line graph, use this
  29. pic1.line (count, lastvalue)-(count+1, i)
  30. lastvalue = i
  31. count = count + 1
  32.  
  33. end sub
  34.  
  35. private sub cmdRefresh_click()
  36.  
  37. pic1.cls
  38. count = 0
  39. 'no need to reset the last value.
  40.  
  41. end sub
  42.  
  43. private sub drawGrid()
  44.  
  45. dim i as integer
  46.  
  47. for i = 1 to 1000 step 100
  48. pic1.line (i, 100)-(i, -100)
  49. next
  50. for i = -100 to 100 step 25
  51. pic1.line (0, i)-(1000, i)
  52. next
  53.  
  54. end sub

Try this code. By the way, what is the end use of the program?

Regards


Uday
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 36
Reputation: Shefali is an unknown quantity at this point 
Solved Threads: 0
Shefali Shefali is offline Offline
Light Poster

Re: reading data using serial port

 
0
  #10
Nov 28th, 2006
thanks again for ur response. the end use of the code is to get data from a device, plot the data as it arrives in the serial port, and once all the data has arrived some post processing has to be done. i am already done with the post processing thing... what i m struggling with now is the real time plotting of the data. the data which i m getting from the serial port looks like this:

8.25,0,13.1,12,8
8.25,5,13.1,12,9
8.25,10,13.1,12,4
8.25,14,14.1,12,6
8.25,22,15.1,12,8
and so on

as u can see there are 5 diff data fields in each line seperated by comma. now i want to plot the second field with respect to time. the code u gave i believe will work fine if the port is sending out a single value at a time. but here its 5 values out of which only 1 needs to be plotted. and this needs to be done in realtime.. i mean as the data is arriving.
i m really stuck now..wud greatly appreciate ur help.
thanks a lot.
regards.
shefali.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC