我无法将字符串发送到串行电缆 [英] I could not send a string to serial cable

查看:44
本文介绍了我无法将字符串发送到串行电缆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow
i have connect two computers with serial cabel
in computer A ,i use the below program to send a string to computer B
but i could not
those notes might be helpfull for you
1- i have com1 only in both computers
2- the serial cable ends in femal shapes,another word i have used 
the serial cable not any otherone
when i got your fine solution i will ask for more question regarding the serialport 
program so be aware.





我的尝试:





What I have tried:

//below are both programs that was in computer A and B
// in computer A(the sender)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.IO.Ports;

namespace MondaySerial
{
    public partial class Form1 : Form
    {
        static SerialPort sp = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }
        //--------------------------------------------------------------
        private void FormLoad(object sender, EventArgs e)
        {
            sp.PortName = "com1";
            //sp.PortName = "com2";
            sp.BaudRate = 9600;
            sp.DataBits = 8;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.Handshake = Handshake.None;
            //
            sp.ReadTimeout = 5000;// Timeout.Infinite;//500;
            sp.Open();
        }
        //-------------------------------------------------------
        protected override void OnClosing(CancelEventArgs e)
        {
            DialogResult result;
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
                buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            if (result == DialogResult.Yes)
            {
                sp.Close();
                //-------------------
                //ButtonForm1.ActiveForm.Close();
                //this.Close();
                Application.Exit();
            }//Yes
            else e.Cancel = true;
        }
        //---------------------------------------------------------
        private void SendStringClick(object sender, EventArgs e)
        {
            sp.WriteLine(textBox2.Text);
        }
        //----------------------------------------------------------
    }
}

// and in computer B (the receiver )

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

namespace MondaySerial
{
    public partial class Form1 : Form
    {
        static SerialPort sp = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }
        //--------------------------------------------------------------
        private void FormLoad(object sender, EventArgs e)
        {
            sp.PortName = "com1";
            //sp.PortName = "com2";
            sp.BaudRate = 9600;
            sp.DataBits = 8;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.Handshake = Handshake.None;
            //
            sp.ReadTimeout = 5000;// Timeout.Infinite;//500;
            sp.Open();
        }
        //-------------------------------------------------------
        protected override void OnClosing(CancelEventArgs e)
        {
            DialogResult result;
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            result = MessageBox.Show(this, "Are You Sure Want to exit", " Answer Yes or No",
                buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                MessageBoxOptions.RightAlign);
            if (result == DialogResult.Yes)
            {
                sp.Close();
                //-------------------
                //ButtonForm1.ActiveForm.Close();
                //this.Close();
                Application.Exit();
            }//Yes
            else e.Cancel = true;
        }
        //---------------------------------------------------------
        private void ReadStringClick(object sender, EventArgs e)
        {
			textBox2.Text =  sp.ReadLine();
        }
        //----------------------------------------------------------
    }
}

推荐答案

引用:

但我不能





这是什么意思?怎么了 ??你有错误???你有没有用调试器完成你的程序(如果是的话,发生了什么,如果没有,为什么不是)????



你说你有一根串行电缆 - 好吧,你建造它还是买它?无论哪种方式,您确定您拥有所谓的零调制解调器电缆吗?



在接收端,假设有数据,那么使用DataReceived事件处理程序不仅仅是从端口读取更好吗?

<最后......
最后...... System.IO.Ports的C#实现还有很多不足之处 - 它的'buggy' - 网上有很多修复/抽象来绕过它的怪癖



what does that mean ? what happens ?? do you get an error ??? have you stepped through your program with a debugger (if so, what happened, if not, WHY NOT) ????

you say you have a serial cable - ok, did you build it or buy it ? either way, are you sure you have what is known as a 'null modem' cable ?

On the receiving side, wouldnt using the DataReceived Event Handler be better than just reading from the port assuming there is data there ?

finally .. the C# Implementation of System.IO.Ports leaves a lot to be desired - its 'buggy' - the net is full of fixes/abstractions to get around its quirks


这篇关于我无法将字符串发送到串行电缆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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