![]() |
| ||
| reading data using serial port 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. |
| ||
| Re: reading data using serial port 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 |
| ||
| Re: reading data using serial port Marikanna, how about reading or sending file thru LAN port... thanks |
| ||
| Re: reading data using serial port 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 |
| ||
| Re: reading data using serial port 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] |
| ||
| Re: reading data using serial port 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. |
| ||
| Re: reading data using serial port 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. |
| ||
| Re: reading data using serial port 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. |
| ||
| Re: reading data using serial port 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 |
| ||
| Re: reading data using serial port 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. |
| All times are GMT -4. The time now is 3:50 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC