Próbuje skomunikować się z urządzeniem zgodnie z protokołem transmisji z załącznika. Mój kod programu wygląda następująco:
class SerialPortProgram
{
//// Create the serial port with basic settings
//private SerialPort port = new SerialPort("COM1",
// 9600, Parity.None, 8, StopBits.One);
static string portName = "COM4";
static int baudRate = 115200; //szybkość transmisji
static Parity parityBits = Parity.Even; //1 bit parzystej parzystości
static int dataBits = 8; //ilość bitów danych
static StopBits stopBits = StopBits.One; //ilość bitów stopu
private SerialPort port = new SerialPort(portName, baudRate, parityBits, dataBits, stopBits);
[STAThread]
static void Main(string[] args)
{
// Instatiate this class
new SerialPortProgram();
}
private SerialPortProgram()
{
// Begin communications
port.Open();
// Attach a method to be called when there is data waiting in the port's buffer
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.RtsEnable = true; // Data-terminal-ready
port.DtrEnable = true; // Request-to-send
port.Encoding = Encoding.ASCII;
//port.ReceivedBytesThreshold = 9;
//port.PinChanged
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("<SOH>#0#0#0#<ETX>");
//byte[] bytes = System.Text.Encoding.ASCII.GetBytes("#0#0#0#");
port.Write(bytes, 0, bytes.Length);
Console.WriteLine("Incoming Data:");
Console.WriteLine(port.ReadLine());
// Enter an application loop to keep this thread alive
//Application.DoEvents();
Application.Run();
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Console.WriteLine("siema");
// Show all the incoming data in the port's buffer
Console.WriteLine(port.ReadExisting());
}
}
Niestety od urządzenia nie dostaje żadnej odpowiedzi, tzn nie zostaje wywołane zdarzenie 'port_DataReceived'
Do urządzenia był dołączony prosty program za pomocą którego można odczytywać dane przesyłane z czujnika urządzenia do komputera. Zainstalowałem WireShark i porównałem wysyłane zapytania do urządzenia. Jak widać na screenie różnią się one od siebie i pewnie dlatego nie dostaje poprawnej odpowiedzi. Macie jakieś pomysły gdzie zrobiłem błąd?
Okna po lewej stronie to poprawnie wysłane zapytanie przez program producenta, okna po prawej stronie to zapytania wysłane przez mój program.Zapytania wysłane w sposób:
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("<SOH>#0#0#0#<ETX>");
oraz
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("#0#0#0#");