C#串口通信 能发数据但接收不到数据 怎么回事? 刚学C#串口编程,从网上下了几个例子测试,但都是能发数据而接收不到数据,代码应该没什么问题,用一台电脑测试的,串口2,3脚串联了,就是接收不到数据,郁闷,哪位高手知道的请帮帮忙吧,谢啦! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System.IO.Ports;using System.Threading;using System.IO;namespace SerialPortDemo{ public partial class Form1 : Form { bool isStar = false; Thread threadReceive = null; SerialPort serialPort = null; int i = 0; int k = 0; public Form1() { InitializeComponent(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 2; serialPort = new SerialPort(); if (serialPort.IsOpen) serialPort.Close(); //获得参数 serialPort.PortName = comboBox1.SelectedItem.ToString(); serialPort.BaudRate = int.Parse(comboBox2.SelectedItem.ToString()); serialPort.Open(); ThreadStart method = new ThreadStart(ReceiveData);//在线程上执行此方法 threadReceive = new Thread(new ThreadStart(method)); } private void button1_Click(object sender, EventArgs e) { try { if (button1.Text == "接收数据") { if (!isStar)//如果isStar等于true,则启动线程 { threadReceive.Start(); this.toolStripStatusLabel1.Text = "正在接收数据......"; } else { threadReceive.Resume();//如果isStar等于false,则解除挂起线程 this.toolStripStatusLabel1.Text = "暂停接收..."; } button1.Text = "停止接收"; } else { threadReceive.Suspend();//如果是"停止接收",则挂起线程 button1.Text = "接收数据"; this.toolStripStatusLabel1.Text = "暂停接收..."; } } catch (Exception ex) { MessageBox.Show(ex.Message); serialPort.Close(); } } //不断循环接收数据 private void ReceiveData() { while (true) { Thread.Sleep(500); this.isStar = true; this.SynReceiveData(); } } //通过串口取数据 private void SynReceiveData() { MessageBox.Show("接收数据" + k); k++; string inputData = serialPort.ReadExisting(); try { if (inputData != string.Empty) { if (inputData.Trim().Length == 45) { SetText(inputData); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } //分隔字符串并保存在指定目录下 private void SetText(string str) { string aa = str; StreamWriter sw = null; if (aa != string.Empty) { string aaa = aa.Insert(13, ","); aaa = aaa.Insert(32, ","); String fileName = "C:\\GAJ_PUB\\kaoqin"; if (Directory.Exists(fileName) == false)//如果目录不存在 { Directory.CreateDirectory(fileName);//创建目录 sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt");//创建文本 } else { sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt"); } sw.WriteLine(aaa); sw.Flush(); sw.Close(); i++; MessageBox.Show("文件已存入指定目录下!!"); } } private void button2_Click(object sender, EventArgs e) { if (threadReceive.ThreadState==ThreadState.Running)//当前状态为启动线程 { threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.Unstarted) { this.Close(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (threadReceive.ThreadState == ThreadState.Running)//当前状态为启动线程 { threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } } }} 既然你说能发出数据,为什么23对连后会收不到数据呢,你可以用串口调试工具检测你的com口,查看是否真的你发出了数据 楼上的能不能把源码发给我啊,邮箱 [email protected],谢了 有一个串口调试工具 accessport133 可以在不打开com口的情况下监视你发出和收到的信息可以去试试看 如果你所说的是事实,你可以先检查你指定的com口,如果com口没问题 你可以换根线了 请教一个关于ListViewItem的问题 写完程序了 要打包了,请教下打包的问题 关于获得进程return值的问题 关于virtual和override怎么用的问题 在cs类文件中,更新语句应该返回什么值 ?顶帖也送分啦 如何存储checkedlistbox内容 急救!!!this.ResumeLayout(false);这句出错,提示 找不到指定模块是什么情况? VS2005如何进行网络化设置 如何让textbox屏蔽Ctrl C操作? 问一个白痴问题,给看看 文档的读取、反编译及写入 WinForm中DataGrid控件排序问题
using System.Threading;
using System.IO;namespace SerialPortDemo
{
public partial class Form1 : Form
{
bool isStar = false;
Thread threadReceive = null;
SerialPort serialPort = null;
int i = 0;
int k = 0; public Form1()
{
InitializeComponent();
this.comboBox1.SelectedIndex = 0;
this.comboBox2.SelectedIndex = 2; serialPort = new SerialPort();
if (serialPort.IsOpen)
serialPort.Close();
//获得参数
serialPort.PortName = comboBox1.SelectedItem.ToString();
serialPort.BaudRate = int.Parse(comboBox2.SelectedItem.ToString());
serialPort.Open(); ThreadStart method = new ThreadStart(ReceiveData);//在线程上执行此方法
threadReceive = new Thread(new ThreadStart(method));
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (button1.Text == "接收数据")
{
if (!isStar)//如果isStar等于true,则启动线程
{
threadReceive.Start();
this.toolStripStatusLabel1.Text = "正在接收数据......";
}
else
{
threadReceive.Resume();//如果isStar等于false,则解除挂起线程
this.toolStripStatusLabel1.Text = "暂停接收...";
}
button1.Text = "停止接收";
}
else
{ threadReceive.Suspend();//如果是"停止接收",则挂起线程
button1.Text = "接收数据";
this.toolStripStatusLabel1.Text = "暂停接收...";
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
serialPort.Close(); } }
//不断循环接收数据
private void ReceiveData()
{
while (true)
{
Thread.Sleep(500);
this.isStar = true;
this.SynReceiveData();
}
} //通过串口取数据
private void SynReceiveData()
{
MessageBox.Show("接收数据" + k);
k++;
string inputData = serialPort.ReadExisting();
try
{
if (inputData != string.Empty)
{
if (inputData.Trim().Length == 45)
{
SetText(inputData);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} } //分隔字符串并保存在指定目录下
private void SetText(string str)
{
string aa = str;
StreamWriter sw = null; if (aa != string.Empty)
{
string aaa = aa.Insert(13, ",");
aaa = aaa.Insert(32, ","); String fileName = "C:\\GAJ_PUB\\kaoqin";
if (Directory.Exists(fileName) == false)//如果目录不存在
{
Directory.CreateDirectory(fileName);//创建目录
sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt");//创建文本
}
else
{
sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt");
}
sw.WriteLine(aaa); sw.Flush(); sw.Close();
i++;
MessageBox.Show("文件已存入指定目录下!!");
} } private void button2_Click(object sender, EventArgs e)
{
if (threadReceive.ThreadState==ThreadState.Running)//当前状态为启动线程
{
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.Unstarted)
{
this.Close();
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (threadReceive.ThreadState == ThreadState.Running)//当前状态为启动线程
{
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
}
}
}
可以去试试看