| | |
reading data using serial port
![]() |
•
•
Join Date: Aug 2005
Posts: 32
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Sep 2006
Posts: 8
Reputation:
Solved Threads: 0
Marikanna,
I have to say that you help me a lot starting this kind of communication but you are not completely right.
Writing
Once again thanks marikanna'cos this evolution is from your original code.
Sugarboy rider
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
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
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]
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]
•
•
Join Date: Apr 2006
Posts: 36
Reputation:
Solved Threads: 0
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.
any help would be greatly appreciated.
thanks very much.
shefali.
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2006
Posts: 36
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
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.
Try this code. By the way, what is the end use of the program?
Regards
Uday
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)
'declarations private count as integer private lastValue as single 'event call backs public sub form_load() pic1.autoredraw = true 'you can set it in properties as well pic1.scale (0,100)-(1000,-100) drawGrid 'rest of initialization code, like opening com port etc. can go here end sub public sub timer1_timer() dim i as integer i = val(comm1.input) 'if you need a vertical bar use this pic1.line (count, 0)-(count, i) 'if you need to draw line graph, use this pic1.line (count, lastvalue)-(count+1, i) lastvalue = i count = count + 1 end sub private sub cmdRefresh_click() pic1.cls count = 0 'no need to reset the last value. end sub private sub drawGrid() dim i as integer for i = 1 to 1000 step 100 pic1.line (i, 100)-(i, -100) next for i = -100 to 100 step 25 pic1.line (0, i)-(1000, i) next end sub
Try this code. By the way, what is the end use of the program?
Regards
Uday
•
•
Join Date: Apr 2006
Posts: 36
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Send data on a serial port (C++)
- How to copy data from serial port to notepad (Visual Basic 4 / 5 / 6)
- Problem with real-time app reading from serial port (ASP.NET)
- Serial Port (C++)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Vbcombobox problem...........
- Next Thread: Can you help me with visual basic code?
| Thread Tools | Search this Thread |
* 6 2007 access activex add age basic beginner birth bmp calculator cd cells.find click client code college connection connectionproblemusingvb6usingoledb creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit excel excelmacro file filename form header iamthwee image inboxinvb internetfiledownload listbox listview liveperson login looping microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading remotesqlserverdatabase report save search sendbyte sites sql sql2008 sqlserver subroutine tags time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web windows





