用C#怎么写UART、SPI、I2C、CAN总线上测试时的数据程序?

用C#怎么写UART、SPI、I2C、CAN总线上测试时的数据程序?,第1张

针对 树莓派 封装的 UWP应用 类,以下代码未经测试,聊以抛砖引玉:

using System

using System.Collections.Generic

using System.Linq

using System.Text

using System.Threading.Tasks

using Windows.Devices.Gpio

using Windows.Storage.Streams

using Windows.Devices.Enumeration

using Windows.Devices.SerialCommunication

using Windows.Devices.I2c

using Windows.Devices.Spi

namespace SlotAgentApp.Services

{

public class DevicesHelper

{

public static void GPIO(int eLevel=0, int ioPortNumber = 5)

{

// 获得系统板载缺省GPIO controller

GpioController gpio = GpioController.GetDefault()

if (gpio == null)

return// 如果系统板载无可用GPIO,则返回

// 打开指定的GPIO

using (GpioPin pin = gpio.OpenPin(ioPortNumber))

{

// Latch HIGH value first. This ensures a default value when the pin is set as output

pin.Write(eLevel==0? GpioPinValue.Low: GpioPinValue.High)

// 设置IO为输出方向

pin.SetDriveMode(GpioPinDriveMode.Output)

} //关闭 pin - will revert to its power-on state

}

#region 示例代码

/*

(一)GPIO接口使用

*/

public void GPIO()

{

// Get the default GPIO controller on the system

GpioController gpio = GpioController.GetDefault()

if (gpio == null)

return// GPIO not available on this system

// Open GPIO 5

using (GpioPin pin = gpio.OpenPin(5))

{

// Latch HIGH value first. This ensures a default value when the pin is set as output

pin.Write(GpioPinValue.High)

// Set the IO direction as output

pin.SetDriveMode(GpioPinDriveMode.Output)

} // Close pin - will revert to its power-on state

}

/*

(二) UART接口

Pin 8 - UART0 TX

Pin 10- UART0 RX

下面的例子先是初始化UART0,然后做了一次读 *** 作和一次写 *** 作

*/

public async void Serial()

{

string aqs = SerialDevice.GetDeviceSelector("UART0") /* Find the selector string for the serial device */

var dis = await DeviceInformation.FindAllAsync(aqs) /* Find the serial device with our selector string */

SerialDevice SerialPort = await SerialDevice.FromIdAsync(dis[0].Id) /* Create an serial device with our selected device */

/* Configure serial settings */

SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000)

SerialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000)

SerialPort.BaudRate = 9600

SerialPort.Parity = SerialParity.None

SerialPort.StopBits = SerialStopBitCount.One

SerialPort.DataBits = 8

/* Write a string out over serial */

string txBuffer = "Hello Serial"

DataWriter dataWriter = new DataWriter()

dataWriter.WriteString(txBuffer)

uint bytesWritten = await SerialPort.OutputStream.WriteAsync(dataWriter.DetachBuffer())

/* Read data in from the serial port */

const uint maxReadLength = 1024

DataReader dataReader = new DataReader(SerialPort.InputStream)

uint bytesToRead = await dataReader.LoadAsync(maxReadLength)

string rxBuffer = dataReader.ReadString(bytesToRead)

}

/*

使用上面的例子时,需要在Package.appxmanifest 中修改下权限修改如下

<Capabilities>

<DeviceCapability Name="serialcommunication">

<Device Id="any">

<Function Type="name:serialPort" />

</Device>

</DeviceCapability>

</Capabilities>

*/

/*

(三)I2C 总线

Pin 3 - I2C1 SDA

Pin 5 - I2C1 SCL

下面的例子首先初始化I2C1 然后向地址为0x40的I2C设备写数据

*/

public async void I2C()

{

// Get a selector string for bus "I2C1"

string aqs = I2cDevice.GetDeviceSelector("I2C1")

// Find the I2C bus controller with our selector string

var dis = await DeviceInformation.FindAllAsync(aqs)

if (dis.Count == 0)

return// bus not found

// 0x40 is the I2C device address

var settings = new I2cConnectionSettings(0x40)

// Create an I2cDevice with our selected bus controller and I2C settings

using (I2cDevice device = await I2cDevice.FromIdAsync(dis[0].Id, settings))

{

byte[] writeBuf = { 0x01, 0x02, 0x03, 0x04 }

device.Write(writeBuf)

}

}

/*

(四) SPI 总线

Pin 19 - SPI0 MOSI

Pin 21 - SPI0 MISO

Pin 23 - SPI0 SCLK

Pin 24 - SPI0 CS0

Pin 26 - SPI0 CS1

下面的例子向SPI0 做了一次写 *** 作

*/

public async void SPI()

{

// Get a selector string for bus "SPI0"

string aqs = SpiDevice.GetDeviceSelector("SPI0")

// Find the SPI bus controller device with our selector string

var dis = await DeviceInformation.FindAllAsync(aqs)

if (dis.Count == 0)

return// "SPI0" not found on this system

// Use chip select line CS0

var settings = new SpiConnectionSettings(0)

// Create an SpiDevice with our bus controller and SPI settings

using (SpiDevice device = await SpiDevice.FromIdAsync(dis[0].Id, settings))

{

byte[] writeBuf = { 0x01, 0x02, 0x03, 0x04 }

device.Write(writeBuf)

}

}

#endregion

}

}

#include<pic.h>//包含单片机内部资源预定义

#ifndef uchar

#define uchar unsigned char

#define uint unsigned int

#endif

//__CONFIG(0x2129)

bit rec_flag //串口中断标志位,有串口中断,置1

uchar uartdata //串口数据保存到此位置中

void Uart_char(uchar dat)//串口发送一个数据

/*************************************************************

函数原型:void main(void)

功能:等待串口发送中断到来,将接收到的设计再通过串口发回

*************************************************************/

void main(void)

{

TRISB1=1

TRISB2=1

SPBRG=0X19 //设置波特率为9600BPS

TXSTA=0X24 //使能串口发送,选择高速波特率

RCSTA=0X90 //使能串口工作,连续接收

RCIE=0X1 //使能接收中断

GIE=0X1 //开放全局中断

PEIE=0X1 //使能外部中断

//INTCON=0X00

/* while(1) //查询模式下

{

RCIE=1

while(RCIF==0)

RCIE=0

Send_char(RCREG)

}

*/

while(1) //中断模式下,等待中断的到来

{

if(rec_flag==1) //如果接收中断到来

{

rec_flag=0//接收标志清零

Uart_char(uartdata)//将接收来的数据发送到串口

}

}

}

/**********************************************

函数原型:void Uart_char(uchar dat)

功能:将dat数据通过串口传送出去

**********************************************/

void Uart_char(uchar dat)

{

TXREG=dat //将dat数据存入TXREG

// TXEN=1 //启动发送,TSR开始移位 *** 作。

while (TRMT==0) //判断是否TSR发送完毕,未完等待。

// TXEN=0 //关发送功能,防止TXREG空时,TXIF产生置位

}

/**********************************************

函数原型:void interrupt usart(void)

功能:串口接收到数据,接受发来的数据将接

收到数据标志位rec_flag置1

**********************************************/

void interrupt usart(void)

{

if(RCIF) //判断是否为串口接收中断

{

rec_flag = 1

//RCIF=0

uartdata = RCREG// 接收数据并存储

//TXREG=recdata //把接收到的数据发送回去

}

}

这段程序应该对你有用


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11423210.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存