comp.lang.ada
 help / color / mirror / Atom feed
* how to access a (serial) device (linux)
@ 2000-01-27  0:00 tilmanglotzner
  2000-01-27  0:00 ` David
  2000-01-27  0:00 ` Alfred Hilscher
  0 siblings, 2 replies; 3+ messages in thread
From: tilmanglotzner @ 2000-01-27  0:00 UTC (permalink / raw)


Hello,

I wrote a program to access serial port via the device file. One
task reads on the port, another task writes to the port. The
tasks somehow seem to block each other, and an antenna controller
connected to the serial port does not receive anything.

1) Is accessing the serial port via a device file the wrong approach ?
Is it better to do this by an interrupt service routine ? A sample
which I could reuse would be very, very helpful here :-)

2) Maybe the processes are in a dead lock because 2 processes try to
access the same file. Is it feasible to access a device file from 2
task  ?

3) Maybe it is better to access the device file from only 1 task.
I would need to open the file as read/write. Text_io seems not be
capable of this.Are there other packages which allow opening
(device) file as read/write ?

Thanks,

Tilman

=== Here the code ============================
with Text_IO;use Text_IO;
with System; use System;

package body Ser_read is
   package Int_Io is new Integer_IO (Integer); use Int_Io;


   task body Read is
      ReadPtr  : File_Type;

      Str      : String(1..100);
      Az       : String(1..20);
      El       : String(1..20);

      DeviceB  : String := "/dev/ttyS0";
      End_Flag : Boolean := False;
      I        : Integer;
      LineCount: Integer := 0;
      Wait     : Duration := 1.0;

   begin
      Put_Line ("[Read] : Started");
      while (End_Flag = False) loop
         select
            accept Init do
               Put_Line ("[Read] : Open " & DeviceB & " for read ...");
               Open (ReadPtr,In_File,DeviceB,"SHARED=NO");
               Put_Line ("[Read] : Opened for Read " & DeviceB);
            end Init;
         or
            accept Read_Orientation do
               Put_Line ("[Read] : Read_Orientation Signal received");
               Write.Send_Command("C2");
               Put_Line ("[Read] : Reading from " & DeviceB & ";");
               Get_Line(ReadPtr,Az,I);
               -- Get_Line(ReadPtr,El,I);
               Put_Line("Azimuth  : " & Az & ";");
               Put_Line("Elivation: " & El & ";");
            end Read_Orientation;
         or
            accept Stop do
               Put_Line ("[Read] : Stop Signal received");
               End_Flag := True;
            end Stop;
         end select;
      end loop;
      Put_Line ("[Read] : Close "  & DeviceB);
      Close (ReadPtr);
      Put_Line ("[Read] : Closed "  & DeviceB);
      Put_Line ("[Read] : Terminating task ...");
   End Read;

   task body Write is
      WritePtr : File_Type;
      DeviceA  : String  := "/dev/ttyS0";
      End_Flag : Boolean := False;


   begin
      Put_Line ("[Write] : Started;");
      while (End_Flag = False) loop
         select
            accept Init do
                     Put_Line ("[Write] : Open " & DeviceA & " for
write ...");
                     Open (WritePtr,Out_File,DeviceA,"SHARE=NO");
                     Put_Line ("[Write] : Opened for Write " & DeviceA);
            end Init;
         or
            accept Send_Command(Comm: in String) do
               Put_Line ("[Write] : Command Signal received: " & Comm &
";");
               Put_Line (WritePtr,Comm);
               Put_Line ("[Write] : Command sent: " & Comm & ";");
            end Send_Command;
         or
            accept Stop do
               Put_Line ("[Write] : Stop Signal received");
               End_Flag := True;
            end Stop;
         end select;
      end loop;

      Put_Line ("[Write] : Close " & DeviceA & " ...");
      Close (WritePtr);
      Put_Line ("[Write] : Closes " & DeviceA );
      Put_Line ("[Write] : Terminating task ...");
   end Write;
end  Ser_read;

=========== and the code of the main program
With Text_IO; use Text_IO;
with Ser_read;

procedure SerialOpen is
   FilePtr  : Text_IO.File_Type;
   CStr     : String (1..8);
   I        : Integer;
   C        : Character;


begin
   Put_Line ("[Main]: Start");
--   Ser_Read.Read.Init;
   Ser_Read.Write.Init;


   CStr := "W020 015";
   Put_Line ("[Main]: Initiate rotor command: " & CStr(1..CStr'Last));
   Ser_Read.Write.Send_Command(CStr);


   Put_Line ("[Main]: Press <CR>");
   Get (C);
   Put_Line ("[Main]: Initiate rotor command: C2");

 --  CStr := "C2";

   Ser_Read.Read.Read_Orientation;

   Put_Line ("[Main]: Press <CR>");
   Get (C);

   CStr := "W000 000";
   Put_Line ("[Main]: initiate rotor command: " & CStr(1..CStr'Last));
   Ser_Read.Write.Send_Command(CStr);

   Put_Line ("[Main]: Initate termination of Read task.. " );
   Ser_Read.Read.Stop;
   Put_Line ("[Main]: Initate termination of Write task.. " );
   Ser_Read.Write.Stop;
end;



Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how to access a (serial) device (linux)
  2000-01-27  0:00 how to access a (serial) device (linux) tilmanglotzner
@ 2000-01-27  0:00 ` David
  2000-01-27  0:00 ` Alfred Hilscher
  1 sibling, 0 replies; 3+ messages in thread
From: David @ 2000-01-27  0:00 UTC (permalink / raw)
  To: tilmanglotzner

In article <86op66$lu9$1@nnrp1.deja.com>,
  tilmanglotzner@my-deja.com wrote:
> Hello,
>
> I wrote a program to access serial port via the device file. One
> task reads on the port, another task writes to the port. The
> tasks somehow seem to block each other, and an antenna controller
> connected to the serial port does not receive anything.
>
> 1) Is accessing the serial port via a device file the wrong approach ?
> Is it better to do this by an interrupt service routine ? A sample
> which I could reuse would be very, very helpful here :-)
>
> 2) Maybe the processes are in a dead lock because 2 processes try to
> access the same file. Is it feasible to access a device file from 2
> task  ?
>
> 3) Maybe it is better to access the device file from only 1 task.
> I would need to open the file as read/write. Text_io seems not be
> capable of this.Are there other packages which allow opening
> (device) file as read/write ?
I have implemented a network physical layer via a serial port in
Linux, using a custom PCMCI card modem.  This channel runs reliably
at 512000 bps, because the hardware supports that kind of rate.

The underlying implementation uses the POSIX.IO and
POSIX.Terminal_Characteristics packages from the FLORIST implementation
of the IEEE Ada95 POSIX bindings.

The /dev/ttySN device is opened with POSIX.IO.Open with Read_Write
mode.

Reading is done with a task that puts the incoming octets in a
protected queue from which the link layer reads using an Ada.Streams
interface.  Reading is done with POSIX.IO.Read.

Writing is done without a task directly from the Stream's Write
procedure by calling POSIX.IO.Write.

To build a robust serial port package requires attention to quite
a number of details. For example, my port Open procedure first obtains
a lock on the port, using the lock file feature.

The link layer has a task (which I call a frame server), that reads the
incoming stream, identifying frames, and calling a procedure that has
registered with the task to receive frames.  This procedure is called
passing a validated frame as an Ada.Streams.Stream_Element_Array.


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how to access a (serial) device (linux)
  2000-01-27  0:00 how to access a (serial) device (linux) tilmanglotzner
  2000-01-27  0:00 ` David
@ 2000-01-27  0:00 ` Alfred Hilscher
  1 sibling, 0 replies; 3+ messages in thread
From: Alfred Hilscher @ 2000-01-27  0:00 UTC (permalink / raw)


I could imagine  that the problem raises due to the opening with
"SHARE=NO". So if one has opened, the other will be inhibited to open
too. Try an other share mode.

tilmanglotzner@my-deja.com wrote:
> 
> Hello,
> 
> I wrote a program to access serial port via the device file. One

> === Here the code ============================
>                Open (ReadPtr,In_File,DeviceB,"SHARED=NO");
>                      Open (WritePtr,Out_File,DeviceA,"SHARE=NO");




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2000-01-27  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-27  0:00 how to access a (serial) device (linux) tilmanglotzner
2000-01-27  0:00 ` David
2000-01-27  0:00 ` Alfred Hilscher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox