C#串口通信 [英] C# Serial Port Communication

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

问题描述

您好,我从插入自制红外接收器的串口读取时遇到问题。 IR Receiver使用lirc在Linux上工作,使用WinLirc在Windows上工作,因此接收器不是问题,也不是遥控器。

我尝试了很多方法。搜索结果1。处理程序:它们永远不会被调用;
2。 SerialPort Read方法,"BytesToRead"和"BytesToRead"。始终为0,方法始终返回0
3。 BaseStream方法,在这里发布,永远不会返回任何内容。
4。试图将DtrEnable设置为true,仍然没有。
5。尝试了8个数据位,仍然没有。

这里发布的My Read()方法在一个线程中被调用,所以它运行时没有停止。那个成功的消息框永远不会弹出。

那么,有人可以发布一个例子,一个工作的例子,用于从串口读取红外信号吗?
网上的所有例子主要是针对文本数据的连续,没有一个工作。难道我做错了什么?

我正在使用.NET System.IO.Ports SerialPort类。我知道有p / invoke方法,但我真的不想使用它。搜索结果帮助!

编辑:刚刚注意到Windows日志中的内容:
更正了发生硬件错误。

错误来源:更正机器检查

错误类型:内存层次结构错误

处理器ID有效:是
处理器ID:0x0
银行编号:2 < br>交易类型:通用
处理器参与:N / A
请求类型:通用读取

是否可以阻止我的读取?我正在运行Windows Server 2008

我的设置是这样的:


Hello, i have a problem with reading from the serial port where i have plugged in a homebrew IR Receiver. The IR Receiver worked on Linux with lirc, works on windows with WinLirc, so the receiver isn't the problem , nor is the remote.

I've tried many many ways.

1. Handlers : they never get called;
2. the SerialPort Read method , "BytesToRead" is always 0, method always returns 0
3. BaseStream method, wich is posted down here, never returns anything.
4. Tried to set DtrEnable to true, still nothing.
5. Tried 8 data bits, still nothing.

My Read() method posted here gets called in a thread, so it runs without stopping. That success message box never pops up.

So, can somebody post an example , a working one, for reading infrared signals from serial please?
All the examples on the net are mostly for text data over the serial, and none of them work. Am i doing something wrong?

I'm using the .NET System.IO.Ports SerialPort class. I know there is the p/invoke method too but i don't really want to use that.

Help!

just noticed something in the windows logs:
A corrected hardware error occurred. 

Error Source: Corrected Machine Check

Error Type: Memory Hierarchy Error

Processor ID Valid: Yes
Processor ID: 0x0
Bank Number: 2
Transaction Type: Generic
Processor Participation: N/A
Request Type: Generic Read

Could it be something that prevented my read? i'm running windows server 2008

My setup is like this:

private void StartSignalRead()
{
serialPort = new SerialPort(" COM1",115200);
//serialPort.BaudRate = 9600;
serialPort.ReadTimeout = 500;
serialPort.Parity = Parity.None;
serialPort.DataBits = 7;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
serialPort.DtrEnable = false;


serialPort.DataReceived + = new SerialDataReceivedEventHandler(SignalReceived);
serialPort.ErrorReceived + = new SerialErrorReceivedEventHandler(ErrorReceived);

serialPort.Open();

if(serialPort.IsOpen)
{
signalsForm.AddText("COM is Open!\\\\ nn");



st = serialPort.BaseStream;
}

private void SignalReceived(object sender,SerialDataReceivedEventArgs e)
{
signalsForm.AddText(serialPort.ReadExisting());
}

void void ErrorReceived( object sender,SerialErrorReceivedEventArgs e)
{
MessageBox.Show(" Received error:" );
}


public void Read()

    private void StartSignalRead()
        {
            serialPort = new SerialPort("COM1",115200);
            //serialPort.BaudRate = 9600;
            serialPort.ReadTimeout = 500;
            serialPort.Parity = Parity.None;
            serialPort.DataBits = 7;
            serialPort.StopBits = StopBits.One;
            serialPort.Handshake = Handshake.None;
            serialPort.DtrEnable = false;
           
           
            serialPort.DataReceived +=new SerialDataReceivedEventHandler(SignalReceived);
            serialPort.ErrorReceived+=new SerialErrorReceivedEventHandler( ErrorReceived);
           
            serialPort.Open();
           
            if(serialPort.IsOpen)
            {
                signalsForm.AddText("COM is Open!\r\n");
            }
           
            st = serialPort.BaseStream;
        }
       
        private void SignalReceived(object sender, SerialDataReceivedEventArgs e)
        {
            signalsForm.AddText(serialPort.ReadExisting());
        }
       
        private void ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            MessageBox.Show("Received error:" );
        }
       
           
         public void Read()
{

推荐答案

IR接收器是否将结果作为普通字节流发送?我认为这样的接收器通常只是摆动DSR(或另一条控制线)来匹配输入信号。您可以查看硬件或Lirc文档来查找。

Does the IR receiver send the result as a normal stream of bytes?  I thought that such receivers often instead just wiggled the DSR (or another control line) to match the incoming signal.  Can you check with the hardware or with the Lirc documentation to find out.


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

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