首页 > 其他 > 详细

Ada 串口

时间:2021-06-01 19:19:39      阅读:18      评论:0      收藏:0      [点我收藏+]

Ada 使用串口的一个例子。使用一个TX接RX的USB转RS232。

with Ada.Streams;
with GNAT.Serial_Communications;
with Ada.Text_IO;

procedure Serial is
	use Ada.Streams;
	use GNAT;
	use Ada.Text_IO;

	subtype Message is Stream_Element_Array (1 .. 20);

	Data   : constant String (1 .. 20)  := "ABCDEFGHIJLKMNOPQRST";
	Read_Data   :  String (1 .. 20)  ;
	Buffer : Message;
	Read_Buf : Message;

	S_Port : constant Natural := 1;
	--  Serial port number

begin
	--  Convert message (String -> Stream_Element_Array)

	for K in Data‘Range loop
	   Buffer (Stream_Element_Offset (K)) := Character‘Pos (Data (K));
	end loop;

	declare
	   --Port_Name : constant Serial_Communications.Port_Name :=
		--			 Serial_Communications.Name (Number => S_Port);
		Port_Name : constant Serial_Communications.Port_Name := "/dev/ttyUSB0";
	   Port      : Serial_Communications.Serial_Port;
	   
	   Offset : Stream_Element_Offset;
	   

	begin
		Put("Port  = ");
		Put_Line (String(Port_Name));
		
	   Serial_Communications.Open
		 (Port => Port,
		  Name => Port_Name);

	   Serial_Communications.Set
		 (Port      => Port,
		  Rate      => Serial_Communications.B9600,
		  Bits      => Serial_Communications.CS8,
		  Stop_Bits => Serial_Communications.One,
		  Parity    => Serial_Communications.Even);

	   Serial_Communications.Write
		 (Port   => Port,
		  Buffer => Buffer);
		  
		 Put ("Write = ");
		 Put_Line (Data);
		  
		 Serial_Communications.Read
		 (Port   => Port,
		  Buffer => Read_Buf,
		  Last => Offset);

	for K in Data‘Range loop
	   Read_Data(K) := Character‘Val(Read_Buf (Stream_Element_Offset (K)));
	end loop;
	Put("Read  = ");
	Put_Line (Read_Data);
	
	Serial_Communications.Close (Port => Port);
	end;
end Serial;

Ada 串口

原文:https://www.cnblogs.com/santion/p/14838225.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!