Ada GNAT.Serial_通信如何转换流元素数组
我尝试使用Ada GNAT.Serial_通信如何转换流元素数组,ada,serial-communication,gnat,Ada,Serial Communication,Gnat,我尝试使用GNAT.Serial_Communications包编写一个带有Arduino的小型通信程序 建立与Arduino的沟通很好。 我正在使用Serial\u Communications.Read()函数获取信息。现在我想将存储在流元素数组中的数据转换成整数 我尝试了Integer'Value()函数,但它不起作用,我收到错误消息:预期类型“Standard.String” 使用String'Value() 我找不到有关流元素\u数组转换的任何信息 Serial_Communica
GNAT.Serial_Communications
包编写一个带有Arduino的小型通信程序
建立与Arduino的沟通很好。
我正在使用Serial\u Communications.Read()
函数获取信息。现在我想将存储在流元素数组中的数据转换成整数
我尝试了Integer'Value()
函数,但它不起作用,我收到错误消息:预期类型“Standard.String”
使用String'Value()
我找不到有关流元素\u数组转换的任何信息
Serial_Communications.Open
(Port => Port,
Name => Port_Name);
Serial_Communications.Set
(Port => Port,
Rate => Serial_Communications.B115200,
Bits => Serial_Communications.CS8,
Stop_Bits => Serial_Communications.One,
Parity => Serial_Communications.Even);
Serial_Communications.Read(Port => Port,
Buffer => Buffer,
Last => Last);
Int_value := Integer'Value(String'Value(Buffer(1)));
Serial_Communications.Close(Port => Port);
从我在中看到的情况来看,Stream\u元素
是一种模块化类型,顺便说一句,它应该已经被转换为一个
差不多
Int_值:=整数(Buffer(Buffer'First));
应该直接工作,但我没有测试任何东西;) 从我在中看到的来看,Stream\u元素
是一种模块化类型,顺便说一句,它应该已经被转换为一个
差不多
Int_值:=整数(Buffer(Buffer'First));
应该直接工作,但我没有测试任何东西;) 类型GNAT.Serial\u Communications.Serial\u Port
是对Ada.Streams.Root\u Stream\u类型的扩展:
type Serial_Port is new Ada.Streams.Root_Stream_Type with private;
这意味着您可以将其用作流,从而使用Read
属性(有关详细信息,请参阅)。此外,我建议使用接口
包中的整数类型,而不是Ada整数
类型,因为所有Ada整数类型都是由支持的最小范围定义的,并且没有强制存储大小(有关详细信息,请参阅和)
以下示例可能会有所帮助:
with Interfaces;
with GNAT.Serial_Communications;
procedure Main is
package SC renames GNAT.Serial_Communications;
Port_Name : SC.Port_Name := "COM1";
Port : aliased SC.Serial_Port;
Int_Value : Interfaces.Integer_8;
begin
SC.Open
(Port => Port,
Name => Port_Name);
SC.Set
(Port => Port,
Rate => SC.B115200,
Bits => SC.CS8,
Stop_Bits => SC.One,
Parity => SC.Even);
-- Check Interfaces package for types other than "Integer_8".
Interfaces.Integer_8'Read (Port'Access, Int_Value);
SC.Close(Port => Port);
end Main;
类型GNAT.Serial\u Communications.Serial\u Port
是对Ada.Streams.Root\u Stream\u类型的扩展:
type Serial_Port is new Ada.Streams.Root_Stream_Type with private;
这意味着您可以将其用作流,从而使用Read
属性(有关详细信息,请参阅)。此外,我建议使用接口
包中的整数类型,而不是Ada整数
类型,因为所有Ada整数类型都是由支持的最小范围定义的,并且没有强制存储大小(有关详细信息,请参阅和)
以下示例可能会有所帮助:
with Interfaces;
with GNAT.Serial_Communications;
procedure Main is
package SC renames GNAT.Serial_Communications;
Port_Name : SC.Port_Name := "COM1";
Port : aliased SC.Serial_Port;
Int_Value : Interfaces.Integer_8;
begin
SC.Open
(Port => Port,
Name => Port_Name);
SC.Set
(Port => Port,
Rate => SC.B115200,
Bits => SC.CS8,
Stop_Bits => SC.One,
Parity => SC.Even);
-- Check Interfaces package for types other than "Integer_8".
Interfaces.Integer_8'Read (Port'Access, Int_Value);
SC.Close(Port => Port);
end Main;
请告诉我们您发送给Arduino的是什么:例如“12345”
或0x30
,0x39
或…请告诉我们您发送给Arduino的是什么:例如“12345”
或0x30
,0x39
或…工作正常。非常感谢。伟大的不要忘记点击正确答案左边的勾号,将您的问题标记为已回答;)工作。非常感谢。伟大的不要忘记点击正确答案左边的勾号,将您的问题标记为已回答;)