C#串口发送文件出现乱码
我做了一个串口调试软件,在发送文件时总是出现乱码,求大神帮助,急急急!!!//导入文件数据源
string fileData = string.Empty;
private void cbx_fileSend_CheckedChanged(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
MessageBox.Show("请先打开串口");
return;
}
else if (cbx_fileSend.Checked)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (openFile.ShowDialog() == DialogResult.OK)
{
string fileName = openFile.FileName;
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{
while (!sr.EndOfStream)
{
fileData = sr.ReadLine();
}
}
txt_send.Text = "文件 " + fileName + " 已经被导入";
txt_send.Enabled = false;
}
}
else
{
txt_send.Enabled = true;
txt_send.Text = "";
}
}
//发送文件
private void sendFile()
{
byte[] wtiteBytes = Encoding.Default.GetBytes(fileData);
serialPort1.Write(wtiteBytes, 0, wtiteBytes.Length);
}
//接收数据事件
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] readByte = new byte[serialPort1.BytesToRead];
serialPort1.Read(readByte, 0, readByte.Length);
string s = Encoding.Default.GetString(readByte);
this.txt_receive.Text=s;
}
为什么这种方法接收到的数据是乱码??直接在发送框里输入汉字可以正常发送,却不能发送文件。试过了将编码改成unicode UTF8格式,都是乱码。