无法通过串行电缆发送或接收字符串 [英] Could not send or receive string through serial cable

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

问题描述

Hellow

i连接两台带串口电缆的电脑

在电脑A中,我使用以下程序将字符串发送到电脑B

但我不能

这些笔记可能对你有所帮助

1-我只在两台电脑上都有com1

2-串口线以女性形状结束,我用过的另一个词

正确的串行电缆而不是任何其他人



当我得到你的精细解决方案时我会问有关serialport

程序的更多问题请注意。

//以下是计算机A和B中的程序

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 right serial cable not any otherone

when i got your fine solution i will ask for more question regarding the serialport
program so be aware.
//below are both programs that was in computer A and B






我的尝试:





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();
        }
        //----------------------------------------------------------
    }
}

推荐答案

从Hyperterminal或类似开始 - 在两台计算机上运行它并确保你输入的内容每个都可以在另一个看到。直到你有这个工作,你有太多的变量来开始调试代码。

问题是所有串行电缆都不相同:一些交叉(需要),一些不要T。有些提供流量控制线,有些则没有。等等。如果你不能用已知的工作软件在它们之间进行交谈,那么你就不能做太多了!

如果PC到PC无法正常工作,请拔掉一端,然后用回形针做空将电缆的引脚2连接至引脚3,然后在连接的PC上再试一次 - 您应该看到所发送的类型(这是一个简单的环回电缆,它对测试非常有用)。

你有Hyperterminal工作 - 我不能为你做 - 然后开始使用调试器检查这两个应用程序是否正常工作。
Start with Hyperterminal or similar - run it on both computers and make sure that what you type at each can be seen at the other. Until you have that working, you have far too many variables to start debugging code.
The problem is that all "serial cables" aren't equal: some cross over (which is needed), some don't. Some provide flow control wires, some don't. And so on. If you can't talk between them with known working software, then you can't do much at all!
If PC to PC doesn't work, unplug one end, and use a paperclip to short pin 2 to pin 3 of the cable, and try again on the connected PC - you should see what you type being sent back (this is a simple loopback cable, and it's useful for testing).
When you have Hyperterminal working - and I can't do that for you - then start using the debugger to check if the two applications are working.


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

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