using System;
using System.IO.Ports;
using System.Collections.Generic;
namespace SerialCom
{
class Program
{
//Created serial port object and initialised
static SerialPort com = new SerialPort(SerialPort.GetPortNames()[0],
1200, Parity.None, 8, StopBits.One);
static void Main(string[] args)
{
initiateDataStreaming();
Console.ReadKey();
}
private static void initiateDataStreaming()
{
//Open com port
com.Open();
// Write Line writes string and new line value to output buffer
// {
byte[] byteToSend = new byte[] {0x10,0x02,0x47,0x10,0x03,0x42,0x1F};
com.Write(byteToSend, 0, byteToSend.Length);
Console.WriteLine("Waiting for incoming data...");
Console.ReadKey();
com.DataReceived +=
new SerialDataReceivedEventHandler(com_DataReceived);
}
private static void com_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine("Hello0"); // Checking if enter method
Console.WriteLine(com.ReadExisting());
}
}
}
When I run the console just says "Waiting for incoming data"
I cannot get the CTG to send data, I have checket the CRC a few times and have tried different CCITT codes to no avail.
If anyone is able to see what is wrong or able to help would be much appreciated, We have clinical trials ready to go in hospital that are waiting on me to get this working so any help is much appreciated!
VulpesPosted Jul 13, 2011, 5:15 AM
Maja GajicPosted Jul 13, 2011, 1:54 AM
I think there is a problem with the way I am splitting up the CRC codeword.
If I have a two byte word 0x421F I need to split into two bytes
to fit into the last two elements in the array below:
So to split up 0x421F I convert it into binary and split it into 8 bits. 01000010 | 0001111
The least significant byte is padded with 3 zeroes which are left out when converting
to hex? I know this is quiet low level but is there any way of keeping the zero padding?
Thanks for any help
Maja :)
VulpesPosted Jul 12, 2011, 4:13 AM
Sam HobbsPosted Jul 12, 2011, 12:06 AM
Be sure to set the baud rate correctly. As I recall, you also need to specify the number of bits (8 or 7 usually), parity (usually N or E) and the number of stop bits (usuallly 1). In addition to that, be sure to get the flow control correct.