سوال:در مورد WINCE
با عرض سلام و خسته نباشید
من یه برنامه رو برد اجرا میکنم ولی پورتو باز نمی کنه و ارور میده
خواهشمندم که کمکم کنید
چند بار کدها رو خوندم و چند جاشو اصلاح کردم ولی بازم کار نمیکنه
اگه نگاهی به کد بندازید و اشکالشو بهم بگین ممنون میشم
من کل کد رو نوشتم ولی مشکلم توی تابع
createfile
فک میکنم باشه
با عرض سلام و خسته نباشید
من یه برنامه رو برد اجرا میکنم ولی پورتو باز نمی کنه و ارور میده
خواهشمندم که کمکم کنید
چند بار کدها رو خوندم و چند جاشو اصلاح کردم ولی بازم کار نمیکنه
اگه نگاهی به کد بندازید و اشکالشو بهم بگین ممنون میشم
من کل کد رو نوشتم ولی مشکلم توی تابع
createfile
فک میکنم باشه
کد:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace GPIOsample
{
public partial class Form1 : Form
{
// Declare Function
[DllImport("coredll.dll")]
public static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("coredll.dll")]
public static extern bool DeviceIoControl(IntPtr hDevice, UInt32 dwIoControlCode, Byte[] lpInBuffer, UInt32 nInBufferSize, Byte[] lpOutBuffer, UInt32 nOutBufferSize, UInt32 lpBytesReturned, IntPtr lpOverlapped);
[DllImport("coredll.dll")]
public static extern bool CloseHandle(IntPtr hDevice);
// Constant of Function
const UInt32 OPEN_EXISTING = 3;
const UInt32 GENERIC_READ = 0x80000000;
const UInt32 GENERIC_WRITE = 0x40000000;
const Int32 INVALID_HANDLE_VALUE = -1;
// Constant from GPIO Driver
const UInt32 PORT_A = 0x00;
const UInt32 PORT_B = 0x10;
const UInt32 PORT_C = 0x20;
const UInt32 PORT_D = 0x30;
const UInt32 PORT_E = 0x40;
const UInt32 PORT_F = 0x50;
const UInt32 PORT_G = 0x60;
const UInt32 PORT_H = 0x70;
const UInt32 PORT_J = 0x80;
const UInt32 SET_OUTPUT = 0x04;
const UInt32 SET_INPUT = 0x03;
const UInt32 GET_PIN = 0x02;
const UInt32 SET_PIN_ON = 0x01;
const UInt32 SET_PIN_OFF = 0x00;
// Variable of Device
private IntPtr hPort;
public Form1()
{
InitializeComponent();
}
private void cmdSetInput_Click(object sender, EventArgs e)
{
byte[] sBuf = new byte[4];
UInt32 sInput = 0;
if (hPort != (IntPtr)INVALID_HANDLE_VALUE)
{
// Set pin position from CheckBox in to sInput
sInput += Convert.ToUInt32(chkInput0.Checked) << 0;
sInput += Convert.ToUInt32(chkInput1.Checked) << 1;
sInput += Convert.ToUInt32(chkInput2.Checked) << 2;
sInput += Convert.ToUInt32(chkInput3.Checked) << 3;
sInput += Convert.ToUInt32(chkInput4.Checked) << 4;
sInput += Convert.ToUInt32(chkInput5.Checked) << 5;
sInput += Convert.ToUInt32(chkInput6.Checked) << 6;
// Convert sInput(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sInput).CopyTo(sBuf, 0);
// Set pin in PORT F = Input Mode
DeviceIoControl(hPort, PORT_F | SET_INPUT, sBuf, (uint)sizeof(UInt32), null, 0, 0, IntPtr.Zero);
}
}
private void cmdSetOutput_Click(object sender, EventArgs e)
{
byte[] sBuf = new byte[4];
UInt32 sInput = 0, sOn = 0, sOff = 0;
if (hPort != (IntPtr)INVALID_HANDLE_VALUE)
{
// Check in CheckBox
if (chkOutput0.Checked)
{
// Set pin position in to sInput
sInput += (1 << 0);
if (chkOnOut0.Checked)
// Set pin position to set logic = 1
sOn += (1 << 0);
else
// Set pin position to set logic = 0
sOff += (1 << 0);
}
if (chkOutput1.Checked)
{
sInput += (1 << 1);
if (chkOnOut1.Checked)
sOn += (1 << 1);
else
sOff += (1 << 1);
}
if (chkOutput2.Checked)
{
sInput += (1 << 2);
if (chkOnOut2.Checked)
sOn += (1 << 2);
else
sOff += (1 << 2);
}
if (chkOutput3.Checked)
{
sInput += (1 << 3);
if (chkOnOut3.Checked)
sOn += (1 << 3);
else
sOff += (1 << 3);
}
if (chkOutput4.Checked)
{
sInput += (1 << 4);
if (chkOnOut4.Checked)
sOn += (1 << 4);
else
sOff += (1 << 4);
}
if (chkOutput5.Checked)
{
sInput += (1 << 5);
if (chkOnOut5.Checked)
sOn += (1 << 5);
else
sOff += (1 << 5);
}
if (chkOutput6.Checked)
{
sInput += (1 << 6);
if (chkOnOut6.Checked)
sOn += (1 << 6);
else
sOff += (1 << 6);
}
// Convert sInput(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sInput).CopyTo(sBuf, 0);
// Set pin position in PORT F = Output Mode
DeviceIoControl(hPort, PORT_F | SET_OUTPUT, sBuf, (uint)sizeof(UInt32), null, 0, 0, IntPtr.Zero);
// Convert sOn(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sOn).CopyTo(sBuf, 0);
// Set Pin in PORT F = Logic 1
DeviceIoControl(hPort, PORT_F | SET_PIN_ON, sBuf, (uint)sizeof(UInt32), null, 0, 0, IntPtr.Zero);
// Convert sOff(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sOff).CopyTo(sBuf, 0);
// Set Pin in PORT F = Logic 0
DeviceIoControl(hPort, PORT_F | SET_PIN_OFF, sBuf, (uint)sizeof(UInt32), null, 0, 0, IntPtr.Zero);
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Open GPIO Device
hPort = CreateFile("GPIO:", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (hPort == (IntPtr)INVALID_HANDLE_VALUE)
{
MessageBox.Show("Open GIO Driver Fail");
}
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
if (hPort != (IntPtr)INVALID_HANDLE_VALUE)
{
// Close GPIO Device
CloseHandle(hPort);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
byte[] rBuf = new byte[4];
UInt32 rOutput, rLen = 0, tmp = 0;
// Check Open Device already
if (hPort != (IntPtr)INVALID_HANDLE_VALUE)
{
// Get value from PORT F
DeviceIoControl(hPort, PORT_F | GET_PIN, null, 0, rBuf, rLen, 0, IntPtr.Zero);
// Convert rBuf(Array of Byte) to rOutput(UInt32)
rOutput = BitConverter.ToUInt32(rBuf, 0);
// Loop pin position in PORT F
for (int i = 0; i < 7; i++)
{
// Define pin position for get value
tmp = (UInt32)1 << i;
// Get value from variable
tmp = rOutput & tmp;
// Convert value to Boolean Type
bool rs = Convert.ToBoolean(tmp);
// Input value to CheckBox by Pin
switch (i)
{
case 0: chkOnRs0.Checked = rs; break;
case 1: chkOnRs1.Checked = rs; break;
case 2: chkOnRs2.Checked = rs; break;
case 3: chkOnRs3.Checked = rs; break;
case 4: chkOnRs4.Checked = rs; break;
case 5: chkOnRs5.Checked = rs; break;
case 6: chkOnRs6.Checked = rs; break;
}
}
}
}
private void cmdGet_Click(object sender, EventArgs e)
{
if (cmdGet.Text == "Get Start")
{
cmdGet.Text = "Get Stop";
timer1.Enabled = true;
}
else
{
cmdGet.Text = "Get Start";
timer1.Enabled = false;
}
}
}
}









دیدگاه