`
touchinsert
  • 浏览: 1314428 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

c# 2.0 serial port communication

 
阅读更多
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.IO.Ports;

namespaceConsoleApplication3
...{
classSerialPortTest
...{
publicstringMessageStr="";
publicboolIsMessageFlag=false;

SerialPortport1;

/**////<summary>
///function:initialserialport
///</summary>
///<paramname="PortName">portnumber</param>
///<paramname="BaudRate">portspeed</param>


publicvoidInitCOM(stringPortName,intBaudRate)
...{
port1
=newSerialPort(PortName,BaudRate);
port1.Handshake
=Handshake.RequestToSend;

port1.DataReceived
+=newSerialDataReceivedEventHandler(port1_DataReceived);
}

/**////<summary>
///function:dealwiththedatareceive
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>

privatevoidport1_DataReceived(objectsender,SerialDataReceivedEventArgse)
...{
StringBuildercurrentline
=newStringBuilder();
if(!IsMessageFlag)
...{
while(port1.BytesToRead>0)
...{
charch=(char)port1.ReadByte();
currentline.Append(ch);
}

MessageStr
=currentline.ToString();
IsMessageFlag
=true;
currentline
=newStringBuilder();
}

//thrownewException("Themethodoroperationisnotimplemented.");
}

/**////<summary>
///function:sendtheinstruction
///</summary>
///<paramname="CommandString"></param>

publicvoidSendCommand(stringCommandString)
...{
byte[]WriteBuffer=Encoding.ASCII.GetBytes(CommandString);
port1.Write(WriteBuffer,
0,WriteBuffer.Length);
}

/**////<summary>
///function:opentheclosedport
///</summary>

publicvoidOpenPort()
...{
port1.Open();
if(port1.IsOpen)
...{
Console.WriteLine(
"theportisopened!");
}

else
...{
Console.WriteLine(
"failuretoopentheport!");
}

}

/**////<summary>
///function:closetheopenedport
///</summary>

publicvoidClosePort()
...{
port1.Close();
if(port1.IsOpen)
...{
Console.WriteLine(
"theportisalreadyclosed!");
}

}


}

}

分享到:
评论

相关推荐

    serial-port-communication.zip_C# serial port_USB 串口通信

    本压缩包“serial-port-communication.zip”显然是针对C#环境下通过USB转串口进行通信的实践教程或代码示例。 在C#中,`System.IO.Ports`命名空间提供了SerialPort类,它是处理串行通信的主要工具。以下将详细介绍...

    visual studio 2010 C# serial port sample

    visual studio 2010 C# serial port sample, welcome communication with me,C# & visual studio learning, this sample is change from echo ,ui is ok, just change little , you can use two comport ...

    c# serial communication using wpf

    为了调试和测试串行通信,你可以使用串口调试助手软件,如RealTerm或Hercules Serial Port Monitor,它们可以显示发送和接收的数据,帮助定位通信问题。 总的来说,通过C#和WPF,我们可以构建一个用户友好的串行...

    Serial Port Communication with winform.伺服系统上位机 .zip

    Winform使用技巧,实战应用开发小系统参考资料,源码参考。经测试可运行。 详细介绍了一些Winform框架的各种功能和模块,以及如何使用Winform进行GUI开发、网络编程和跨平台应用开发等。 适用于初学者和有经验的...

    serial-communication-program.zip_C# serial

    它内建丰富的外设接口,包括串行通信接口(Serial Communication Interface, SCI),可以实现UART(通用异步收发传输器)功能,适用于简单的串行通信任务。 C#是一种面向对象的编程语言,通常用于Windows桌面应用...

    SerialPortHelper_C#_rs232串口_SerialPortHelper_

    在IT行业中,串行通信(Serial Communication)是一种基础但至关重要的技术,特别是在设备间的数据交换和嵌入式系统中。RS232是串行通信的一种标准,全称为"推荐标准232",它定义了接口的电气特性、信号线功能以及...

    PC-Serial-Communication.rar_CSharp serial_CSharp 串口_Csharp 界面设计_

    在本文中,我们将深入探讨如何使用C#进行串行通信,特别是在PC-Serial-Communication项目中,这涉及到C#编程语言、串口通信以及界面设计。串行通信是计算机之间或计算机与硬件设备之间交换数据的一种常见方式,尤其...

    SerialCommunication.zip

    本项目"SerialCommunication.zip"包含了一套基于C#语言编写的串口通信工具源码,支持RS485和RS232两种常见的串行通信协议。下面将详细介绍这些知识点。 **RS485和RS232简介** RS485和RS232是两种广泛应用于工业控制...

    Serial 串口C++ C C#

    在IT领域,串口通信(Serial Port Communication)是一种古老但仍然广泛应用的数据传输方式,尤其在嵌入式系统、工业控制以及设备调试等场景中。本文将深入探讨串口通信的基本概念,以及如何在C++、C和C#编程语言中...

    C#通过串口获取地磅称重重量.zip

    二、串行通信(Serial Port Communication) 串行通信是一种简单但实用的数据传输方式,它一次传输一个比特位,适用于长距离通信或者设备之间的低速连接。在Windows系统中,串口通常被映射为COM端口。C#中的`System....

    RS232_serial_port_communication.rar_C#编程_C#_

    标题中的“RS232_serial_port_communication.rar”暗示了我们将在C#编程环境中探讨如何通过RS232串行端口进行通信。RS232,全称是EIA/TIA-232,是一种广泛使用的串行通信接口标准,主要用于设备间的通信,如计算机与...

    Arduino-Serial-Communication:C#中的Arduino串行通信

    "Arduino-Serial-Communication:C#中的Arduino串行通信"这个主题涵盖了利用C#语言与Arduino板进行数据交互的基础知识,这对于开发基于Arduino的嵌入式项目至关重要。 串行通信是Arduino与计算机或其他设备之间常用...

    C#代码小程序

    串口通信(Serial Port Communication)是C#编程中的一个重要领域,尤其是在硬件交互和设备控制中。串口通信允许计算机通过串行接口与其他设备进行数据交换。在C#中,我们可以使用System.IO.Ports命名空间中的...

    自己用c#写的串口升级单片机代码程序

    在IT行业中,串口通信(Serial Port Communication)是一种古老但仍然广泛应用的技术,特别是在嵌入式系统和设备固件升级中。本项目是一个使用C#语言编写的串口升级单片机代码程序,它具备对HEX文件的处理能力,使得...

    C#中串口编程

    在C#编程中,串口通信(Serial Port Communication)是一种常用的技术,用于设备间的异步数据传输,如打印机、GPS设备、嵌入式系统等。本文将深入探讨C#中的串口编程,包括如何设置串口参数、发送和接收数据以及处理...

    c#串口通信+CRC校验

    串口通信(Serial Port Communication)是指通过计算机的串行端口进行数据传输。在C#中,我们可以使用`System.IO.Ports`命名空间中的`SerialPort`类来实现这一功能。`SerialPort`类提供了一系列属性、方法和事件,如...

    c# 串口转网络透传 上位机

    串口通信(Serial Port Communication)是计算机硬件通信的一种方式,它允许设备通过串行数据线进行双向通信。在C#中,可以使用`System.IO.Ports`命名空间下的类,如`SerialPort`,来管理和操作串口。这包括设置波特...

    激光所激光测距数据采集C#代码.zip

    从压缩包子文件的文件名称“SerialportSample-master”来看,这是项目的一个分支或版本,可能意味着该系统使用了串口通信(Serial Port Communication)功能。串口通信是计算机间通过串行接口进行数据交换的一种方式...

    C# 与上位机松下plc通信的列子。

    在C#中,与硬件设备通信通常涉及串口通信(Serial Port Communication)、网络通信(如TCP/IP)或通过特定的库和API。对于PLC,我们可能需要使用如Modbus、OPC UA等工业通信协议。松下PLC通常支持多种通信方式,例如...

    C# 串口通信 通用通信工具

    串口通信(Serial Port Communication)是指通过串行接口进行的数据传输,通常包括RS-232、RS-485等标准。在C#中,System.IO.Ports命名空间提供了SerialPort类,该类包含了创建、配置和管理串口所需的所有方法和属性...

Global site tag (gtag.js) - Google Analytics