프로그래밍을 잘 모르는 학생인데요 어쩌다보니 일을 맞게 되었습니다.
문제가 생기는 부분은아래에 적어분 코드 전문에서 위의 코드는 잘 돌아가는데 아래에서 발생하는 문제가
private void sportRCV(object sender, EventArgs e)
{
Device.DataEventArgs de = (Device.DataEventArgs)e;
NeuroSky.ThinkGear.DataRow[] tempDataRowArray = de.DataRowArray;
TGParser tgParser = new TGParser();
tgParser.Read(de.DataRowArray);
L = tgParser.ParsedData.Length;
double[] RawLD = new double[L];
float[] RawL = new float[L];
if (serialPort1.BytesToRead > 0) //데이터가 읽혀지면 출력
{
string valueStr = serialPort1.ReadLine();
string[] valueStrArray = valueStr.Split(',');
이쪽인데요
Device.DataEventArgs de = (Device.DataEventArgs)e;
이부분이 EventArgs 타입을 DataEventArgs로 캐스팅 할수 없다고 하면서 오류가 납니다.
DataEventArgs타입의 경우 회사에서 준 솔루션 파일에 포함되어있는 헤더파일 안에 정의되어있고요
콘솔파일로 돌릴때는 돌아가는데 그걸 윈도우 폼으로 바꾸려고 다른 팀원이 작업중이던 원도우폼에 조금씩 잘라 붙여가고 있었는데
저곳에서 캐스팅 할수 없다면서 오류가 나더라고요
콘솔에서는 잘 캐스팅 되면서 왜 저기서는 저러는지 답답해 죽겠습니다 ㅜㅜㅜ
코드 전문도 올리겠습니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.IO.Ports;
using NeuroSky.ThinkGear;
using NeuroSky.ThinkGear.Algorithms;
using Mathematics;
using System.Windows.Forms;
using System.Drawing;
namespace testprogram {
class Program {
static Connector connector;
static byte poorSig;
static float[] RawF = new float[1024];
static int index = 0;
static int L = 0;
public static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Console.WriteLine("HelloEEG!");
// Initialize a new Connector and add event handlers
connector = new Connector();
connector.DeviceConnected += new EventHandler(OnDeviceConnected);
connector.DeviceConnectFail += new EventHandler(OnDeviceFail);
connector.DeviceValidating += new EventHandler(OnDeviceValidating);
// Scan for devices across COM ports
// The COM port named will be the first COM port that is checked.
connector.ConnectScan("COM12");
// Blink detection needs to be manually turned on
connector.setBlinkDetectionEnabled(true);
Thread.Sleep(450000);
Console.WriteLine("Goodbye.");
connector.Close();
Environment.Exit(0);
}
// Called when a device is connected
static void OnDeviceConnected(object sender, EventArgs e) {
Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e;
Console.WriteLine("Device found on: " + de.Device.PortName);
de.Device.DataReceived += new EventHandler(OnDataReceived);
}
// Called when scanning fails
static void OnDeviceFail(object sender, EventArgs e) {
Console.WriteLine("No devices found! :(");
}
// Called when each port is being validated
static void OnDeviceValidating(object sender, EventArgs e) {
Console.WriteLine("Validating: ");
}
// Called when data is received from a device
static void OnDataReceived(object sender, EventArgs e) {
//Device d = (Device)sender;
Device.DataEventArgs de = (Device.DataEventArgs)e;
DataRow[] tempDataRowArray = de.DataRowArray;
TGParser tgParser = new TGParser();
tgParser.Read(de.DataRowArray);
L = tgParser.ParsedData.Length;
double[] RawLD = new double[L];
float[] RawL = new float[L];
//float[] RawFFT = new float[L];
/* Loops through the newly parsed data of the connected headset*/
// The comments below indicate and can be used to print out the different data outputs.
for (int i = 0; i < tgParser.ParsedData.Length; i++){
if (tgParser.ParsedData[i].ContainsKey("Raw")){
//Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
RawLD[i] = tgParser.ParsedData[i]["Raw"];
RawL[i] = (float)(tgParser.ParsedData[i]["Raw"]);
RawF[index] = RawL[i];
++index;
if (index == RawF.Length)
{
for (int k = 0; k < RawF.Length; k++)
{
//Console.WriteLine("RawF = " + k + ":" + RawF[k]);
}
float[] RawFFT = FFT1D.powerSpectrum1D(RawF, false);
for (int k = 0; k < RawFFT.Length; k++)
{
Console.WriteLine("RawFFT = " + ":" + RawFFT[k]);
}
index = 0;
}
}
}
}
}
}
위가 콘솔 코드 전문이고요
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NeuroSky.ThinkGear;
using NeuroSky.ThinkGear.Algorithms;
using System.Threading;
using Mathematics;
namespace testprogram
{
public partial class Form1 : Form
{
Connector connector;
byte poorSig;
float[] RawF = new float[1024];
int index = 0;
int L = 0;
int m = 0;
int l = 0;
//private SerialPort port = new SerialPort();
private Queue<byte> recievedData = new Queue<byte>();
public Form1()
{
string InputData = String.Empty;
InitializeComponent();
}
private void Serialport1_datareceived(object sender, SerialDataReceivedEventArgs e)
{
m = 2;
label.Text = "2";
try
{
/* int BTR = serialPort1.BytesToRead;
char[] array = new char[BTR];
serialPort1.Read(array, 0, BTR);
*/
//byte[] data = new byte[serialPort1.BytesToRead];
//serialPort1.Read(data, 0, serialPort1.BytesToRead);
//data.ToList().ForEach(b => recievedData.Enqueue(b));
this.Invoke(new EventHandler(sportRCV)); //쓰레드 위반을 피해가기 위해
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "종료", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void sportRCV(object sender, EventArgs e)
{
Device.DataEventArgs de = (Device.DataEventArgs)e;
NeuroSky.ThinkGear.DataRow[] tempDataRowArray = de.DataRowArray;
TGParser tgParser = new TGParser();
tgParser.Read(de.DataRowArray);
L = tgParser.ParsedData.Length;
double[] RawLD = new double[L];
float[] RawL = new float[L];
if (serialPort1.BytesToRead > 0) //데이터가 읽혀지면 출력
{
string valueStr = serialPort1.ReadLine();
string[] valueStrArray = valueStr.Split(',');
//======================================= 여기부터 추가=============================================
float[] valuefloatArray = new float[valueStrArray.Length];
for (int i = 0; i < valueStrArray.Length; ++i)
{
valuefloatArray[i] = float.Parse(valueStrArray[i]);
}
float[] valuefftArray = Mathematics.FFT1D.powerSpectrum1D(valuefloatArray, false);
//=======================================여기까지 추가===============================================
for (int i = 0; i < valueStrArray.Length; ++i)
{
//float value = float.Parse(valueStrArray[i]); // ((기존))
float value = valuefftArray[i]; // 위의껄 이걸로 바꿈
txtRxData.Text += ";" + value;
}
//txtRxData.Text += serialPort1.ReadLine();
// txtRxData.Text += serialPort1.BytesToRead.ToString() + ";";
//float j = serialPort1.ReadByte();
// //txtRxData.AppendText(j.ToString() + ";");
// char str = (char)j;
// txtRxData.AppendText(str + ";");
// //txtRxData.Text += j.ToString() + ";";
}
}
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
cboPort.Items.AddRange(ports);
cboPort.SelectedIndex = 0;
System.Int32[] Baudlates = { 9600, 115200, 4800, 19200, 38400 };
for (int temp = 0; temp < 5; temp++)
{
cborate.Items.Add(Baudlates[temp]);
}
cborate.SelectedIndex = 0;
btnClose.Enabled = false;
}
private void btnOpen_Click(object sender, EventArgs e)
{
btnOpen.Enabled = false;
btnClose.Enabled = true;
try
{
serialPort1.PortName = cboPort.Text;
serialPort1.Open();
l = 2;
label1.Text = "2";
}
catch (Exception ex)
{
l = 1;
label1.Text = "port is not connected";
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
btnOpen.Enabled = true;
btnClose.Enabled = false;
try
{
serialPort1.Close();
l = 1;
label1.Text = "port is not connected";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen)
serialPort1.Close();
}
private void toolStripStatusLabel1_TextChanged(object sender, EventArgs e)
{
if (m == 2)
{
label.Text = "getting data...";
}
}
private void label1_TextChanged(object sender, EventArgs e)
{
if(l == 1)
{
label1.Text = "port is not connected";
}
if (l == 2)
{
label1.Text = serialPort1.PortName + " is connected | baudrate =" + serialPort1.BaudRate;
}
}
private void cborate_SelectedIndexChanged(object sender, EventArgs e)
{
System.String SelectedBaud;
SelectedBaud = cborate.SelectedItem.ToString() + ",8N1";
serialPort1.BaudRate = (Int32)cborate.SelectedItem;
}
}
}
여기까지가 윈도우폼으로 바꾸려는 중에 있는 코드입니다.
코드가 아직 완성도 아니라 뒤죽박죽인데요
콘솔프로그램 돌릴때는 멀쩡히 하던 캐스팅을 왜 못하는지 모르겠습니다. ㅜㅜㅜㅜ