C#串口通信 能发数据但接收不到数据 怎么回事?

lhllucky 2008-09-18 11:08:08
刚学C#串口编程,从网上下了几个例子测试,但都是能发数据而接收不到数据,代码应该没什么问题,用一台电脑测试的,串口2,3脚串联了,就是接收不到数据,郁闷,哪位高手知道的请帮帮忙吧,谢啦!
...全文
946 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhllucky 2008-09-18
  • 打赏
  • 举报
回复
谢谢大家的建议,可能是线没连接好的问题了,呵呵,怎么分啊,分太少了
gongkexin 2008-09-18
  • 打赏
  • 举报
回复
如果你所说的是事实,你可以先检查你指定的com口,如果com口没问题 你可以换根线了
oneatree 2008-09-18
  • 打赏
  • 举报
回复
应该是 机器上还有其他的模拟串口打开了 所以接受数据的串口号可能不对


lhllucky 2008-09-18
  • 打赏
  • 举报
回复
我用串口调试工具试了,也没收到,是哪的问题啊
gongkexin 2008-09-18
  • 打赏
  • 举报
回复
代码我没有的,因为我没遇到过这样的问题,一半23角对连是用来测试线是否有问题的
gongkexin 2008-09-18
  • 打赏
  • 举报
回复
有一个串口调试工具 accessport133 可以在不打开com口的情况下监视你发出和收到的信息
可以去试试看
jacketl 2008-09-18
  • 打赏
  • 举报
回复
一楼的代码?
slimfeng 2008-09-18
  • 打赏
  • 举报
回复
你先用串口调试助手(如果没有,从网上下一个)测试一下,互发数据,如果有问题可能串口有问题,如果没有问题再查程序
wdgphc 2008-09-18
  • 打赏
  • 举报
回复
你同一个串口的2,3脚连上后,只能打开一次串口.
但是,你没用收到数据,怎么能确认发出了呢?
lhllucky 2008-09-18
  • 打赏
  • 举报
回复
楼上的能不能把源码发给我啊,邮箱 lhllucky666@163.com,谢了
gongkexin 2008-09-18
  • 打赏
  • 举报
回复
既然你说能发出数据,为什么23对连后会收不到数据呢,你可以用串口调试工具检测你的com口,查看是否真的你发出了数据
hongqi162 2008-09-18
  • 打赏
  • 举报
回复
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();
}
}


}
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧