从串口显示文本 [英] display text from serial port

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

问题描述

你好



i我试图从串口接收和显示数据

i我收到开放端口的消息但是没有得到txt是什么我我从我的手机发送。我有加密狗连接到电脑并从手机发送短信



提前感谢





 使用 System.Collections.Generic; 
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.IO.Ports;
使用 System.IO;
使用 System.Windows.Forms;
使用 System.Globalization;
使用 System.Threading;
使用 System.Resources;


命名空间 WindowsFormsApplication1
{
public 部分 class Form1:表格
{

public Form1()
{

InitializeComponent();
if (!mySerialport.IsOpen)
{
mySerialport.Open();
tbRX.Text = port opened :);
}
else {
tbRX.Text = port busy :(;

}
}
private string rxstring;

private void Form1_Load( object sender,EventArgs e)
{

}


private void mySerialport_DataReceived( object sender,SerialDataReceivedEventArgs e)
{
rxstring = mySerialport.ReadExisting();
this .Invoke( new EventHandler(dsi playTEXT));
}
private void dsiplayTEXT( object o,EventArgs e)
{
tbRX.AppendText(rxstring);

}
}
}

解决方案

请参考读写方法串行端口中的文本: https:// msdn .microsoft.com / zh-cn / library / system.io.ports.serialport_methods(v = vs.110).aspx [ ^ ]


你不能只打开串口并期望收到消息!

你必须使用正确的AT命令告诉移动设备做什么: AT命令及其用途简介 [ ^ ]



你做到了不仅仅是将文本附加到DataReceived事件中的文本框 - 它发生在不同的线程上,因此您将尝试获得交叉线程异常。你需要调用控件来设置它的文本属性。


你的问题是2个完全不相关的问题:

- 在串口上与你的设备通信。 />
- 显示内容。



对于串口上的设备,您需要了解设备是什么以及如何与设备通信。

Hello

i am trying to receive and display data from serial port
i am getting message of open port but not getting txt what i am sending from my mobile . I have dongle connected to pc and sending sms from mobile

thanks in advance


using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using System.Resources;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
           
            InitializeComponent();
            if (!mySerialport.IsOpen)
            {
                mySerialport.Open();
                tbRX.Text = "port opened   :)";
            }
            else {
                tbRX.Text = "port busy :(";           
                       
            }
        }
        private string rxstring;
   
        private void Form1_Load(object sender, EventArgs e)
        {
        
        }
       

        private void mySerialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            rxstring = mySerialport.ReadExisting();
            this.Invoke(new EventHandler(dsiplayTEXT));
        }
        private void dsiplayTEXT(object o, EventArgs e)
        {
            tbRX.AppendText(rxstring);        
        
        }
    }
}

解决方案

Please refer methods for reading and writing text in serial port here: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport_methods(v=vs.110).aspx[^]


You can't just open the serial port and expect to receive messages!
You have to use the right AT Commands to tell the mobile what to do: Introduction to AT commands and its uses[^]

And you don't just append text to a text box in the DataReceived event either - it happens on a different thread, so you will get a cross threading exception is you try. You need to Invoke the control to set it's text property.


Your question is 2 completely unrelated questions:
- Communicate with your device on serial port.
- display something.

For your device on serial port, you need to understand what is the device and how to communicate with it.


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

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