用串口调试助手往wince平板上发送数字,结果自动换行是怎么回事?

x87896 2016-03-26 08:05:07
代码如下:
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Microsoft.VisualBasic;
using System.IO;
using System.IO.Ports;
using System.Threading;


namespace SerialPort0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void myDelegate();//这是一个委托,在串口接收事件里用到
public string i;
public int j = 1;
public int n = 0;
public int b = 0;
private void serialPort1_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
//串口接收事件。默认当缓冲区有1个数据时触发此事件,可以修改COM3控件的ReceivedBytesThreshold属性修改
{
Invoke(new myDelegate(updateTextbox), new object[] { });//调用updateTextbox方法
}
public void updateTextbox()
//更新“历史数据”和“当前数据”两个Textbox的文本
{
string i = COM3.ReadExisting();//读取串口3的数据
/*textBox2.Text = i;//当前数据
textBox1.Text += i;//历史数据*/
if (n == 1)//开始写入文件
{
textBox2.Text = i;//当前数据
textBox1.Text += i;//历史数据
FileStream fs = new FileStream("\\NandFlash\\Documents and Settings\\CreateTXT\\" + this.textBox4.Text + ".txt", FileMode.Append);//建立对应的txt,如果是重复的,在原来的基础上往后写
StreamWriter sr = new StreamWriter(fs);
i = COM3.ReadExisting();//读取串口3的数据
sr.WriteLine(textBox2.Text);//写入txt
sr.Close();
fs.Close();
}


}

private void button1_Click(object sender, EventArgs e)//打开串口的按钮,对串口属性进行初始化以及打开串口
{
COM3.BaudRate = Convert.ToInt32(BaudRatebox.Text);//通过一个ComboBox控件设置波特率
COM3.Parity = Parity.None;
COM3.DataBits = 8;//数据有几位
COM3.StopBits = StopBits.One;
COM3.DiscardNull = true;//默认的
COM3.DtrEnable = false;//默认的
COM3.Encoding = Encoding.ASCII;
COM3.RtsEnable = false;//不知道 默认的
//以上是对串口属性初始化
COM3.Open();//打开串口
btnOpenSP.Enabled = false;//打开串口按钮变灰
BaudRatebox.Enabled = false;//波特率选择变灰
label1.Text = "串口已打开!";
}


private void button2_Click(object sender, EventArgs e)//关闭串口的按钮
{
COM3.Close();
timer1.Enabled = false;
timer2.Enabled = false;
btnOpenSP.Enabled = true;
label1.Text = "串口已关闭!";
// label8.Text = "保存结束!";
BaudRatebox.Enabled = true;

}

private void Form1_Load(object sender, EventArgs e)
{
if (!COM3.IsOpen)//这个if没有意义不用写
{
label1.Text = "串口未打开!";
}
BaudRatebox.Text = "9600";//默认波特率是9600

}

private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.ScrollToCaret();//无效,不知道为啥?这个不用管
}

private void button1_Click_1(object sender, EventArgs e)//清空历史数据
{
textBox1.Text = "";
}

private void button2_Click_1(object sender, EventArgs e)
{
n = 1;//保存文件,n=1
label8.Text = "正在保存…";
timer1.Enabled = true;//打开定时器
timer1.Interval = 1000 * Convert.ToInt32(this.textBox3.Text);//设定计时时间,以ms为单位,所以乘1000,输入的数字应该是对应的秒数
timer2.Enabled = true;//打开定时器2
timer2.Interval = 1000;//倒计时,每秒减1
b = Convert.ToInt32(this.textBox3.Text);//读取输入的秒数
}

private void timer1_Tick_1(object sender, EventArgs e)
{
timer1.Enabled = false;//定时时间到,关闭timer1
n = 0;//令n=0,不再写入txt
label8.Text = "保存结束!";
}


private void timer2_Tick(object sender, EventArgs e)
{
do
{
b = b - 1;
this.textBox3.Text = b.ToString();
} while (b < 0);
if (b == 0)
timer2.Enabled = false;//减为0不再减,关闭定时器2
}

private void textBox3_TextChanged(object sender, EventArgs e)
{

}

private void textBox4_TextChanged(object sender, EventArgs e)
{

}

private void label1_ParentChanged(object sender, EventArgs e)
{

}

private void label8_ParentChanged(object sender, EventArgs e)
{

}

}
}
...全文
396 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
热人么你 2016-05-26
  • 打赏
  • 举报
回复
结贴给分吧。在#6楼帮你找出问题了。
热人么你 2016-05-12
  • 打赏
  • 举报
回复
sr.WriteLine(textBox2.Text);//写入txt 就是这句了,WriteLine()会自动加回车换行的 你改成: sr.Write(textBox2.Text);//写入txt
91program 2016-03-31
  • 打赏
  • 举报
回复
LZ,你想怎么去除了? 1) 发送方不要发回车换行; 2) 接收方对接收到的字符串进行处理,删除或者替换其中的回车换行。 两种方法都可以。
x87896 2016-03-30
  • 打赏
  • 举报
回复
引用 3 楼 91program 的回复:
[quote=引用 2 楼 x87896 的回复:] [quote=引用 1 楼 91program 的回复:] 自动?肯定是不会的。 C#不懂 建议你看看接收到的字符串中是否有回车换行,即 0x0D 和 0x0A。 如果没有,再看一个显示的过程
怎么查看有没有回车换行呢?如果有的话怎么办呢?[/quote] 从读取串口的数据开始分析,如:string i = COM3.ReadExisting();//读取串口3的数据,中是否包含回车换行。[/quote] 我看了 确实包含,然后怎么去除呢?
91program 2016-03-28
  • 打赏
  • 举报
回复
引用 2 楼 x87896 的回复:
[quote=引用 1 楼 91program 的回复:] 自动?肯定是不会的。 C#不懂 建议你看看接收到的字符串中是否有回车换行,即 0x0D 和 0x0A。 如果没有,再看一个显示的过程
怎么查看有没有回车换行呢?如果有的话怎么办呢?[/quote] 从读取串口的数据开始分析,如:string i = COM3.ReadExisting();//读取串口3的数据,中是否包含回车换行。
91program 2016-03-27
  • 打赏
  • 举报
回复
自动?肯定是不会的。 C#不懂 建议你看看接收到的字符串中是否有回车换行,即 0x0D 和 0x0A。 如果没有,再看一个显示的过程
x87896 2016-03-27
  • 打赏
  • 举报
回复
引用 1 楼 91program 的回复:
自动?肯定是不会的。 C#不懂 建议你看看接收到的字符串中是否有回车换行,即 0x0D 和 0x0A。 如果没有,再看一个显示的过程
怎么查看有没有回车换行呢?如果有的话怎么办呢?

19,500

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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