golang直接io的使用

Python048

golang直接io的使用,第1张

原文链接: https://mp.weixin.qq.com/s/gW_3JD52rtRdEqXvyg-lJQ?st=17068D6966C713BFB3B2DFDED2100927184379EBD73179345AA52F7C4770FAB3D9B809F2DDAF6808BD4F4F8FCCC362C468BF936BDB8AD9CD3FD180F6EF4D5D7B47895F771EE88366C0FE695489FF8C80FD9E42300D22D2760A2418189B459A619E1BA2E0BBCA50455B277293988FAAA45BCB1075B07EDAA36B62D6A0CAA886E8045895054068CB955DED17A44781062708DF01AAFCDA746F6C50158B19E5E42A88D02F0ECC5D8BC4EC108AF374A720C972EE1C5D832F23B6B107DC32194F4B0A3840F4739B0806FA6523E2D7A86ABD79A0EFE634158A19905FDB65E67AE97871&vid=1688855587348942&cst=E9F266277367B28319F39975A546E7810FB56065BDFDF61A124CA07F8C69AF9566D809347DAA8BFB56A5A5BFDAC28DAC&deviceid=3f371756-5e39-4325-9fec-0b55bfeb87f5&version=4.0.6.6516&platform=win

所以,通过以上 AlignedBlock 函数分配出来的内存一定是 512 地址对齐的。

有啥缺点吗?

浪费空间嘛。 命名需要 4k 内存,实际分配了 4k+512 。

开源库地址: https://github.com/ncw/directio

没有定义。

Go语言特点1、函数式编程闭包。

2、工程化资源管理,错误处理,测试无参,也没有定义返回值声明以后是import语句,引入要的模块。

//创建一个串口通讯

SerialPort CurrentPort = null

CurrentPort = new SerialPort()

CurrentPort.ReadBufferSize = 128

CurrentPort.PortName = comName //端口号

CurrentPort.BaudRate = bandRate//比特率

CurrentPort.Parity =parity//奇偶校验

CurrentPort.StopBits = stop//停止位

CurrentPort.DataBits = databit//数据位

CurrentPort.ReadTimeout = 1000//读超时,即在1000内未读到数据就引起超时异常

//绑定数据接收事件,因为发送是被动的,所以你无法主动去获取别人发送的代码,只能通过这个事件来处理

CurrentPort.DataReceived += Sp_DataReceived

CurrentPort.Open()

定义一个变量 byte[] receiveStr

//绑定的事件处理函数

private static void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

{

SerialPort sp = sender as SerialPort

if (sp == null)

return

byte[] readBuffer = new byte[sp.ReadBufferSize]

sp.Read(readBuffer, 0, readBuffer.Length)

//赋值

receiveStr=readBuffer//当然你可以通过转换将byte[]转换为字符串。

}

//你要求的按钮事件可以这么写

private void button1_Click(object sender, EventArgs e)

{

if(receiveStr!=null)

{

变量 xxx=receiveStr

}

}