Programming Custom Hardware in Visual Basic. Screen shot of Visual Basic Application for the Port I/OIntroduction. Audience: You are developing custom hardware for Windows 9. You are frustrated learning how to create a DLL. You want Port I/O in Visual Basic.
You want to mimic Quick. Basic's INPUT and OUTPUT statements in VB. You want to mimic Turbo C's inportb() and outportb() statements in VB. Level: ALL. Pre- requisites: Some Visual Basic programming, Win. PC. Compilers: Visual Basic 4. Microsoft Visual C++ 4. DLL and not program your own). Downloads: All source code to DLL, compiled DLL. You might be curious how to write Visual Basic (VB) applications for your unique hardware. For example, you custom developed a PC card. It might be a data acquisition card. This tutorial will show you can write VB programs. Dynamically linked library (DLL). This tutorial is in response to the dozens of postings on the VB usenet group every month. How do I create a DLL?""How do you get VB to call functions written in Visual C++ or. Sadly, you might be frustrated with the posted responses. You might be frustrated. You might be frustrated that the DLL's on the Internet. Here, this tutorial gives you step- by- step instructions along with GIF image. DLL. This tutorial is also in response to the hundreds of email Boondog gets each month about. VB apps for the 8. PC Interface Card. This simple tutorial will show you how to get started. Dynamically Linked Libraries (DLL)Why do I need DLLs? You might have just started using Visual Basic (VB), appreciating how easy it is. Win. 95 3. 2- bit applications with it. The learning curve is relatively quick. You. might have migrated from Quick. Basic or Turbo C, thus having some knowledge of the. If you have migrated from DOS' Quick. Basic to VB, you soon realize that Quick. Basic's. INPUT and OUTPUT (or Turbo C's inportb and outportb). VB. These functions are crucial for PC hardware. Thus without INPUT or OUPUT you can't read from or. Windows Serial Communications component library. Developer tools for serial port communications; Supports RS232, multidrop RS422 and RS485 ports, virtual serial ports. There is a way around this, using a DLL. As the name implies, DLLs allows. VB to link (a step before compiling) code (libraries you coded up in another language. Delphi, Borland C++ or Microsoft's Visual C++) during run- time (dynamically). VC++ has port I/O (input and output) read/write functions. Also VC++'s compiler. DLLs (in addition to executable EXE files). Thus you. 1. Write VC++ code that uses these read/write functions. Compile it into a DLL (instead of an executable EXE file) file. Call your functions from VB. Writing your own DLL or just using one? If you don't have VC++ don't worry. You can still use the FREE DLL here to. Download 8. 25. 5. ZIP which. contains 8. You just copy the 8. DLL file to your C: /windows. You can then have your VB program use them. But if you are curious then writing a DLL is very easy. The steps in this. Visual C++ 5. 0, but easy enough to mimic in Delphi. Visual C++ 4. 0 and Borland C++. Writing the DLL. There are two files you need to create. The first is a DEF (define) file. The. second is the CPP (C++ source) file. Both are simple ASCII text files. You can. use any editor (e. DOS' edit, or Windows' Notepad). These are listed. DESCRIPTION DLL FOR 8. CARD. Out. 82. 55 @1. In. 82. 55 @2. The name of your DLL library is given on the first line. It is. 8. 25. 5. The second line is just a comment. Exports list the. VC++. These functions are: Out. In. 82. 55. If you eventually. My. Function @3. 8. FILE: 8. 25. 5. cpp. AUTH: P. OH/Boondog Automation. DATE: 0. 7/0. 1/9. DESC: CPP source file for 8. DLL - compiled with Microsoft Visual C++ 5. Visual C++'s inp and out functions. FUNC: Out. 82. 55. DESC: uses Microsoft's Visual C++ _outp() function. Port. Data to Port. Address. // - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -. Out. 82. 55( int Port. Address, int Port. Data ). short Dummy. Need Dummy since _outp officially returns int. Win. 32 C++. // whereas int is 3. Win. 32 C++. // use (short) to force returning 1. VB. Dummy = (short)(_outp( Port. Address, Port. Data )). Dummy). }; // end of Out. FUNC: In. 82. 55. DESC: uses Microsoft's Visual C++ _inp() function. Port. Address. // - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -. In. 82. 55( int Port. Address ). short Port. Data. // short is a 1. Win. 32 C++. // whereas int is 3. Win. 32 C++. // use (short) to force returning 1. VB. Port. Data = (short)(_inp( Port. Address )). return( Port. Data ). }; /* end of In. In. 82. 55 and Out. It uses VC++'s _inp() and _out(). The single underscore before inp and outp are needed. This underscore notation refers to downloadly compatiable functions. VC++. Step 1: Write your . Create a directory called c: /port. Type and save the DEF file as 8. CPP file as 8. 25. Step 2: Visual C++ 5. Bring up Visual C++. Select FILE - NEW as seen in Figure 1. Step 3: Create Your Project. This brings up the NEW window. Make sure the Projects tab is selected and. Win. 32 Dynamic- Link Library (Figure 2). Make sure that Location is. This. is why you created a directory c: /port in Step 1. Type 8. 25. 5 in the Project Name. Click on OK. You should now see the result (Figure 3). If not. click on the classes tab. You have just created a project called 8. Step 4: Add your . Left click on 8. 25. This selects it and turns it blue. Next, right. click your mouse and choose Add Files to Project (Figure 4). An Insert Files to Project (Figure 5) will pop up. Make sure the Files of Type is. C++ files. Next, choose the 8. OK. Step 5: Add your . Similar to Step 4, click on 8. Right click and select. Add Files to Project. Again the Insert File into Project window pops. Figure 6). This time make sure the Files of Type is set for. Definition (. def) files. Click on 8. 25. 5. OK. Your 8. 25. 5 Project. Figure 7). Save everything. FILE - SAVE ALL. Step 6: Build your . On the top bar click on Build and select Build 8. Figure 8). This. will start compiling and create th. If you didn't mistype. VC++ will respond with 0 errors. Your new 8. 25. 5. Figure 9). Step 7: Copy your . Copy your new DLL file to your c: /windows directory. You can do this from. DOS (my habit), by bringing up MS- DOS prompt and cd`ing to c: /port/8. Debug. (Figure 1. Of course, you can drag- and- drop from Explorer. Congradulations! Your . Visual Basic programs! Easy right? Using Your DLL in Your VB Programs. To use your DLL functions, you add the following lines. VB program's declaration section. Typically. right after the Option Explicit statement. Note: the. underscore in VB means that the statement is spread. Private Declare Function Out. Lib "8. 25. 5. dll" _. By. Val Port. Address As Integer, _. By. Val Port. Data As Integer) As Integer. Private Declare Function In. Lib "8. 25. 5. dll" _. By. Val Port. Address As Integer) As Integer. To write to a port, use Out. Dummy = Out. 82. 55(Cntrl, 1. Dummy, Cntrl are integers. Here, the decimal value. Cntrl. It is. necessary to use associate an integer variable (I called it "Dummy". Out. 82. 55 function. This is because the DLL. VC++'s outp(), which returns an integer. To read from a port, use In. Port. Value = In. Port. Selected). where Port. Value and Port. Selected are integers. The 8- bit. number at port Port. Selected will be assigned (in decimal. Port. Value. The Count Program. The 8. 25. 5. dll was applied to the 8. PC Interface Card. This is a custom- build card that plugs into the ISA slot. PC's motherboard. The card provides you with three. A, B and C, giving you a total of 2. I/O. You can. download COUNT. ZIP. This file contains, the VBW, VBP, FRM and EXE. Also, Count's source code listing is in the COUNT. TXT file. In this simple application, 8 LEDs were connected to. Card. The program. LEDs. This may not sound very. The above figure is a screen shot of the COUNT program. The user types in the 8. Card's base address (6. OK. The program. starts counting. Designing Count's Form. The VB program uses 1. The object type, its name. Object Type. Name. Value. Formfrm. Count. Caption: Count. Labellbl. Address. Caption: 8. Address. Labellbl. Decimal. Output. Caption: Decimal Output. Labellbl. Port. ACaption: Port ALabellbl. Port. BCaption: Port BLabellbl. Port. CCaption: Port COptionopt. Port. AOptionopt. Port. BOptionopt. Port. CCommandcmd. Go. Go. Commandcmd. End. End. Textboxtxt. Address. Text: (None)Textboxtxt. Output. Window. Text: (None)Timertmr. Timer. Interval: 5. Count's VB code. The VB code listing is given below. Option Explicit. 'Declare use of the DLL. Private Declare Function Out. Lib "8. 25. 5. dll" (By. Val Port. Address As Integer, By. Val Port. Data As Integer) As Integer. Private Declare Function In. Using Visual Basic 2. Control Arduino Uno. I know this has been done in the past a few times so here's one more. This is a skeletal Visual Basic 2. Arduino Sketch that I mixed together to test the PC to Arduino Uno connection via serial. It simply turns LED 1. The Visual Basic 2. Form. 1 with 2 buttons Button. Button. 2 and Serial. Port. 1 controls. Button. 1 sends a 1 and Button. COM1. 0 (change this to match your PC to Arduino port setting)See attached photo of my simple form design. The Arduino Uno Sketch code simply waits and reads the serial port. If it see 1 it will turn PIN 1. PIN 1. 3 off. If you have an LED on PIN 1. On the Arduino Uno, PIN 1. LED. I used COM1. Arduino serial port. The purpose of this code is to simplify explanation of how to connect VB to Arduino. You can add error processing and more intelligence based on your particular needs. Make sure you drag the Serial Port control icon from the Toolbox onto your form. It should have the name Serial. Port. 1WARNING: On my PC I had to close the Arduino IDE Serial Monitor window while runing the VB program, else I run into all sorts of error message about COM port access denied and the program will fail. You can download Visual Basic Express 2. Microsofthttp: //www. START OF VB 2. 01. CODE - -- -- -- -- -- -- -- -- ' NOTE: I am using COM1. Visual Basic code to match your COM port. Imports System. IOImports System. IO. Ports. Imports System. Threading. Public Class Form. Shared _continue As Boolean. Shared _serial. Port As Serial. Port. Private Sub Form. Load(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles My. Base. Load. Serial. Port. 1. Close()Serial. Port. 1. Port. Name = "com. Arduino port. Serial. Port. 1. Baud. Rate = 9. Serial. Port. 1. Data. Bits = 8. Serial. Port. 1. Parity = Parity. None. Serial. Port. Stop. Bits = Stop. Bits. One. Serial. Port. 1. Handshake = Handshake. None. Serial. Port. Encoding = System. Text. Encoding. Default 'very important! End Sub. Private Sub Button. Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles Button. Click. Serial. Port. Open()Serial. Port. Write("1")Serial. Port. 1. Close()End Sub. Private Sub Button. Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles Button. Click. Serial. Port. Open()Serial. Port. Write("0")Serial. Port. 1. Close()End Sub. End Class'- -- -- -- -- -- - END OF VB 2. CODE - -- -- -- -- -- -- -- -- //- -- -- -- -- -- -- START OF ARDUINO SKETCH - -- -- -- -- -- -- -- -- //// Mixed by: Hazim Bitar// Based on: Science Guy 1. Tube tutorial http: //youtu. Sfy. XOXj. 8int led. Pin = 1. 3; // the number of the LED pinvoid setup() {Serial. Mode(led. Pin, OUTPUT); // set LED as outputdigital. Write(led. Pin, LOW); //turn off LED}void loop(){while (Serial. Serial. read() - '0'; // deduct ascii value of '0' to find numeric value of sent numberif (val == 1) { // test for command 1 then turn on LEDSerial. LED on"); digital. Write(led. Pin, HIGH); // turn on LED}else if (val == 0) // test for command 0 then turn off LED{Serial. LED OFF"); digital. Write(led. Pin, LOW); // turn off LED}else // if not one of above command, do nothing{//val = val; }Serial. Serial. flush(); // clear serial port}//- -- -- -- -- -- -- END OF ARDUINO SKETCH - -- -- -- -- -- -- -- -- .
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |