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.
Shefali 0 Light Poster
Marikanna 5 Light Poster
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
royaloba 0 Junior Poster in Training
Marikanna,
how about reading or sending file thru LAN port...
thanks
sugarboy rider 0 Newbie Poster
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
udaywali 0 Newbie Poster
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.
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
Edited by Dani because: Formatting fixed
Shefali 0 Light Poster
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.
udaywali 0 Newbie Poster
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.
Shefali 0 Light Poster
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.
udaywali 0 Newbie Poster
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.
'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
Shefali 0 Light Poster
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.
udaywali 0 Newbie Poster
Shefali
Your data has a neat pattern and VB is just one of the best languages to handle that kind of stuff
say you have read one line of input
dim s as string
dim line as string
dim i as integer
dim p2 as string
dim v as integer
s = ""
s = s & comm1.input
do while (1)
'get one line
do while instr(s, chr(10)) > 0
'get one line of data
line = left(s, instr(s, chr(10)) -1)
s = mid(s, len(s1)+1) 'remaing part of input
'get first comma
i = instr(s, ",")
p2 = instr(i+1, s, ",")
v = val(p2)
addPoint2Plot v
loop
'append comm1 input to any remaining trailing chars of last input
s = s & comm1.input
'add some kind of exit code here.
loop
I assume addPoint2Plot will plot the data as I has explained in previous example.
Am I clear?
Uday Wali
Shefali 0 Light Poster
hello again
udaywali thanks so much for ur help. ur code has been very helpful for me. i have used the split function to get the particular value of a string. but i used ur code for plotting the stuff and it worked nicely. thanks so much for showing me the way.
a small question.. will it be possible to give titles to the axis? i mean as i get more data the axis shud also get changed accordingly. is that possible? can u give me any code samples?
and one more thing i wanted to ask u is.. as of now the graph needs to refreshed... is it not possible that it automatically shifts towards left and the axis and the grid re-adjust automatically. something like the task-manager (performance) graph in windows.
thanks a lot again
regards.
shefali.
udaywali 0 Newbie Poster
Shefali,
Writing text on X axis is very simple.
picture1.currentx = 500
picture1.currenty = 10
picture1.print "1000"
will print on the x axis. Trick is writing on the y axis because you need to rotate text. If you need help on that, let me know. I have some ready code, I can share with all. It is big, so i have not included here.
Scrolling is also very difficult and slow in VB. You should have a structure to store the values in say, a collection. Collections maintain data in a linked list, so deleting and adding is easy but it is some what slow. Create a class with two values, one for x and one for y. You could use CPoint for example.
'class module CPoint
'declarations section
Public x as single
public y as single
Create a collection in your code to store instances of this class.
Public sub Timer_time()
static dataPts as collection
static cnt as integer
private p as CPoint
if dataPts is nothing then
'on the first call, create a new collection
set dataPts = new collection
cnt = 0
end if
cnt = cnt + 1
set p = New CPoint
p.x = cnt
p.y = ReadPort()
dataPts.add p
if dataPts.count > 1000
dataPts.remove 1
endif
'you have a max of 1000 points in a collection,
'which you may now display
DisplayValues dataPts
End sub
Private sub DisplayValues(c as collection)
dim i as integer
dim x0 as integer 'x value at left of graph
if c.count > 0 then
x0 = c(1).x
endif
'clear the screen and display grid
picture1.cls
drawGrid
'draw set of lines correspoinding to x and y values in the collection
for i = 1 to c.count -1
picture1.line (c(i).x - x0, c(i).y)-(c(i+1).x - x0, c(i+1).y)
next
end sub
I have written it quickly without much thought to clarity of idea. Because you know my earlier message, I guess you can guess my thoughts.
If it is not clear, let me know again.
Regards
Uday
Bisaye 0 Newbie Poster
I have a kind of problem on filtering data´s which were recorded through serial port. My question is I would like to know if I can take a selected datas to plot a real time graph. The datas are quite a lot of parameters for a time being I want to plot nine parameters. If you want the data format is like this
‹...‚.10D20®B...‚B01DA,20,2,L,49.98,11.96,3.35,2.35,2.55,890,264,93,1205.006,860,P\..
.‚.20D20¨G...‚B02DA,20,2,L,50.06,11.96,3.30,2.45,2.65,865,255,93,1205.008,928,P.ñ..
.‚.10D30«D...‚?01DA,30,2,S,P,L,P,S,P,M,P,P,R,N,N,D,E,S,80010.,99.999,0.08333¾Ÿ..
.‚.20D30¥I...‚?02DA,30,2,L,P,L,P,N,P,M,P,P,R,N,N,D,E,S,1457.6,99.994,0.08333.W..
.‚.10D31©E...‚101DA,31,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,M,0,1¥:...‚.20D31£J..
.‚102DA,31,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,P,M,0,1vh...‚.10D32§F..
.‚P01DA,32,0,1.00,15,85,31,27,2¬C...
from these datas I only want to read the type of values B01DA,20,2,L,49.98,11.96,3.35,2.35,2.55,890,264,93,1205.006,860, and
B02DA,20,2,L,50.06,11.96,3.30,2.45,2.65,865,255,93,1205.008,928
for information these datas are sequential and they start after three dots and a coma as you can see so I want to select the above ones only. Any information would be greatly appreciated
itserial 0 Newbie Poster
i have creaded a vb serial port test for a 14 port serial pc board.
i am having problems with one port only. the seventh port. i get the 8015, error code. it seems that i can not open the port at all. the documentation states that it is a rs232 port. the port is used on a printer. all the other ports work with the port test. please help!
vb5prgrmr 143 Posting Virtuoso
Ahhhh!!! A necrophiliac!!!! It plays with the dead threads!!!
Please, in the furture, if you need to reference and old thread, copy it's URL into your new thread...
As for your question, it is time to use your friends (yahoo, google, ask, answers, bing) and search for vb6 rs232. You will find many examples out there.
Good Luck
mariospelekis 0 Newbie Poster
hello vb net, i am searching in some kind of code tha is able to read from serial port an incoming Hex line from a device which send its order at Hex system.My problem is that i can receive for the time only the first byte and then vb stop understand the next one.The on desk device i have must sent 3 byte per time:1)the hex recognition of the devise 2)one action in hex 3)one number in hex.Thanks in advance
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.