C#下用rs232串口通讯问题

leiminghui 2003-10-16 07:20:53
各位大侠,我现在在做一个ITS的大型项目。由于公司技术的转型。我们用。NET作为开发工具。现在有关如何与设备进行串口通讯不大明,请指点。最好能提供源代码。
...全文
253 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
ms44 2003-12-09
  • 打赏
  • 举报
回复
如果熟悉vc,那么就直接用invoke调用api好了.
langzifengxing 2003-12-09
  • 打赏
  • 举报
回复
Adding the MSComm Control



You must add the control to a form instead of just instantiating the control straight from code because it requires special OCX state information and a developing license be included in your program's assembly. By drawing it onto a form, VS.NET handles these for you.

Create a new Windows Form
Add the MSComm COM/OCX Control to your "Windows Forms"
Right Click on the Toolbox
Choose "Customize Toolbox..."
Select and add the "Microsoft Communications Control"
Draw the new control onto your form (Telephone icon)

Properties and Event Info



Here is a quick overview of some important properties of the MSComm control.

com.CommPort
Sets or gets the computer's serial port to be used.


com.PortOpen
Opens or closes the serial port.


com.RThreshold
Sets how many characters should be received before firing the OnComm event. Set to 0 to disable event calling. Set to 1 to fire OnComm every time a character is received.


com.InputMode
On of the MSCommLib.InputModeConstants constants to specify either sending/receiving text strings or byte arrays. Defaults to text which is easier to work with but not as reliable as byte arrays.


com.Settings
Used to setup the port in the format "baud,p,d,s" where baud = baud rate, p = parity, d = # data bits, and s = # stop bits. Ex: com.Settings = "9600,n,8,1"


com.Handshaking
On of the MSCommLib.HandshakeConstants constants to specify the type of handshaking: none, RTS/CTS hardware hs, and/or XOn/XOff software hs


com.InBufferCount
Returns the number of characters waiting in the receive buffer.


com.Input
Returns and removes a stream of data from the receive buffer. Used to check for data waiting. Returns a string if in text mode or byte array if in binary/byte mode.


com.Output
Writes a stream of data to the transmit buffer. Ex: com.Output = "Hello" sends "Hello" through the serial port.


com.CommEvent
Returns a MSCommLib.CommEventConstants, MSCommLib.ErrorConstants, or MSCommLib.OnCommConstants constant representing the most recent error or event that occurred. Check this in the OnComm event.


com.NullDiscard
If true, the serial control will ignore all 0x00 (null) characters come in. You will usually want to disable this so you can receive 0x00 since it may be important.


com.InputLen
The number of characters the Input property reads from the receive buffer. Setting InputLen to 0 reads the entire contents of the receive buffer when com.Input is used.

OnComm Event
The one single event that the com control calls is the OnComm event whenever something happens. To use this, be sure to set RThreshold = 1 and check the InBufferCount inside your event. Use com.CommEvent for more information as to why the OnComm event was fired. Example:

public MyForm()
{
InitializeComponents(); // Initialize Form Components
com.RThreshold = 1; // Fire OnComm event after any data is received
com.OnComm += new System.EventHandler(this.OnComm); // Assigns the event handler
}

private void OnComm(object sender, EventArgs e) // MSCommLib OnComm Event Handler
{
if (com.InBufferCount > 0) ProcessData((string) com.Input);
if (com.CommEvent == MSCommLib.OnCommConstants.comEvCTS)
Console.WriteLine("CTS Line Changed");
}




Protocol Development



If you are making your own serial interface/protocol, you really must have a good standard in place. Serial data flows into the com port byte by byte and must be buffered and parsed correctly. Think of it this way, if a terminal sent your computer "Hello World" it may come in as four OnComm triggers: "H", "ello", " Wo", and "rld"

The best protocols are usually a mix of these methods.
Here are three simple protocol techniques:

Beginning and Ending ("Start" & "Stop") Codes
This is good for sending text as it lets everybody know when text starts and ends. You simply tack on a non-normal byte at the beginning and end of the text. For example, you'd use '---' to signify the start of the string and '===' to signify the end. So you would use: com.Output = "---Hello World===";


Fixed Length Codes
Used for specific commands. You can create your own codes to send and specify what they mean. Say I want to control the lighting in a house, I'd setup a protocol of commands like this:
1st byte = House Code, 2nd byte = Light Code, 3rd byte = On or Off (0 for off, 1 for on)
So to turn on the 11th light in my house (house code #3) I'd use:
com.Output = new byte[] {3, 11, 0};


Prefixed Data Packet
This is probably the most common and flexible but requires the most coding. Just prefix your data packet with the length of the data. The prefix must be a fixed size, such as two bytes which would allow a data packet of up to 65,535 bytes. Then the receiver knows how much data is in the packet because it always takes the first two bytes and uses the rest as the data packet.
Example: com.Output = ((char) 00) + ((char) 11) + "Hello World";


langzifengxing 2003-12-05
  • 打赏
  • 举报
回复
想要的话 发信 f5i5j@hotmail.com
rroo 2003-11-14
  • 打赏
  • 举报
回复
推荐一个好的控件,
http://ourworld.compuserve.com/homepages/richard_grier/NETCommOCX.htm
langzifengxing 2003-11-14
  • 打赏
  • 举报
回复
安装VB后可看到组件 使用方法在研究
leiminghui 2003-10-20
  • 打赏
  • 举报
回复
我发现在c#中引入了MSCOMM它不象在VB一样可以看到有一个电话模样的图标,也就是说是可视
的。这个通讯控件不可视没关系。但我要引入大恒图像捕捉卡的控件。我要用它显示图像并且抓拍,兄弟,教我一下如何将我引入的COM在C#的窗体里显示出来。
yanfeng106 2003-10-20
  • 打赏
  • 举报
回复
在网上找找MSComm的用法就知道了,或者去没本书。
lizhenlz 2003-10-20
  • 打赏
  • 举报
回复
如果只使用msncomm ActiveX控件的话.我这里只有vc++.net的代码.主要就分为这几步:(1)首先是初始化串口(2)捕捉串口事件(3)实现串口的收发.这个应该不难的.
wuzhiwen 2003-10-20
  • 打赏
  • 举报
回复
你先装VB,才会出现MSCOMM控件,大哥,
leiminghui 2003-10-20
  • 打赏
  • 举报
回复
各位大侠,不怕你们笑话。我用了。NET的“添加引用”--选择“COM”然后选择了MSCOMM控件。原来不象VB等控件是可视的。现在我犯傻了。比如我要打开串口。这时候我不知道代码怎么下手呀。望各位大侠在见笑之下指点指点!
zglet 2003-10-20
  • 打赏
  • 举报
回复
我也想知道如何实现串口读写的简单方法,并且想交一些朋友,我的EMAIL :agreen001@163.com
colin666 2003-10-17
  • 打赏
  • 举报
回复
网上有很多控件的,微软也有,你到csdn上搜一下,很多的。当然如果你不嫌麻烦你也可以自己用API写
kangzerun 2003-10-17
  • 打赏
  • 举报
回复
用mscomm 吧,把它引用到你的项目中,直接用,一切OK
leiminghui 2003-10-17
  • 打赏
  • 举报
回复
我的通讯是既要监听端口接收指令也要发送指令。大侠,帮个忙。急!!
leiminghui 2003-10-17
  • 打赏
  • 举报
回复
我的E-MAIL:leiminghui@etang.com
leiminghui 2003-10-17
  • 打赏
  • 举报
回复
你们提供一点愿代码给我吗?感激不尽,正急!
再度梦想 2003-10-17
  • 打赏
  • 举报
回复
我刚做一个C#下的串口程序,有一部分是用AIP,有一个是用控件的,与我联系吧,我不知道你是指哪一部分。

well@bdwell.com.cn
zglet 2003-10-17
  • 打赏
  • 举报
回复
我和你遇到的问题相同,你可以使用API去打开端口,把串口当文件进行读写!

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧