Zum Inhalt springen

JRinfo

Mitglieder
  • Gesamte Inhalte

    1
  • Benutzer seit

  • Letzter Besuch

  1. Hallo Forum, ich bin das erste Mal in einem Forum. Bei mir geht es um folgendes: ich habe ein Messsystem für Werkzeuge (CNC) mit folgenden Ausgängen entwickelt: RS232, USB, LPT. Eine einfache Software in MS Visual C# 2008 soll die RS232 und USB verwalten. RS232 klappt, aber USB hat noch ein Problem. Ich verwende einen FTDI FT232R USB Controller. Als Virtueller COM-Port funktioniert die USB. Für Visual C# existiert die Datei FTD2XX_NET.dll. Nun möchte ich beim Empfang von Daten einen FT_EVENT_RXCHAR auslösen – und genau da haperts. Wie programmiere ich einen EVENT? Über toolStripButton2_Click öffne ich die USB. Mit einer do – while Schleife konnte ich feststellen, das Daten ankommen. Bis auf ein EVENT fuktioniert alles. Was habe ich bei der Aktivierung das EVENT falsch gemacht ???? Grüße JRinfo 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.Windows; using System.IO.Ports; using System.IO; using System.Runtime.InteropServices; using FTD2XX_NET; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form1 : Form { string daten; string readData; private string strPrintText; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; FTDI myFtdiDevice = new FTDI(); //einen EVENT erzeugen!!!!!!!! private EventWaitHandle DataReceived = new EventWaitHandle(false, EventResetMode.AutoReset); // Handle for data received events BackgroundWorker DataHandler = new BackgroundWorker(); // Second thread for processing data received public Form1() { InitializeComponent(); DataHandler.DoWork += new DoWorkEventHandler(DataHandler_DoWork); } private void Form1_Load(object sender, EventArgs e) { string[] ports = SerialPort.GetPortNames(); foreach (string comname in ports) { toolStripDropDownButton1.DropDownItems.Add(comname); } } private void toolStripButton2_Click(object sender, EventArgs e) // USB öffnen { toolStripButton1.Checked = false; if (serialPort1.IsOpen) serialPort1.Close(); if (toolStripButton2.Checked == true) { ftStatus = myFtdiDevice.OpenByDescription("WASI-MESSSYSTEM"); if (ftStatus == FTDI.FT_STATUS.FT_OK) { ftStatus = myFtdiDevice.SetBaudRate(115200); if (ftStatus == FTDI.FT_STATUS.FT_OK) { ftStatus = myFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_7, FTDI.FT_STOP_BITS.FT_STOP_BITS_2, FTDI.FT_PARITY.FT_PARITY_EVEN); if (ftStatus == FTDI.FT_STATUS.FT_OK) { if (!DataHandler.IsBusy) DataHandler.RunWorkerAsync(); //einen EVENT erzeugen!!!!!!!! ftStatus = myFtdiDevice.SetEventNotification(FTDI.FT_EVENTS.FT_EVENT_RXCHAR, DataReceived); if (ftStatus != FTDI.FT_STATUS.FT_OK) { } } } } } else { if (myFtdiDevice.IsOpen == true) { myFtdiDevice.Close(); } } } void DataHandler_DoWork(object sender, DoWorkEventArgs e) { uint numBytes = 0; if ((ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytes)) == FTDI.FT_STATUS.FT_OK) { if ((ftStatus = myFtdiDevice.Read(out readData, numBytes, ref numBytes)) == FTDI.FT_STATUS.FT_OK) { daten = daten + readData; } } } private void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { toolStripButton1.Text = e.ClickedItem.Text; } private void toolStripButton1_Click(object sender, EventArgs e) { toolStripButton2.Checked = false; if (toolStripButton1.Checked == true) { if (myFtdiDevice.IsOpen == true) { myFtdiDevice.Close(); } if (!serialPort1.IsOpen) { try { serialPort1.BaudRate = 9600; serialPort1.DataBits = 7; serialPort1.StopBits = StopBits.Two; serialPort1.Parity = Parity.Even; serialPort1.Handshake = Handshake.None; serialPort1.Open(); } catch { string message = serialPort1.PortName + " kann nich geöffnet werden!\nWird vermutlich von einer anderen Anwendung benutzt"; MessageBoxIcon icon = MessageBoxIcon.Information; MessageBoxButtons button = MessageBoxButtons.OK; MessageBox.Show(message, "Information", button, icon); toolStripButton1.Checked = false; } } } else { if (serialPort1.IsOpen) serialPort1.Close(); } } private void toolStripButton5_Click(object sender, EventArgs e) { this.richTextBox1.Clear(); daten = ""; } private void DisplayText(object sender, EventArgs e) { richTextBox1.AppendText(daten); } private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { int C = serialPort1.BytesToRead; int X; char chr; for (X = 0; X < C; X++) { chr = Convert.ToChar(serialPort1.ReadByte()); if (chr == '%') { this.Invoke(new EventHandler(DisplayText)); } else daten = daten + chr; } } private void toolStripButton7_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1; if (printDialog1.ShowDialog() == DialogResult.OK) { strPrintText = richTextBox1.Text; printDocument1.Print(); } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { StringFormat stringFormat = new StringFormat(); Pen blackPen = new Pen(Color.Black, 3); RectangleF rectFPapier, rectFText; rectFPapier = e.MarginBounds; rectFText = RectangleF.Inflate(rectFPapier, 0, -2 * richTextBox1.Font.GetHeight(e.Graphics)); stringFormat.Trimming = StringTrimming.Word; e.Graphics.DrawString(strPrintText, richTextBox1.Font, Brushes.Black, rectFText, stringFormat); } } }

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...