The Way to Programming
The Way to Programming
I am making a really simple robot from computer components. I need a program that will open and close cd units in a time period of 5 seconds. There will be 2 cd units. So my idea is to make a program that will open the cd unit then keep it opened for 5 sec and then close it. I can sort this out with hardware accessories some electrical timers. But i want to see if i can keep it single with a program. It should run on windows xp platform.
I have some basic knowledge of c++ so if there is a good article out there on this i think i would be able to write it. But if someone is willing to write it for me which should take too long i think it would be perfect. As i sad i am on a project to make a robot so i have a lot of work on hardware and really little time.
in python using pygame
import pygame.cdrom as cdrom cdrom.init() cd = cdrom.CD(0) # 0 = first cdrom device cd.init() cd.eject() cd.quit() cdrom.quit()
To do it in C++, use CreateFile() and give it the drive letter for the file name. When you have the handle, you can use DeviceIoControl() and use IOCTL_STORAGE_EJECT_MEDIA or IOCTL_STORAGE_LOAD_MEDIA. For a simple timer, you can use GetTickCount(). If you need something better, you can use QueryPerformanceCounter().
CreateFile
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
DeviceIoControl
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216%28v=vs.85%29.aspx
GetTickCount
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408%28v=vs.85%29.aspx
QueryPerformanceCounter
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904%28v=vs.85%29.aspx
Below is a simple program that will open and close all your CD trays.
#include#include #define MAXBUFFER 512 int _tmain() { HANDLE hDrive; TCHAR drives[MAXBUFFER]; if (GetLogicalDriveStrings(MAXBUFFER - 1, drives)) { DWORD temp; TCHAR * ptrDrives = drives; TCHAR strDrive[] = TEXT("\\\\.\\ :"); do { if (GetDriveType(ptrDrives) == DRIVE_CDROM) { *(strDrive + 4) = *ptrDrives; hDrive = CreateFile(strDrive, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hDrive == INVALID_HANDLE_VALUE) break; if (!DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &temp, NULL)) { CloseHandle(hDrive); break; } if (!DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &temp, NULL)) { CloseHandle(hDrive); break; } CloseHandle(hDrive); } while (*ptrDrives++); } while (*ptrDrives); } return 0; }
Here it is. It is done in Visual studio. Works perfectly except the first cycle. ( it opens and closes the first driver without delay). But after that it just does what is suppose to do.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Diagnostics; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint = "mciSendStringA")] public static extern void mciSendStringA(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback); string rt = ""; private void button1_Click(object sender, EventArgs e) { timer1.Interval = 1000 * 5; //timer to open first and close second timer1.Enabled = true; timer1.Start(); timer2.Interval = 1000 * 5; //revers timer timer2.Enabled = true; mciSendStringA("open D: type CDAudio alias driveD", rt, 127, 0); //defining driver mciSendStringA("open E: type CDAudio alias driveE", rt, 127, 0); } private void button2_Click(object sender, EventArgs e) { //test button mciSendStringA("set driveF door open", rt, 127, 0); MessageBox.Show("Radi li"); } private void timer1_Tick(object sender, EventArgs e) { //open first close second mciSendStringA("set driveE door closed", rt, 127, 0); mciSendStringA("set driveD door open", rt, 127, 0); //MessageBox.Show("start"); timer1.Stop(); timer2.Start(); } private void timer2_Tick(object sender, EventArgs e) { //open second close first mciSendStringA("set driveD door closed", rt, 127, 0); mciSendStringA("set driveE door open", rt, 127, 0); //MessageBox.Show("stop"); timer2.Stop(); timer1.Start(); } } }
Sign in to your account