상세 컨텐츠

본문 제목

텍스트, 바이너리 파일을 시리얼 포트로 전송

C#

by xarfox 2010. 8. 12. 10:07

본문

Here are two helpful little methods for sending files through the serial port. Of course, these are the bare essentials and as always, you should check to make sure the port is open first (port.IsOpen) and use try/catch around trying to open a file, but you get the gist with this code. The binary sending routine is limited to about 2GB (the size of an int), but this should be okay for most uses.

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); }

관련글 더보기