C # Serial Communication-file transfer test,
Note: This program may not be practical and can only be used for testing.
1. Use virtual serial port tool VSPD to virtualize two serial ports COM1 and COM2
Ii. Conventions
The Code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; namespace COMClient {// <summary> // Convention // </summary> public enum Protocol {Client sending file name = 0, the Client sends data blocks = 1, the Client sends the last data block = 2, the Server receives the data blocks = 3, and the Server ends with 4 }}View Code
Iii. Function Description:
The COMClient program listens to the COM1 serial port and the COMServer program listens to the COM2 serial port. The COMClient selects and sends the file first, and the COMServer receives the file. Data is sent multiple times. The data sent each time is determined by the first byte, and the subsequent byte is the data itself.
4. Send a file to the COMClient Client
Code:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. IO. ports; using System. IO; using System. threading; namespace COMClient {public partial class Form1: Form {# region variable // <summary> // serial port resource // </summary> private static SerialPort serialPort = null; /// <summary> /// File //// </summary> private static FileStream fs = null; private static long index = 0; private static long blockCount; private static int size = 4095; private static DateTime dt; # endregion # region Form1 public Form1 () {InitializeComponent () ;}# endregion # region Form1_Load private void Form1_Load (object sender, EventArgs e) {serialPort = new SerialPort ("COM1"); serialPort. dataReceived + = ne W SerialDataReceivedEventHandler (serialPort_DataReceived); serialPort. open () ;}# endregion # region btnConn_Click private void btnConn_Click (object sender, EventArgs e) {if (openFileDialog1.ShowDialog () = DialogResult. OK) {dt = DateTime. now; fs = new FileStream (openFileDialog1.FileName, FileMode. open, FileAccess. read); blockCount = (fs. length-1)/size + 1; List <byte> bList = new List <byte> (); BList. add (int) Protocol. the Client sends the file name); bList. addRange (ASCIIEncoding. UTF8.GetBytes (openFileDialog1.FileName); byte [] bArr = bList. toArray (); serialPort. write (bArr, 0, bArr. length) ;}# endregion /// <summary> // receives Serial Data Events /// </summary> public void serialPort_DataReceived (object sender, SerialDataReceivedEventArgs e) {if (serialPort. bytesToRead = 0) return; int bt = serialPort. readByte (); swit Ch (bt) {case (int) Protocol. Server end received this time: if (index! = BlockCount-1) {byte [] bArr = new byte [size + 1]; bArr [0] = (int) Protocol. the Client sends data blocks; fs. read (bArr, 1, size); index ++; Thread. sleep (1); serialPort. write (bArr, 0, bArr. length);} else {byte [] bArr = new byte [fs. length-(size * index) + 1]; bArr [0] = (int) Protocol. the Client sends the last data block; fs. read (bArr, 1, bArr. length-1); index ++; serialPort. write (bArr, 0, bArr. length);} break; case (int) Protocol. server end: index = 0; double totalSeconds = DateTime. now. subtract (dt ). totalSeconds; MessageBox. show ("completed, time consumed" + totalSeconds + "seconds, speed" + (fs. length/1024.0/totalSeconds ). toString ("#. 0 ") +" KB/S "); fs. close (); fs. dispose (); break ;}}}}View Code
V. COMServer receives files
Code:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. IO. ports; using System. IO; namespace COMServer {public partial class Form1: Form {# region variable // <summary> // serial port resource // </summary> private static SerialPort serialPort = null; /// <summary> /// file /// </summary> private static FileStream fs = null; # endregion # region Form1 public Form1 () {InitializeComponent ();} # endregion # region formparts load private void formparts load (object sender, EventArgs e) {serialPort = new SerialPort ("COM2"); serialPort. dataReceived + = new SerialDataReceivedEventHandler (serialPort_DataReceived); serialPort. open () ;}# endregion /// <summary> // receives Serial Data Events /// </summary> public void serialPort_DataReceived (object sender, SerialDataReceivedEventArgs e) {if (serialPort. bytesToRead = 0) return; # region receives data int bt = serialPort. readByte (); List <byte> bList = new List <byte> (); while (serialPort. bytesToRead> 0) {byte [] bArr = new byte [serialPort. bytesToRead]; serialPort. read (bArr, 0, bArr. length); bList. addRange (bArr);} byte [] receiveData = bList. toArray (); # endregion switch (bt) {case (int) Protocol. client Sending File Name: string path = ASCIIEncoding. UTF8.GetString (receiveData); string fileName = Path. getFileName (path); fs = new FileStream (@ "d: \ _ temporary file \ COM test" + fileName, FileMode. create, FileAccess. write); byte [] bArr = new byte [1]; bArr [0] = (int) Protocol. received by the Server; serialPort. write (bArr, 0, bArr. length); break; case (int) Protocol. data Block sent by the Client: fs. write (receiveData, 0, receiveData. length); fs. flush (); bArr = new byte [1]; bArr [0] = (int) Protocol. received by the Server; serialPort. write (bArr, 0, bArr. length); break; case (int) Protocol. the Client sends the last data block: fs. write (receiveData, 0, receiveData. length); fs. flush (); bArr = new byte [1]; bArr [0] = (int) Protocol. server end; serialPort. write (bArr, 0, bArr. length); fs. close (); fs. dispose (); break ;}}}}View Code