Link to home
Start Free TrialLog in
Avatar of T0masz
T0masz

asked on

SerialNG read a line

Im trying to get SerialNG to read until the end of line any examples would be great.
Thanks in advice

Edit: Here is the link http://www.domis.de/cms/index.php?module=ContentExpress&func=display&ceid=8
Tom
Avatar of shaneholmes
shaneholmes

Never heard of it, you may want to post code related to what youa re doing, to see if we can suggest a fix that way

Shane
Avatar of T0masz

ASKER

Im writing a service that would read a line from a serial port phrase the line, then write it to a file/database.

Tom
Well, for those of us who have never heard of,  let alone used,  such control, we will need to see your attempt at doing it. DO you have any code where you tried it yourself.  We have no idea what the Properties, Methods, or Events of the control are..... Im assuming it has some basic functionality like most serial components though...

Shane
Avatar of T0masz

ASKER

The SerialNG has 2 ways of getting data, onclusterrx or oncharrx, the cluster doesnt really work for me since it stops reading the cluster after certain amount of data,time. So I figured I would use onrxcharevent and read every character add it onto a string until \n then phrase the string append to a file.

Tom
You still haven't posted any of the code on your attempt....can we see it...

Shane
Avatar of T0masz

ASKER

procedure TForm1.Timer1Timer(Sender: TObject);
var i : Integer;
begin
 SerialPortNG1.Active := True;
 if not SerialPortNG1.Active then
    begin
      memoResult.Lines.Add('Error: Port could not be opened');
      Exit;
    end;
  if SerialPortNG1.NextClusterSize >= 0 then
        memoResult.Lines.Add(SerialPortNG1.ReadNextClusterAsString);
  end;

For now this reads the cluster every second, I have no clue what it treats as a cluster tho...

Tom

I'll suggest You reading from serial port without any component.
It's really easy.
Several lines of code or so and You've got full control of what You are doing.

Avatar of T0masz

ASKER

anyone have an idea how to seperate the cluster into strings? I was thinking about separating them by \n or something but im still working on it.

Tom
Hello,

I would suggest use event if possible and not timer, buffer might get filled up in less than a second or maybe you might reduce it to 50ms. Seperating the cluster string(!) wouldnt be a problem if it has a repeating sequence for marking eoln. Although if you told us more about the input device and what sort of data you are trying to aquire, does it have linebreaks it might help, for instance i'm not much good with serial input, but is your hardware 8bit? if it is 4bit then the eoln code could be hidden inside the character and you would have to deal with it on bit level.

Kunfu Faresi
Avatar of T0masz

ASKER

its a 8bit phone call monitor, I tried using this function but couldnt get it to seperate by \n

--snip--
function split(input:string;schar:char;s:integer):string;
    var
       i,n:integer;
       schop: string;
    begin
       n := 1;
       for i := 1 to length(input) do
       begin
         if (input[i] = schar) then
         begin
           inc(n);
           if n = s then
           split := schop
           else
           schop := '';
           end
         else
           schop := schop + input[i];
         end;
    end;
--snip---

I can see some strange things in this function. Maybe I don't understand.

Could You write what was it supposed to do?
Avatar of T0masz

ASKER

it should returns the Nth string that matches the schar so lets say in a string "1 2 3" split(string," ",2); would return 2

Tom
maybe your break is chr(9) or something similar have you tried dumping it as ascii values from serial port  using something like
---snip--
var
 s,s2:String;
 i:integer;
begin
 s2 := 'abc';
 for i := 1 to length(s2) do
  s := s + inttostr( ord( s2[i] ) );
---snip--

where s2 is what you get from serial port? there are some characters that would look like a space in some controls but actually has some other like value chr(9) TAB.

Kunfu Faresi
1.
function split(input:string;schar:char;s:integer):string;
    var
       i,n:integer;
       schop: string;
    begin
       n := 1;
       for i := 1 to length(input) do
       begin
         if (input[i] = schar) then
         begin
           inc(n);
           if n = s then
           begin
           split := schop;
           break;              // no need to check further
           end
           else
           schop := '';
           end
         else
           schop := schop + input[i];
         end;
// to allow return 3 from your example
        if n=s-1 then
         split := schop;
    end;

2. You wanted to have schar=\n ?
   But what do you mean by \n #10, #13 or #10#13
Avatar of T0masz

ASKER

Sorry, I was on a one day trip, couldnt respond, I have no clue what the answer to #2 is... any way to check? I hooked up a cisco router to my serial port and it reads everything just fine,  but now I have to split the clusters by newline chars then for every line phrase and save it, and with the rest of the string ( after \n ) I would have to store it, and then reattach it the next time the cluster event happends. And I still cant get it to split by the newline char.

Tom
I use serial ports for all sorts of things - and have found SerialNG at the difficult end.

I use AsyncFree without any difficulties, - with that you can build up whatever strings you like as they appear at the port, then any string parser would do - even Delphi's inbuilt one.

Newline characters (\n in C) have two meanings depending on platform - Windows and Unix. In essence it is not a character but a sequence depending on platform.

One interprets '\n' as
 Delphi                #13, the other as #13#10
 Hex                    h0D        -           h0D0A,
 Ascii symbology   CR          -           CR,LF
, though which requires what I am unsure. One platform cares, the other does not (as it automatically puts in the LF, Linefeed, #10, h0A)  

 
Avatar of T0masz

ASKER

I tried splitting the lines but Its all messed up, can anyone post a working example ( using any component ) that would just read the line and display/write it somewhere. I would really appreciated, since I have no clue how to do it myself using SerialNG...
Tom
Avatar of T0masz

ASKER

uciacomport did the trick, great component.

Tom
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial