SerialPort类的用法与示例

SerialPort类的用法与示例

2023年6月22日发(作者:)

SerialPort类的⽤法与⽰例Microsoft .Net框架SerialPort类的⽤法与⽰例从Microsoft .Net 2.0版本以后,就默认提供了Port类,⽤户可以⾮常简单地编写少量代码就完成串⼝的信息收发程序。本⽂将介绍如何在PC端⽤C# .Net 来开发串⼝应⽤程序。1. 串⼝硬件信号定义DB9 Connector 信号定义针脚信号123456789RXDTXDDTRDSRRTSCTSRI定义接收数据发送数据作⽤Received Line Signal Detector(Data Carrier Detect)Received DataTransmit DataSignal GroundData Set ReadyRequest To SendClear To SendRing IndicatorDCD载波检测数据终端准备好Data Terminal Ready数据准备好请求发送清除发送振铃提⽰SGND信号地2. 串⼝端⼝号搜索⼀个最简单的办法:string[] portList = tNames();for (int i = 0; i < ; i++){ string name = portList[i]; (name);}  还有⼀种通过调⽤API的⽅法来获取实现,可以获取详细的完整串⼝名称,对于USB-to-COM虚拟串⼝来说特别适⽤。通过下⾯程序可以获取到与设备管理器中⼀样的名字,例如“Prolific USB-to-Serial Comm Port(COM34)”, ⽽上⾯的⽅法只能获取到“COM34”。///

/// 枚举win32 api/// public enum HardwareEnum{ // 硬件 Win32_Processor, // CPU 处理器 Win32_PhysicalMemory, // 物理内存条 Win32_Keyboard, // 键盘 Win32_PointingDevice, // 点输⼊设备,包括⿏标。 Win32_FloppyDrive, // 软盘驱动器 Win32_DiskDrive, // 硬盘驱动器 Win32_CDROMDrive, // 光盘驱动器 Win32_BaseBoard, // 主板 Win32_BIOS, // BIOS 芯⽚ Win32_ParallelPort, // 并⼝ Win32_SerialPort, // 串⼝ Win32_SerialPortConfiguration, // 串⼝配置 Win32_SoundDevice, // 多媒体设置,⼀般指声卡。 Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP) Win32_USBController, // USB 控制器 Win32_NetworkAdapter, // ⽹络适配器 Win32_NetworkAdapterConfiguration, // ⽹络适配器设置 Win32_Printer, // 打印机 Win32_PrinterConfiguration, // 打印机设置 Win32_PrintJob, // 打印机任务 Win32_TCPIPPrinterPort, // 打印机端⼝ Win32_POTSModem, // MODEM Win32_POTSModemToSerialPort, // MODEM 端⼝ Win32_DesktopMonitor, // 显⽰器 Win32_DisplayConfiguration, // 显卡 Win32_DisplayControllerConfiguration, // 显卡设置 Win32_VideoController, // 显卡细节。 Win32_VideoSettings, // 显卡⽀持的显⽰模式。 // 操作系统 Win32_TimeZone, // 时区 Win32_SystemDriver, // 驱动程序 Win32_DiskPartition, // 磁盘分区 Win32_LogicalDisk, // 逻辑磁盘 Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。 Win32_LogicalMemoryConfiguration, // 逻辑内存配置 Win32_PageFile, // 系统页⽂件信息 Win32_PageFileSetting, // 页⽂件设置 Win32_BootConfiguration, // 系统启动配置 Win32_ComputerSystem, // 计算机信息简要 Win32_OperatingSystem, // 操作系统信息 Win32_StartupCommand, // 系统⾃动启动程序 Win32_Service, // 系统安装的服务 Win32_Group, // 系统管理组 Win32_GroupUser, // 系统组帐号 Win32_UserAccount, // ⽤户帐号 Win32_Process, // 系统进程 Win32_Thread, // 系统线程 Win32_Share, // 共享 Win32_NetworkClient, // 已安装的⽹络客户端 Win32_NetworkProtocol, // 已安装的⽹络协议 Win32_PnPEntity,//all device}/// /// WMI取硬件信息/// /// /// /// public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey){ List strs = new List(); try { using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType)) { var hardInfos = (); foreach (var hardInfo in hardInfos) { if (ties[propKey].ng().Contains("COM")) { (ties[propKey].ng()); } } e(); } return y(); } catch { return null; } finally { strs = null; }}//通过WMI获取COM端⼝string[] portList = MulGetHardwareInfo(32_PnPEntity, "Name");  3. 串⼝属性参数设置参见MSDN上的帮助⽂件,SerialPort类所包含的属性详见下表。 名称BaseStreamBaudRateBreakStateBytesToReadBytesToWriteCanRaiseEventsCDHoldingContainer说明获取 Stream 对象的基础 SerialPort 对象。获取或设置串⾏波特率。获取或设置中断信号状态。获取接收缓冲区中数据的字节数。获取发送缓冲区中数据的字节数。获取⼀个值,该值指⽰组件是否可以引发⼀个事件。(继承⾃ Component。)获取端⼝的载波检测⾏的状态。获取 IContainer ,其中包含 Component。(继承⾃ Component。)获取“可以发送”⾏的状态。CtsHoldingDataBitsDesignModeDiscardNullDsrHoldingDtrEnableEncodingEventsHandshakeIsOpenNewLineParityParityReplacePortNameReadBufferSizeReadTimeoutRtsEnableSiteStopBitsWriteBufferSizeWriteTimeout简单初始化串⼝参数的⽰例程序:SerialPort mySerialPort = new SerialPort("COM2");te = 9600;=;ts = ;ts = 8;ake = ;获取“可以发送”⾏的状态。获取或设置每个字节的标准数据位长度。获取⼀个值,该值指⽰是否 Component 当前处于设计模式。(继承⾃ Component。)获取或设置⼀个值,该值指⽰ null 字节在端⼝和接收缓冲区之间传输时是否被忽略。获取数据设置就绪 (DSR) 信号的状态。获取或设置⼀个值,该值在串⾏通信过程中启⽤数据终端就绪 (DTR) 信号。获取或设置传输前后⽂本转换的字节编码。获取的事件处理程序附加到此列表 Component。(继承⾃ Component。)使⽤ Handshake 中的值获取或设置串⾏端⼝数据传输的握⼿协议。获取⼀个值,该值指⽰ SerialPort 对象的打开或关闭状态。获取或设置⽤于解释 ReadLine 和 WriteLine ⽅法调⽤结束的值。获取或设置奇偶校验检查协议。获取或设置⼀个字节,该字节在发⽣奇偶校验错误时替换数据流中的⽆效字节。获取或设置通信端⼝,包括但不限于所有可⽤的 COM 端⼝。获取或设置 SerialPort 输⼊缓冲区的⼤⼩。获取或设置读取操作未完成时发⽣超时之前的毫秒数。获取或设置⼀个值,该值指⽰在串⾏通信中是否启⽤请求发送 (RTS) 信号。获取或设置 ISite 的 Component。(继承⾃ Component。)获取或设置每个字节的标准停⽌位数。获取或设置串⾏端⼝输出缓冲区的⼤⼩。获取或设置写⼊操作未完成时发⽣超时之前的毫秒数。ReceivedBytesThreshold获取或设置 DataReceived 事件发⽣前内部输⼊缓冲区中的字节数。ceived += new SerialDataReceivedEvenHandler(DataReceive_Method);();  4. 串⼝发送信息SerialPort类定义了多种⽅法⽤于串⼝发送信息。Write(Byte[], Int32, Int32) 使⽤缓冲区中的数据将指定数量的字节写⼊串⾏端⼝Write(Char[], Int32, Int32) 使⽤缓冲区中的数据将指定数量的字符写⼊串⾏端⼝Write(String) 将指定的字符串写⼊串⾏端⼝WriteLine(String) 将指定的字符串和NewLine值写⼊输出缓冲区下⾯是⼀个简单的例⼦说明如何通过串⼝发送字符串和字节数据:using ;private static void SendSampleData(){ // Instantiate the communications // port with some basic settings SerialPort port = new SerialPort( "COM1", 9600, , 8, ); // Open the port for communications (); // Write a string ("Hello World"); // Write a set of bytes (new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3); // Close the port ();}  下⾯是如何发送⼀个⽂本⽂件的例⼦:private static void SendTextFile(SerialPort port, string FileName){ (xt(FileName).ReadToEnd());}  下⾯是如何发送⼀个⼆进制⽂件的例⼦:private static void SendBinaryFile(SerialPort port, string FileName){ using (FileStream fs = ad(FileName)) ((new BinaryReader(fs)).ReadBytes((int)), 0, (int));}  5. 串⼝接收信息SerialPort类定义了多种⽅法⽤于串⼝接收信息。Read(Byte[], Int32, Int32) 从SerialPort输⼊缓冲区读取⼀些字节,并将那些字节写⼊字节数组中指定的偏移量处Read(Byte[], Int32, Int32) 从SerialPort输⼊缓冲区读取⼀些字符,并将那些字符写⼊字符数组中指定的偏移量处ReadByte() 从SerialPort输⼊缓冲区中同步读取⼀个字节ReadChar() 从SerialPort输⼊缓冲区中同步读取⼀个字符ReadExisting() 在编码的基础上,读取SerialPort对象的流和输⼊缓冲区中所有⽴即可⽤的字节ReadLine() ⼀直读取到输⼊缓冲区中的NewLine值ReadTo(String) ⼀直读取到输⼊缓冲区中的指定value的字符串通常⼀个⽐较常见的⽤法就是将串⼝⾥⾯⽴即能⽤的字符或数据读取然后打印在textbox等控件中显⽰#region Namespace Inclusionsusing System;using ;using ;#endregionnamespace SerialPortExample{ class SerialPortProgram { // Create the serial port with basic settings private SerialPort port = new SerialPort("COM1", 9600, , 8, ); [STAThread] static void Main(string[] args) { // Instatiate this class new SerialPortProgram(); } private SerialPortProgram() { ine("Incoming Data:"); // Attach a method to be called when there // is data waiting in the port's buffer ceived += new SerialDataReceivedEventHandler(port_DataReceived); // Begin communications (); // Enter an application loop to keep this thread alive (); } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Show all the incoming data in the port's buffer ine(isting()); } }}  另外还有⼀种应⽤场合是需要缓存⼀段串⼝接收数据,然后在缓存数据中查找有⽤信息,这时可以采⽤下⾯例⼦所⽤的办法。using System;using ;using c;namespace SerialComBuffering{ class Program { SerialPort com = new SerialPort(tNames()[0], 9600, , 8, ); List bBuffer = new List(); string sBuffer = ; static void Main(string[] args) { new Program(); } Program() { ceived += new SerialDataReceivedEventHandler(com_DataReceived); (); ine("Waiting for "); y(); } void com_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Use either the binary OR the string technique (but not both) // Buffer and process binary data while (oRead > 0) ((byte)te()); ProcessBuffer(bBuffer); // Buffer string data sBuffer += isting(); ProcessBuffer(sBuffer); } private void ProcessBuffer(string sBuffer) { // Look in the string for useful information // then remove the useful data from the buffer } private void ProcessBuffer(List bBuffer) { // Look in the byte array for useful information // then remove the useful data from the buffer } }}  

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687368733a4830.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信