用rs232串口线交叉连接两台电脑测试接收不到值 代码贴出来 各位前辈 看看
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.IO.Ports;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ }
//初始化SerialPort对象方法.PortName为COM口名称,例如"COM1","COM2"等,注意是string类型
public void InitCOM(string PortName)
{
serialPort1 = new SerialPort(PortName);
serialPort1.BaudRate = 9600;//波特率
serialPort1.Parity = Parity.None;//无奇偶校验位
serialPort1.StopBits = StopBits.Two;//两个停止位
serialPort1.Handshake = Handshake.RequestToSend;//控制协议
serialPort1.ReceivedBytesThreshold = 4;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数 }
//打开串口的方法
public void OpenPort()
{
try
{
serialPort1.Open();
}
catch { }
if (serialPort1.IsOpen)
{
Console.WriteLine("the port is opened!");
}
else
{
Console.WriteLine("failure to open the port!");
}
} //关闭串口的方法
public void ClosePort()
{
serialPort1.Close();
if (!serialPort1.IsOpen)
{
Console.WriteLine("the port is already closed!");
}
}
private void button1_Click(object sender, EventArgs e)
{
InitCOM("COM3");
OpenPort(); byte[] data = Encoding.Unicode.GetBytes(this.textBox1.Text);
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
ClosePort();
MessageBox.Show(this, "利用串口成功发送数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//DataReceived事件委托方法
private void port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
StringBuilder currentline = new StringBuilder();
//循环接收数据
while (serialPort1.BytesToRead > 0)
{
char ch = (char)serialPort1.ReadByte();
currentline.Append(ch);
}
//在这里对接收到的数据进行处理
//
currentline = new StringBuilder();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
} private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] datar = Convert.FromBase64String(serialPort1.ReadLine());
textBox2.Text = Encoding.Unicode.GetString(datar);
ClosePort();
MessageBox.Show(this, "利用串口成功接收数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
ClosePort();
MessageBox.Show(this, "利用串口接收数据失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
C# SerialPort串口通信
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.IO.Ports;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ }
//初始化SerialPort对象方法.PortName为COM口名称,例如"COM1","COM2"等,注意是string类型
public void InitCOM(string PortName)
{
serialPort1 = new SerialPort(PortName);
serialPort1.BaudRate = 9600;//波特率
serialPort1.Parity = Parity.None;//无奇偶校验位
serialPort1.StopBits = StopBits.Two;//两个停止位
serialPort1.Handshake = Handshake.RequestToSend;//控制协议
serialPort1.ReceivedBytesThreshold = 4;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数 }
//打开串口的方法
public void OpenPort()
{
try
{
serialPort1.Open();
}
catch { }
if (serialPort1.IsOpen)
{
Console.WriteLine("the port is opened!");
}
else
{
Console.WriteLine("failure to open the port!");
}
} //关闭串口的方法
public void ClosePort()
{
serialPort1.Close();
if (!serialPort1.IsOpen)
{
Console.WriteLine("the port is already closed!");
}
}
private void button1_Click(object sender, EventArgs e)
{
InitCOM("COM3");
OpenPort(); byte[] data = Encoding.Unicode.GetBytes(this.textBox1.Text);
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
ClosePort();
MessageBox.Show(this, "利用串口成功发送数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//DataReceived事件委托方法
private void port1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
StringBuilder currentline = new StringBuilder();
//循环接收数据
while (serialPort1.BytesToRead > 0)
{
char ch = (char)serialPort1.ReadByte();
currentline.Append(ch);
}
//在这里对接收到的数据进行处理
//
currentline = new StringBuilder();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
} private void button2_Click(object sender, EventArgs e)
{
try
{
byte[] datar = Convert.FromBase64String(serialPort1.ReadLine());
textBox2.Text = Encoding.Unicode.GetString(datar);
ClosePort();
MessageBox.Show(this, "利用串口成功接收数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
ClosePort();
MessageBox.Show(this, "利用串口接收数据失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
C# SerialPort串口通信
你必须打开接收数据的那个串口 并且设置这个串口的port1_DataReceived处理函数
倒是那个发送数据的串口只需要打开,然后发送数据即可