通过串口C#发送文件 [英] Sending file over serial port C#

查看:481
本文介绍了通过串口C#发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家



我正在通过串口向我的Telit调制解调器发送编译的python文件(.pyo扩展名)。调制解调器有一个内置的python引擎。



我正在编写一个C#应用程序,以便于将编译文件下载到调制解调器。我可以使用超级终端中的发送文本文件方法通过COM9将文件成功发送到调制解调器但是我无法通过C#执行此操作。



是否发送文本文件方法通过串口发送时将文件转换为原始二进制数据或字节数组?



使用串口类我遵循这种方法:



 / /使用文件流打开文件
FileStream fileStream = new FileStream(@c:\ test.pyo,FileMode.Open,FileAccess.Read,FileShare.Read))

/ /将打开的文件存储为二进制
BinaryReader binary = new BinaryReader(fileStream,Encoding.GetEncoding(28591));

//此时我写入端口
port.Write(binary.ReadBytes((int)fileStream.Length),0,(int)fileStream.Length);

这是发送文本文件的正确方法吗?

解决方案

你好;

您可以尝试阅读该文件:



 public byte [] ReadByteArrayFromFile(string fileName)
{
byte [] buff = null;
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int)numBytes);
返回buff;
}





或:

 使用 System.IO; 

private static void SendTextFile(SerialPort端口,字符串 FileName)
{port.Write(File.OpenText(FileName).ReadToEnd()); }

private static void SendBinaryFile(SerialPort端口,字符串 FileName)
{
使用 (FileStream fs = File.OpenRead(FileName))
port.Write(( new BinaryReader(fs))。ReadBytes(( int )fs.Length), 0 ,( int )fs。长度);
}







和更多detials尝试链接:

http://www.dreamincode.net/forums/topic / 35775-serial-port-communication-in-c%23 / [ ^ ]



问候..


< blockquote>更改SerialPort的编码。,,


我不知道关于.net的任何内容。


Hi Guys

I am sending a compiled python file (.pyo extension) over the serial port to my Telit modem. The modem has a built in python engine.

I am writing a C# application to facilitate the downloading of compiled files to the modem. I can successfully send the file to the modem over COM9 using the "send text file" method in hyper terminal but I cannot do this from C#.

Does the "send text file" method convert the file to raw binary data or a byte array when sending over the serial port??

Using the serial port class I have followed this approach:

//open the file using file stream
FileStream fileStream = new FileStream(@"c:\test.pyo", FileMode.Open, FileAccess.Read, FileShare.Read))

//store the open file as binary
BinaryReader binary = new BinaryReader(fileStream , Encoding.GetEncoding(28591));

//at this point I write to the port
port.Write(binary.ReadBytes((int) fileStream.Length), 0, (int) fileStream.Length);

Is this the right approach for sending a text file?

解决方案

Hello ;
you can try that for reading the file :

public byte[] ReadByteArrayFromFile(string fileName)
{
byte[] buff = null;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int)numBytes);
return buff;
}



or :

using System.IO;

private static void SendTextFile(  SerialPort port, string FileName)
{ port.Write(File.OpenText(FileName).ReadToEnd()); }

private static void SendBinaryFile(  SerialPort port, string FileName)
{
  using (FileStream fs = File.OpenRead(FileName))
    port.Write((new BinaryReader(fs)).ReadBytes(      (int)fs.Length), 0, (int)fs.Length);
}




and for more detials try that links :
http://www.dreamincode.net/forums/topic/35775-serial-port-communication-in-c%23/[^]

regards..


Change the Encoding of your SerialPort.,,


i dont know anythng about .net.


这篇关于通过串口C#发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆