From: tilmanglotzner@my-deja.com
Subject: how to access a (serial) device (linux)
Date: 2000/01/27
Date: 2000-01-27T00:00:00+00:00 [thread overview]
Message-ID: <86op66$lu9$1@nnrp1.deja.com> (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.
next reply other threads:[~2000-01-27 0:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-01-27 0:00 tilmanglotzner [this message]
2000-01-27 0:00 ` how to access a (serial) device (linux) Alfred Hilscher
2000-01-27 0:00 ` David
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox