?
Solved

Reading Real time data thru RS232 port!!

Posted on 2002-03-24
6
Medium Priority
?
17,391 Views
Last Modified: 2013-12-13


Hi

I have wriiten a program to read from a data file from disk and plot result. Now I want to obtain my data thru the serial port coming from a device that reads patient's heart data. The data comes every second and written to a file. I have to read it as it is comming from the device. But since I have to read the data in real time while my device reading the patient heart data and then plot it when the device stops (after 60 minutes max).
The problem I am facing is when the file is open by my medical device for writing then how can I read it?
Is there a way to do this please ?
All thoughts and suggestions are welcome.

PS: I don't have the interface between the medical device and the computer yet. It is going to be a card.

Thanks
0
Comment
Question by:nawaf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 6892269
Have you tried explicitly opening the file in read only mode in the plot program?
0
 
LVL 22

Expert Comment

by:cookre
ID: 6892277
Also, much depends on how the file is opened by the monitor program.  If you open it exclusive, don't.

If the monitor program opens it in buffered mode, then the plot ptogram won't always have up to the second data and will also have to deal with partial records.

Since you only sample once per second, you'll have plenty of time to flush the file after each write.

Which OS and language are you using?
0
 
LVL 2

Expert Comment

by:Ariel Garcia
ID: 6979670
Please>>

Specify lenguage for APPs

In you can get a file OCX for this propouse i dont get the address site but i can search....

answer this before i search...
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:nawaf
ID: 6980377
I am using fortran C.Thanks
0
 
LVL 22

Accepted Solution

by:
cookre earned 800 total points
ID: 6982440
For lack of anymore details, here's some sample code that use a normal stream to capture and save observations once per second.  Note the fflush() after each write.  I simulated the data fetch with rand().

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>

FILE  *DataFile;
int    ctr;
char   DateStr[9];
char   TimeStr[9];

int main(int argc, char* argv[])
{
DataFile=fopen("c:\\testy.dat","w+t");
if (DataFile==NULL)
   {
   printf("Can't open data file\n");
   return 1;
   }
srand((unsigned)time(NULL));
for (ctr=0; ctr<3600; ctr++)
    {
    _strdate(DateStr);
    _strtime(TimeStr);
    fprintf(DataFile,"Obs %03d on %s at %s = %010d\n",ctr+1,DateStr,TimeStr,rand());
    fflush(DataFile);
    Sleep(1000);
    }
fclose(DataFile);
return 0;
}


While that program is running, you can run this one any number of times to take a look at what has been captured and saved by the above.  Note that the data fie is opened 'read only' and that each time the program is run it appends to the listing file.

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>

FILE  *DataFile;
FILE  *ListFile;

char   DateStr[9];
char   TimeStr[9];
char   LineIn[256];


int main(int argc, char* argv[])
{

DataFile=fopen("c:\\testy.dat","rt");
if (DataFile==NULL)
   {
   printf("Can't open data file\n");
   return 1;
   }

ListFile=fopen("c:\\testy.txt","a+t");
if (ListFile==NULL)
   {
   printf("Can't open list file\n");
   return 1;
   }
_strdate(DateStr);
_strtime(TimeStr);
fprintf(ListFile,"Observations captured %s %s\n",DateStr,TimeStr);

while(NULL!=fgets(LineIn,256,DataFile))
     {
     fprintf(ListFile,"   %s\n",LineIn);
     }
fclose(ListFile);
fclose(DataFile);
return 0;
}
0
 

Expert Comment

by:antonyleung
ID: 7646556
I will use VB and a control mscomm32.ocx to implement your solution.  Since, I don't know the protocol of your device, I guess it just return some characters with control bits.

The mscomm32 object provides an OnComm event to handle all the signal transfer between PC and an input device.  As you see from the codes below, I will read the incoming signal into a buffer and scan through the buffer to remove all the unwanted control signal (you should refer to your hardware manual to find out what are they), then print it on a textbox for realtime monitoring and at the same time write it to a record file.

The codes follows:


Public PrepareLog as Boolean

Private Static Sub MSComm1_OnComm()
    Dim SignalString$  
    Select Case MSComm1.CommEvent
        ' Event messages.
        Case comEvReceive
            Dim buffer As Variant
            buffer = MSComm1.Input
           
         ' Eliminate transferring signal from screen.
            SignalString$ = Chr$(8) & Chr$(32) & Chr$(8)  'the unwnated control signal of the device
            For i = 1 To Len(buffer)
                If i = InStr(buffer, SignalString$) Then
                    buffer = Left$(buffer, i - 1) & Mid$(buffer, i + 3)
                End If
            Next
 
         'provide onscreen realtime signal respond on a textbox "txtTerminal"    
           txtTerminal.Text = txtTerminal.Text & buffer
           txtTerminal.SelStart = Len(txtTerminal.Text)

        'write the file to record the process
        PrintLog buffer, filelognum

           txtTerminal.SelStart = Len(txtTerminal.Text)
    End Select
End Sub

Public Sub PrintLog(PrintString As String, FileNo As Integer)
    If PrepareLog = False Then Form_Initialize
    Dim length As Long
    length = LOF(filenum)
    Put #FileNo, length + 1, PrintString
End Sub

Private Sub Form_Initialize()
start = Now
PrepareLog = True
    filelognum = FreeFile
    OpenLog
        PrintLog "This file record down all the comm-port activities that carried out by the host.", filelognum
        PrintLog "Written by Antony Leung.", filelognum
        PrintLog "********************************************************************************", filelognum
        PrintLog "Application:" + App.EXEName + ".exe", filelognum
        PrintLog "Working path:" + App.Path, filelognum
        PrintLog "Login Time:" + Format(start), filelognum
        PrintLog "", filelognum
End Sub

Public Sub OpenLog()
    Open App.Path & "\filename.extension" For Binary As #filelognum 'your record file goes here
End Sub
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Introduction to Processes

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question