public static void Start(Handshake handshake) { int nWritten; // Note: zeros must be sent first to help the receiver sync after the noise generated // by the port initialization - don't send the important stuff right away...! byte[] start = { 0 }; if (handshake == Handshake.None) { Debug.Print("***** Testing No Flow Control *****"); start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'3', (byte)'\r', (byte)'\n', 0 }; } else if (handshake == Handshake.XOnXOff) { Debug.Print("***** Testing SW Flow Control *****"); start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'4', (byte)'\r', (byte)'\n', 0 }; } else if (handshake == Handshake.RequestToSend) { Debug.Print("***** Testing HW Flow Control *****"); start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'5', (byte)'\r', (byte)'\n', 0 }; } SerialPortTest app = new SerialPortTest(); byte[] Record = new byte[c_BufferSize]; UInt32 uiRecordNumber = 0x1323; // Starting record number (pulled from a hat) SerialPort hPort = new SerialPort("COM1", (int)BaudRate.Baudrate115200); hPort.Handshake = handshake; hPort.ReadTimeout = 500; hPort.Open(); hPort.Flush(); while (0 < hPort.Read(Record, 0, Record.Length)) ; // Note: // When a hardware reset is issued to some targets prior to a deploy, the original deployed // code begins to execute for a second or two before Visual Studio is able to gain control, // halt the old deployed code and deploy the current program. // The following two second delay is to prevent this app from issuing the start record prior // to VS gaining control. Otherwise, the PC side app will get the signal to start before // the target side app has even deployed. This will result in the loss of the first three // or four data records and the test will fail. Thread.Sleep(2000); // Send out a simple signal to show the PC side test software that we're ready to go hPort.Write(start, 0, start.Length); // Test COM port handshake by flooding the port with testable sequential records // and verifying that the records arrive correctly. The handshake is verified // by causing 5 second pauses during reading which should translate to five // second pauses in the transmit as well - unless the driver buffer is obviating // the need for handshake - in which case, the buffer should be diminished or the // volume of traffic should increase until handshake must be employed. for (int i = 0; i < c_NumRecords; i++) { if (!app.ReceiveRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #1, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); // return; // We may as well quit now } //Debug.Print(AsString(Record)); //Debug.Print(new string(System.Text.UTF8Encoding.UTF8.GetChars(Record))); if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems { Debug.Print("There were problems with test #1, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); //return; // We may as well quit now } uiRecordNumber++; // Next record } for (int i = 0; i < c_NumRecords; i++) { app.CreateRecord(uiRecordNumber, Record); //Debug.Print("\nSending Record: " + uiRecordNumber); //Debug.Print(new string(System.Text.UTF8Encoding.UTF8.GetChars(Record))); if(!app.SendRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #2, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); return; // We may as well quit now } uiRecordNumber++; } // This is just like the previous test - except that a gap of five seconds // will be inserted before receiving the rest of the records which will // hopefully hold off transmission due to a functioning handshake. for (int i = 0; i < 5; i++) { if (!app.ReceiveRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #3, record #" + toString(i + 1) + " of 5"); return; // We may as well quit now } if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems { return; // We may as well quit now } uiRecordNumber++; // Next record } Thread.Sleep(5000); // Do not allow reception of characters for five seconds (simulate erasing FLASH or some such) for (int i = 0; i < (c_NumRecords - 5); i++) { if (!app.ReceiveRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #4, record #" + toString(i + 1) + " of " + toString(c_NumRecords - 5)); return; // We may as well quit now } if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems { byte[] CRecord = new byte[c_BufferSize]; app.CreateRecord(uiRecordNumber, CRecord); Debug.Print("got: " + AsString(Record)); Debug.Print("exp: " + AsString(CRecord)); Debug.Print("There were problems with test #4, record #" + toString(i + 1) + " of " + toString(c_NumRecords - 5)); return; // We may as well quit now } uiRecordNumber++; // Next record } // During this test, the receiving unit will hopefully hold off which will // cause this transmission to also (hopefully) report the same holdoff for (int i = 0; i < c_NumRecords; i++) { app.CreateRecord(uiRecordNumber, Record); if (!app.SendRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #5, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); return; // We may as well quit now } uiRecordNumber++; } Debug.Print("All records apparently sent and received as expected"); hPort.Dispose(); GC.WaitForPendingFinalizers(); }
public static void Start(Handshake handshake) { int nWritten; // Note: zeros must be sent first to help the receiver sync after the noise generated // by the port initialization - don't send the important stuff right away...! byte[] start = { 0 }; if (handshake == Handshake.None) { Debug.Print("***** Testing No Flow Control *****"); start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'3', (byte)'\r', (byte)'\n', 0 }; } else if (handshake == Handshake.XOnXOff) { Debug.Print("***** Testing SW Flow Control *****"); start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'4', (byte)'\r', (byte)'\n', 0 }; } else if (handshake == Handshake.RequestToSend) { Debug.Print("***** Testing HW Flow Control *****"); start = new byte[] { 0, 0, 0, 0, 0, (byte)'X', (byte)'2', (byte)'5', (byte)'\r', (byte)'\n', 0 }; } SerialPortTest app = new SerialPortTest(); byte[] Record = new byte[c_BufferSize]; UInt32 uiRecordNumber = 0x1323; // Starting record number (pulled from a hat) SerialPort hPort = new SerialPort("COM1", (int)BaudRate.Baudrate115200); hPort.Handshake = handshake; hPort.ReadTimeout = 500; hPort.Open(); hPort.Flush(); while (0 < hPort.Read(Record, 0, Record.Length)) { ; } // Note: // When a hardware reset is issued to some targets prior to a deploy, the original deployed // code begins to execute for a second or two before Visual Studio is able to gain control, // halt the old deployed code and deploy the current program. // The following two second delay is to prevent this app from issuing the start record prior // to VS gaining control. Otherwise, the PC side app will get the signal to start before // the target side app has even deployed. This will result in the loss of the first three // or four data records and the test will fail. Thread.Sleep(2000); // Send out a simple signal to show the PC side test software that we're ready to go hPort.Write(start, 0, start.Length); // Test COM port handshake by flooding the port with testable sequential records // and verifying that the records arrive correctly. The handshake is verified // by causing 5 second pauses during reading which should translate to five // second pauses in the transmit as well - unless the driver buffer is obviating // the need for handshake - in which case, the buffer should be diminished or the // volume of traffic should increase until handshake must be employed. for (int i = 0; i < c_NumRecords; i++) { if (!app.ReceiveRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #1, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); // return; // We may as well quit now } //Debug.Print(AsString(Record)); //Debug.Print(new string(System.Text.UTF8Encoding.UTF8.GetChars(Record))); if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems { Debug.Print("There were problems with test #1, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); //return; // We may as well quit now } uiRecordNumber++; // Next record } for (int i = 0; i < c_NumRecords; i++) { app.CreateRecord(uiRecordNumber, Record); //Debug.Print("\nSending Record: " + uiRecordNumber); //Debug.Print(new string(System.Text.UTF8Encoding.UTF8.GetChars(Record))); if (!app.SendRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #2, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); return; // We may as well quit now } uiRecordNumber++; } // This is just like the previous test - except that a gap of five seconds // will be inserted before receiving the rest of the records which will // hopefully hold off transmission due to a functioning handshake. for (int i = 0; i < 5; i++) { if (!app.ReceiveRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #3, record #" + toString(i + 1) + " of 5"); return; // We may as well quit now } if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems { return; // We may as well quit now } uiRecordNumber++; // Next record } Thread.Sleep(5000); // Do not allow reception of characters for five seconds (simulate erasing FLASH or some such) for (int i = 0; i < (c_NumRecords - 5); i++) { if (!app.ReceiveRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #4, record #" + toString(i + 1) + " of " + toString(c_NumRecords - 5)); return; // We may as well quit now } if (!app.CheckRecord(uiRecordNumber, Record)) // If there were problems { byte[] CRecord = new byte[c_BufferSize]; app.CreateRecord(uiRecordNumber, CRecord); Debug.Print("got: " + AsString(Record)); Debug.Print("exp: " + AsString(CRecord)); Debug.Print("There were problems with test #4, record #" + toString(i + 1) + " of " + toString(c_NumRecords - 5)); return; // We may as well quit now } uiRecordNumber++; // Next record } // During this test, the receiving unit will hopefully hold off which will // cause this transmission to also (hopefully) report the same holdoff for (int i = 0; i < c_NumRecords; i++) { app.CreateRecord(uiRecordNumber, Record); if (!app.SendRecord(hPort, Record)) // If there were problems { Debug.Print("There were problems with test #5, record #" + toString(i + 1) + " of " + toString(c_NumRecords)); return; // We may as well quit now } uiRecordNumber++; } Debug.Print("All records apparently sent and received as expected"); hPort.Dispose(); GC.WaitForPendingFinalizers(); }