C#中的串口通信 [英] Serial Port Comunication in C#

查看:93
本文介绍了C#中的串口通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是我第一次在这里发帖,每个人都告诉我这是寻求帮助的最佳地点..

我正在用C#制作一个项目( Windows Forms)关于智能房屋的面部识别,指纹探测器,报警系统和访问DVR(视频摄像头管理器)等等。



问题是我自己制作了一个电子接口,它从报警器的传感器读取并使用串行端口(USB适配器)发送强电信号。 (例如,要打开房间里的灯,空调,或者从闹钟的传感器读取数据)

我曾经使用过 VB6 我将在另一个项目中给你的代码..

它是使用 MSCOMM32.OCX 库制作的,我没有制作代码,但是我明白了..

事情就是我需要使用现在在.NET中使用的Serial 组件来转换这个代码,它具有不同的属性,我可以''这是VB6中的代码:

Hello, It''s the first time I make a post here, everyone tells me that is the best place to ask for help..
I''m making a project in C# (Windows Forms) about an intelligent house with facial recognition, fingerprints detector, an alarm system and access to the DVR (videocameras manager) and stuff like that.

The problem is that I have an electronical interface that I made myself which reads from the alarm''s sensors and sends strong electrical signals using the Serial Port (USB Adapter). (For example, to turn on the lights in a room, the air conditioning, or reading data from the sensor of the alarm)
I used to have it working with a VB6 code that I''ll give you next in another project..
It''s made using the MSCOMM32.OCX library, I didn''t make the code, but I understand it..
The thing is that I need to convert this code using the Serial Component used nowadays in .NET which has different properties that I can''t assimilate.

This is the code in VB6:

Private Sub Form_Load()
//MScomm Configuration.
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 0
MSComm1.Handshaking = comNone
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.DTREnable = True
MSComm1.PortOpen = True

outputs = 0
Text1.Text = outputs
MSComm1.Output = Chr(&H31)
MSComm1.Output = Chr(outputs)

command= &H37
MSComm1.Output = Chr(command)
Timer1.Enabled = True
index = &H80
Text2.Text = 253
Text3.Text = 200
Text4.Text = 400

End Sub







Private Sub MSComm1_OnComm()

If MSComm1.CommEvent = comEvReceive Then
input = MSComm1.Input
 ......
End Sub





我只需要知道什么是类似的事件和属性来配置串行组件..

这将是非常有用的..

先谢谢,卢卡斯。



I just need to know what are the similar events and properties to configurate the Serial component..
It would be REALLY helpfull..
Thanks in Advance, Lucas.

推荐答案

这里的SerialPort MSDN文档:

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx [ ^ ]



SerialPort MSDN documentation here :
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^]

SerialPort comm = new SerialPort("COM1");
// 9600,n,8,1
comm.BaudRate = 9600;
comm.Parity = Parity.None;
comm.DataBits = 8;
comm.StopBits = StopBits.One;
// handShaking
comm.Handshake = Handshake.None;

comm.DtrEnable = true;

comm.Open();



使用comm.DataReceived事件在收到数据时通知,并且comm.Read读取它。



使用comm.Write写入数据。


use comm.DataReceived event to be notified when data are received, and comm.Read to read it.

use comm.Write to write data.


这篇关于C#中的串口通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆