Author Topic: Help! I Can't Find My Entry Point!  (Read 15204 times)

0 Members and 1 Guest are viewing this topic.

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #25 on: April 29, 2015, 01:17:42 am »
I can code VB or VB.net 2010 to accomplish non-hardware complicated software goals.

My question is how to get VB or VB.net 2010 to do what I want since i want to be able to use the XP PC parallel port, to have access to manipulate the parallel port registers, to use inp(port,data) and out(port,data) and so have full and direct control of the parallel port hardware.

My whole post is exactly about how to get access to the parallel port, to work with XP using VB6 or VB.net 2010.
« Last Edit: April 29, 2015, 01:22:07 am by SuzyC »
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Help! I Can't Find My Entry Point!
« Reply #26 on: April 29, 2015, 01:26:34 am »
Quote
I want to work with VB6 (It seems so much easier to work with, so much less complicated than VB.net) or VB.net 2010. I have no trouble creating the code to do what I want with VB6. VB.net takes about 5x as much time to accomplish anything but I have created some serious programs with it.

So you are having trouble getting VB6 to do what you want?

The VB6 way is how Mechatrommer has explained with API calls to the DLL, the VB.NET way is just porting the code across from C#.

You seemed to imply by the quote that you have VB6 already working but want to get it working in VB.NET.

I can probably have a 5 minute look at it tomorrow if nobody else has replied in the meanwhile, just trying to get it clear what you are after.
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #27 on: April 29, 2015, 01:45:38 am »
Mechattrommer, you say, "btw parallel port is dead. try learning ftdi usb... fwiw"

I don't know anything about getting a USB based interface to work with VB as well..I don't have a clue how to do this.

Could you explain how to do this with a visual basic program tp input/output bytes with a simple handshake PC<---> FTDI<-->MCU?

Where to I get the info I would need to learn how to use FTDI USB solutions with VB?
« Last Edit: April 29, 2015, 01:55:09 am by SuzyC »
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #28 on: April 29, 2015, 01:50:00 am »
Thanks Wilksey, but No, I don't have the parallel port working with any language, that is, working with anything newer than QBASIC.
« Last Edit: April 29, 2015, 01:54:09 am by SuzyC »
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: Help! I Can't Find My Entry Point!
« Reply #29 on: April 29, 2015, 02:00:59 am »
The VB6 way is how Mechatrommer has explained with API calls to the DLL, the VB.NET way is just porting the code across from C#.

Mechattrommer, you say, "btw parallel port is dead. try learning ftdi usb... fwiw"

SuzyC, Wilksey is referring to Mechatrommer's post #15, which explains exactly how to get these functions that you need.
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Help! I Can't Find My Entry Point!
« Reply #30 on: April 29, 2015, 02:03:56 am »
I used to use INPOUT32 to read from the port, you do have to make sure that your base address is as expected though (usually 378H).
Have a look here: http://www.codeproject.com/Articles/15020/Reading-from-Parallel-Port-using-Inpout-dll

Low level routines such as inp and out will crash if you get the address wrong don't forget, so you need to look in "device manager" to make sure the base address is correct!

With regards to FTDI, it just creates a "COM" port on your machine, which you can use the .NET SerialPort or the ACTIVE-X MSCOMM OCX in VB6 to essentially create a terminal to communicate with the device.  There are other ways but this is the best method.

Easiest way to learn is to get a few USB -> RS232 adaptors and a null modem cable, or a dual USB to RS232 adaptor and null modem cable and simulate a comms link.

Out of interest, what are you planning on doing with your parallel port?  I've used it to drive an LCD and LED matrix before and also as a interface between the PC and a set of I2C and SPI sensors with a CPLD in between to do the interfacing, but moved on to RS232 after that as it was easier to drop a RS485 transceiver and run a bus.
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #31 on: April 29, 2015, 03:05:52 am »
Thanks Wilksley, I took a look at the FTDI sample programs downloadable from their website, but I am sure that the simplicity of working with the parallel port is preferable to the USB handshake latency, and I saw there is possible problems of PC shutdown and crashing, and I also saw that there is a significantly greater coding complexity using the FTDI  with a serial code interface in VB.

I want my MCU to use a periodic timed fixed-length serial stream, send debug packets of info from my PIC projects just using a  single pin on the MCU, use the RS232 Async UART, the TX pin to output at the fastest baud rate with no handshake  to a fast (64MHz clock) dedicated mcu's IRQ pin acting as an I/O cache/buffer to immediately catch the TX stream output, buffer this fast stream and cache this data. For this, I can use another  PICC or Arduino dev. board. which then re-transmits this data at high speed to a VB  program using the PC parallel port, using the parallel port control lines to make a handshake interface. All this so I can then log, save this data, or display a lot of data in near real-time from my MCU, else to debug program code, even graph this data to see my MCU's program operation, or to examine vars etc.  I want the PC and MCU to work as fast as possible and not slow down the PC, wasted time managing complex handshaking.

Ok, to put it in another way, I am just creating my own any MCU chip universal MCU development/debugging system.
« Last Edit: April 30, 2015, 12:36:10 am by SuzyC »
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11639
  • Country: my
  • reassessing directives...
Re: Help! I Can't Find My Entry Point!
« Reply #32 on: April 29, 2015, 05:40:46 am »
I don't know anything about getting a USB based interface to work with VB as well..I don't have a clue how to do this.
Could you explain how to do this with a visual basic program tp input/output bytes with a simple handshake PC<---> FTDI<-->MCU?
Where to I get the info I would need to learn how to use FTDI USB solutions with VB?
i made the ftdi "wrapper" so to speak a while ago. i havent get back to ftdi project, so i may be rusted... strictly speaking.... for VB6 fanboys only (attached zip) you need to figure that out OSSHW is all about "lack of documentation" :P

That is, if I only new how to jump through the software hoops to implement them in Microsoft's hardware crippled VB variants.
mourning will not help. you give me one "hardware not crippled" IDE and i'll buy you a coffee.

I don't have much knowledge about C++ programming especially because it often involves dealing with the Windows API or  with creating code for interfacing with hardware and .dll creation and even the GUI with maybe hundreds of poorly documented and even some needed undocumented esoteric system functions and Win API calls..all this seems horribly daunting to attempt without years of university IT curriculum, then work experience in the real world, must be followed by a few years of on the job experience with fellow coders to be any good at it.
so you want to do VB through hardware access all in 1 day? not even with API calls? good luck! there are meaner beast than windows API... win driver development that is... and i've not touch it since i dont develop windows compatible hardware back then during "learning period" and to get USB license for a device can break a bank. do you think the "learning period" is 1 or 3 days like? i've been VB for ~15years and am still self learning... that my reply #15 is consisted in that 15 years of learning and you got it in 1 night and the zip below is weeks of sweat. make your 1st step today or never!

I want my MCU to use a periodic timed fixed-length serial stream... dedicated mcu's IRQ pin acting as...
you keep talking archaic terminology. IRQ, ie "interrupt request" vector in PC (in real time), in "Newer Windowss" is dead! unless you go back to DOS or Win311
« Last Edit: April 29, 2015, 06:03:10 am by Mechatrommer »
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #33 on: April 29, 2015, 09:16:33 am »
Mechatrommer, I don't think you understood my explanation, I intend to use an MCU IRQ on an external MCU that grabs the data stream from the project  MCU being monitored/debugged. This intermediary MCU then uses the parallel port register's handshake lines, with the PC. This caching MCU has a PC-MCU handshake that does not use PC IRQ's,  the PC just uses polling of port control pins.

Although a VB interface to the parallel port may have taken quite some time for someone to develop a successful software method, I assume that we don't all have to go to the trouble of re-inventing the wheel with a legacy port interface.

Isn't it the purpose of this forum to share helpful software techniques so to both get help and help others with software/hardware problems?

I know that interfacing hardware like the parallel port to VB will require API, that is quite obvious.
« Last Edit: April 29, 2015, 09:27:05 am by SuzyC »
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Help! I Can't Find My Entry Point!
« Reply #34 on: April 29, 2015, 10:33:24 am »
So, you're sending a serial data stream over a parallel bus?

You're reading far too much into the FTDI scenario, the FTDI driver will create a VCP on your machine which allows you to access a Serial (COM) port, if you have never done serial port stuff then you won't understand, but after your project I would certainly make time to learn to how interface a MCU -> RS232 transceiver -> PC and write a little "terminal" program to learn how it all works.

Did you check your LPT base address in device manager to make sure it was 378H?

Try the codeproject example I linked to, it didn't crash, but I don't have a PC with a parallel port any more!
 

Offline deephaven

  • Frequent Contributor
  • **
  • Posts: 796
  • Country: gb
  • Civilization is just one big bootstrap
    • Deephaven Ltd
Re: Help! I Can't Find My Entry Point!
« Reply #35 on: April 29, 2015, 10:45:11 am »
Trying to directly drive the LPT port in Windows XP or later OS's is a bad idea, you won't be able to stop the OS periodically accessing the port and messing with your interface.
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11639
  • Country: my
  • reassessing directives...
Re: Help! I Can't Find My Entry Point!
« Reply #36 on: April 29, 2015, 11:31:28 am »
Isn't it the purpose of this forum to share helpful software techniques so to both get help and help others with software/hardware problems?
yes we just did, its just depends on how you interpret it. i guess the people who are very well versed in what you really want have already long gone. so we gave you the advice of today. one stated he dont have parallel port around anymore, heck i dont even have serial in my 6yrs old pc here. how can we help? cheers ;)
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3240
  • Country: gb
Re: Help! I Can't Find My Entry Point!
« Reply #37 on: April 29, 2015, 11:36:42 am »
Trying to directly drive the LPT port in Windows XP or later OS's is a bad idea, you won't be able to stop the OS periodically accessing the port and messing with your interface.

Why would the OS periodically access the port?  I have bit bashed I2C and SPI through a parallel port on XP, Vista and Windows 7 with no problems at all, other than overall speed.
 

Offline deephaven

  • Frequent Contributor
  • **
  • Posts: 796
  • Country: gb
  • Civilization is just one big bootstrap
    • Deephaven Ltd
Re: Help! I Can't Find My Entry Point!
« Reply #38 on: April 29, 2015, 11:41:24 am »
Trying to directly drive the LPT port in Windows XP or later OS's is a bad idea, you won't be able to stop the OS periodically accessing the port and messing with your interface.

Why would the OS periodically access the port?  I have bit bashed I2C and SPI through a parallel port on XP, Vista and Windows 7 with no problems at all, other than overall speed.

I don't know, but that was what was reported by our software guy when we went from W95 to XP.
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Help! I Can't Find My Entry Point!
« Reply #39 on: April 29, 2015, 11:52:01 am »
Sounds like a softie blaming hardware!

XP upwards have no issues using a parallel port, the OS only does what it is told, it doesn't need to access the port periodically.

I have used LPT on XP quite a few times and have had no issues at all.
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #40 on: April 29, 2015, 01:57:48 pm »
Well I declare, it is time for me to halt my exceptions and take your pointers and try to learn how to do this myself!

Mechattrommer, it's time for me to learn something about C# or C++ to solve this problem.

Question, I assume your code snippet hints for Winio applies to Visual Basic.Net 2010?

  public declare function InitializeWinIo lib "WinIo32.dll" () as boolean
  public declare sub ShutdownWinIo lib "WinIo32.dll" ()
  public declare function MapPhysToLin lib "WinIo32.dll" (byref PhysStruct as tagPhysStruct) as byte


I know this is a newbie question, but in conversion, how do I know when to use "byref" rather than "byval"? in the conversion?

Also, I have not succeeded in getting the native C code version of Winio to work..others say it worked ok when the .dll was copied to the correct directory..I need some clarification here. If I compile the converted to .net 2010 C code for a "Release" result, then if I just copy the Winio32.dll to the Release folder it should work?  The Winio32.dll doesn't need to be also to be   Windows\System32 ?

First, I need to get this program to work in C, then convert it to VB.net 2010.


« Last Edit: April 29, 2015, 02:01:09 pm by SuzyC »
 

Offline neslekkim

  • Super Contributor
  • ***
  • Posts: 1305
  • Country: no
Re: Help! I Can't Find My Entry Point!
« Reply #41 on: April 29, 2015, 02:54:17 pm »
The sample code IS C#, can't you just use that?

Some times you say that the samples work fine, next you say you cannot use them.. Do they work or not?


Question, I assume your code snippet hints for Winio applies to Visual Basic.Net 2010?

  public declare function InitializeWinIo lib "WinIo32.dll" () as boolean
  public declare sub ShutdownWinIo lib "WinIo32.dll" ()
  public declare function MapPhysToLin lib "WinIo32.dll" (byref PhysStruct as tagPhysStruct) as byte


This is VB6 code as he stated earlier, Look at this to see the difference in declarations for C#, VB.net and VB6:
http://www.pinvoke.net/default.aspx/kernel32.outputdebugstring

I know this is a newbie question, but in conversion, how do I know when to use "byref" rather than "byval"? in the conversion?

This is one of the first things you need to know, if you have created lots of applications before, and you DON'T know what byref and byval means, i don't belive your earlier statement.
Atleast google it.. http://www.dotnetperls.com/byval-byref

Also, I have not succeeded in getting the native C code version of Winio to work..others say it worked ok when the .dll was copied to the correct directory..I need some clarification here. If I compile the converted to .net 2010 C code for a "Release" result, then if I just copy the Winio32.dll to the Release folder it should work?  The Winio32.dll doesn't need to be also to be   Windows\System32 ?

Or you can take the two files as I mentioned earlier, BOTH the dll AND the sys file, and add them in your project, and mark them as files to be copied to your output folder on build, so you don't need to copy manually.

First, I need to get this program to work in C, then convert it to VB.net 2010.

No, you need either to learn C#, You don't need to convert this to VB.net (and forget the 2010 thing, vs2010 can use .net v2.0 and up to .net v4.0, that is what you need to choose)
If you absolutely need to use VB, it's best to just create an small C# classlibrary, copy the code that was defined in the samples for the winio declarations, and then use this classlibrary from VB, that is what is meant with an wrapper, but if you search around on pinvoke.net, you will find enough information to be able to convert the code to vb.net, but you need to try to read an search a bit to learn this.
 

Offline nitro2k01

  • Frequent Contributor
  • **
  • Posts: 843
  • Country: 00
Re: Help! I Can't Find My Entry Point!
« Reply #42 on: April 29, 2015, 03:06:25 pm »
Seriously, do yourself a favor and go serial. When you're using the parallel port, your thread needs to be running for each bit you transfer. With a serial port, all timing sensitive parts are abstracted away. On the PC side, you have a standard interface that is still maintained, and is rather portable between platforms. On the microcontroller side, you have a hardware UART, so that the CPU only needs to be busy once for every byte, instead of once for every bit. A byte goes in on one side and comes out on the other side. Easy. FT232 offers a maximum of 3 MBaud. Is this enough for your application?

It is true that USB can only handle data transfers a maximum of 1000 times per second, but if you're sending larger packets than 1 byte, that shouldn't be a huge problem. Only if you send one byte from the PC and stall until you get a byte back will this be a problem.
Whoa! How the hell did Dave know that Bob is my uncle? Amazing!
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Help! I Can't Find My Entry Point!
« Reply #43 on: April 29, 2015, 05:12:15 pm »
First of all, read the post that neslekkim has posted, it contains some useful pointers.

I second dropping 2010 from the VS/VB.NET, 2010 is just the edition that as around when .NET 3.5 / 4 was released (can't remember which one). you have 2013 now perhaps newer, I still use 2010.

VB6 and VB.NET are 2 completely different languages.

C and C# is NOT the same, the examples are in C# (C SHARP).

Take one of the examples, compile it, run it, it will complain it can't find some files, find the "bin\x86\release" folder where the .exe is, and copy the .dll and the .sys to that folder, if you still get errors or issues and you are compiling on a x86 system then there must be some underlying issue with the hardware or hardware driver, as this works "out of the box" for me, i've done it on XP x32 and 7 x32, if you are running on x64 make sure the architecture is set to x86 explicitly rather than "any".
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #44 on: April 29, 2015, 10:55:47 pm »
Thanks Wilksey, I just realized that I didn't copy over the Winio32.sys file. I didn't think that I needed it, didn't know that it was needed to include this file for X86 32-bit windows. I will try adding the .sys file to the Release folder and this should get it to work with C# in .net 2010.

Update, InpOut32.dll does work with my VB6 VBasic basic program, the VB IDE was "having a problem and must close." issue when I entered a port number used on a PCI Parallel Port card of 0xDFE0 for either an input or output. It does work ok with any port# <32767.  Seems the integer type in the .Dll is a signed 16-bit integer.


Thank you very much Neslekkim for your detailed help.
A compiled version Winio.exe came with the .zip file downloaded and that worked with a click on my Desktop.
My compiled .net 2010 would not work, but as I noted above, it might be because I hadn't copied the .sys file to the Release directory. It compiles to have a result compatible with .net ver 2.5.
I would like to learn more about C#, but I can see it is quite different and sophisticated compared to MCU C, but my first impression just looking at keywords shows it has great capability if one knows how to use it. I am going to check out the links tomorrow you posted.

Nitro2k01:  Thanks for  your suggestion. I succeeded in getting Inout32.dll to work and made a loop test on my PC.

My VB6 program running in the VB IDE managed to achieve >260 thousand loops each second.

Dim N as Long
Dim T, u as Integer
T=Timer
DoIt:

     out &H378, 0
     u=inp(&H378)

     N=N+1

    if(Timer-T)<10 then goto DoIt

End

 That's pretty fast, and there is only two short lines of initialization to use  InpOut32.dll so as to create an interface to this .dll to VB6.  Needless to say, when the program is inputting and outputting in a continuous loop, my 6-core PC is always running at least one core at >50% of computing capacity according to the Task Manager graph.

« Last Edit: April 30, 2015, 12:14:20 am by SuzyC »
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #45 on: April 30, 2015, 12:26:37 am »
Many Thanks to Everyone who offered their comments and help!

Thanks nitro2kO1,

All PC motherboards's with a Parallel Port have a bidirectional single port used as a byte-wide input and output port so I don't get it that you say the PC is doing business by the bit, it is obviously inputting and outputting one byte at a time.

I would very much like to see a listing of a VB.net or VB6 interface to the USB serial solution.

This is all it takes in VB6:

Private Declare Function Inp Lib "inpout32.dll" _
Alias "Inp32" (ByVal PortAddress As Integer) As Integer

Private Declare Sub Out Lib "inpout32.dll" _
Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

You can argue about the speed but I love the absolute simplicity of this PC to external world hardware interface solution.
« Last Edit: April 30, 2015, 12:47:02 am by SuzyC »
 

Offline nitro2k01

  • Frequent Contributor
  • **
  • Posts: 843
  • Country: 00
Re: Help! I Can't Find My Entry Point!
« Reply #46 on: April 30, 2015, 12:46:29 am »
Thanks nitro2kO1,

All PC motherboards's with a Parallel Port have a bidirectional single port used as a byte-wide input and output port so I don't get it that you say the PC is doing business by the bit, it is obviously inputting and outputting one byte at a time.
I thought for some reason from your description that you were only using one pin and was clocking individual bits. The general point still stands though, no timing intensive work is being offloaded to hardware, and you effectively dedicate one CPU core just for transferring the data. Plus, LPT ports will start to go away, and will be harder to access for every Windows version.

For a serial port, You need the MSComm control. However, I'm not sure if you can open a port at 3 MBaud in VB6 (even if the port supports it) or if you are restricted to standard baud rates.

https://msdn.microsoft.com/en-us/library/aa259393(v=vs.60).aspx
Whoa! How the hell did Dave know that Bob is my uncle? Amazing!
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #47 on: April 30, 2015, 01:00:36 am »
According to Microsoft MSDN and using MSComm, all you need to do is:

Use the MSComm control in Visual Basic .NET to access serial ports
Because no Microsoft .NET Framework classes exist to access the communications resources that connected to your computer, you can use the MSComm control in Microsoft Visual Basic 6.0. The MSComm control provides serial communications for your application by enabling the transmission and reception of data through a serial port. To implement basic serial communications by using a modem, follow these steps:

(There are some caveats: This example uses 9600 baud, only applicable up to VB.net 2003 and to establish communication with the LPTx port, you must stop the Print Spooler service. To do this, use the Services tool)

in Administrative Tools.
    Start Microsoft Visual Studio .NET.
    On the File menu, point to New, and then click Project.
    Under Project Types, click Visual Basic Projects.
    Under Templates, click Console Application.
    In the Name box, type MyConsoleApplication, and then click OK.

    By default, Module1.vb is created.
    Right-click the MyConsoleApplication project, and then click Add Reference.
    Click the COM tab, click Microsoft Comm Control 6.0 under Component Name, click Select, and then click OK.

    Note To use the MSComm control, you must install the related COM components of Microsoft Visual Basic 6.0 on the same computer that has Microsoft Visual Studio .NET installed.

    For more information about license issues when you use Visual Basic 6.0 controls in Visual Studio .NET, click the following article number to view the article in the Microsoft Knowledge Base:
    318597 Errors when you use Visual Basic 6.0 controls in Visual Studio .NET
    Replace the code in Module1.vb with the following code example.

    Imports MSCommLib

    Module Module1

        Sub Main()
            'New a MSComm control
            Dim MSComm1 As MSComm
            MSComm1 = New MSComm
            ' Buffer to hold input string.
            Dim Buffer As String
            ' Use the COM1 serial port.
            MSComm1.CommPort = 1
            ' 9600 baud, no parity, 8 data, and 1 stop bit.
            MSComm1.Settings = "9600,N,8,1"
            ' Tell the control to read the whole buffer when Input is used.
            MSComm1.InputLen = 0
            ' Open the serial port.
            MSComm1.PortOpen = True
            Console.WriteLine("Open the serial port.")
            ' Tell the control to make the Input property return text data.
            MSComm1.InputMode() = InputModeConstants.comInputModeText
            'Clear the receive buffer.
            MSComm1.InBufferCount() = 0
            ' Send the attention command to the modem.
            MSComm1.Output = "ATV1Q0" & Chr(13)
            Console.WriteLine("Send the attention command to the modem.")
            Console.WriteLine("Wait for the data to come back to the serial port...")
            ' Make sure that the modem responds with "OK".
            ' Wait for the data to come back to the serial port.
            Do
                Buffer = Buffer & MSComm1.Input
            Loop Until InStr(Buffer, "OK" & vbCrLf)
            ' Read the "OK" response data in the serial port.
            ' Close the serial port.
            Console.WriteLine("Read the OK response data in the serial port.")
            MSComm1.PortOpen = False
            Console.WriteLine("Close the serial port.")
        End Sub

    End Module

   
Use platform invoke services to call Win32 API functions in Visual Basic .NET to access serial and parallel ports
To do this, follow these steps:

    Start Microsoft Visual Studio .NET.
    On the File menu, point to New, and then click Project.
    Under Project Types, click Visual Basic Projects.
    Under Templates, click Console Application.
    In the Name text box, type MyConsoleApplication, and then click OK.

    By default, Module1.vb is created.
    Add the following code to Module1.vb before the Module Module1 statement:

    Option Strict On

    ' Define a CommException class that inherits from the ApplicationException class,
    ' and then throw an object of type CommException when you receive an error message.
    Class CommException
       Inherits ApplicationException
       Sub New(ByVal Reason As String)
          MyBase.New(Reason)
       End Sub
    End Class

    Declare structures, constants, and references to external functions that are in Kernel32.dll

    To call unmanaged functions from your managed Visual Basic .NET application, you must declare references to the structures that you pass as parameters to the unmanaged functions, and you must declare the constants that you pass as parameters to the unmanaged functions. To do this, add the following code to Module1.vb after the Module Module1 statement:

    'Declare structures.
       Public Structure DCB
       Public DCBlength As Int32
       Public BaudRate As Int32
       Public fBitFields As Int32 'See Comments in Win32API.Txt
       Public wReserved As Int16
       Public XonLim As Int16
       Public XoffLim As Int16
       Public ByteSize As Byte
       Public Parity As Byte
       Public StopBits As Byte
       Public XonChar As Byte
       Public XoffChar As Byte
       Public ErrorChar As Byte
       Public EofChar As Byte
       Public EvtChar As Byte
       Public wReserved1 As Int16 'Reserved; Do Not Use
    End Structure

    Public Structure COMMTIMEOUTS
       Public ReadIntervalTimeout As Int32
       Public ReadTotalTimeoutMultiplier As Int32
       Public ReadTotalTimeoutConstant As Int32
       Public WriteTotalTimeoutMultiplier As Int32
       Public WriteTotalTimeoutConstant As Int32
    End Structure

    'Declare constants.
    Public Const GENERIC_READ As Int32 = &H80000000
    Public Const GENERIC_WRITE As Int32 = &H40000000
    Public Const OPEN_EXISTING As Int32 = 3
    Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
    Public Const NOPARITY As Int32 = 0
    Public Const ONESTOPBIT As Int32 = 0

    'Declare references to external functions.
    Public Declare Auto Function CreateFile Lib "kernel32.dll" _
       (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
          ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
             ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
                ByVal hTemplateFile As IntPtr) As IntPtr

    Public Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
       ByRef lpDCB As DCB) As Boolean

    Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
       ByRef lpDCB As DCB) As Boolean

    Public Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

    Public Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean

    Public Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32, _
          ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean

    Public Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
       ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, _
          ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean

    Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean

    Before you can access a serial port or a parallel port, you must obtain a handle to the appropriate port and then configure the port communications. To do this, add the following initialization code to Module1.vb after the Sub Main statement.

    Note To establish communication with the LPTx port, you must stop the Print Spooler service. To do this, use the Services tool in Administrative Tools.

    ' Declare the local variables that you will use in the code.
    Dim hSerialPort, hParallelPort As IntPtr
    Dim Success As Boolean
    Dim MyDCB As DCB
    Dim MyCommTimeouts As COMMTIMEOUTS
    Dim BytesWritten, BytesRead As Int32
    Dim Buffer() As Byte

    ' Declare the variables to use for encoding.
    Dim oEncoder As New System.Text.ASCIIEncoding
    Dim oEnc As System.Text.Encoding = oEncoder.GetEncoding(1252)

    ' Convert String to Byte().
    Buffer = oEnc.GetBytes("Test")
         Try
             ' Access the serial port.
             Console.WriteLine("Accessing the COM1 serial port")
             ' Obtain a handle to the COM1 serial port.
             hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
             ' Verify that the obtained handle is valid.
             If hSerialPort.ToInt32 = -1 Then
                Throw New CommException("Unable to obtain a handle to the COM1 port")
             End If
             ' Retrieve the current control settings.
             Success = GetCommState(hSerialPort, MyDCB)
             If Success = False Then
                Throw New CommException("Unable to retrieve the current control settings")
             End If
             ' Modify the properties of the retrieved DCB structure as appropriate.
             ' WARNING: Make sure to modify the properties according to their supported values.
             MyDCB.BaudRate = 9600
             MyDCB.ByteSize = 8
             MyDCB.Parity = NOPARITY
             MyDCB.StopBits = ONESTOPBIT
             ' Reconfigure COM1 based on the properties of the modified DCB structure.
             Success = SetCommState(hSerialPort, MyDCB)
             If Success = False Then
                Throw New CommException("Unable to reconfigure COM1")
             End If
             ' Retrieve the current time-out settings.
             Success = GetCommTimeouts(hSerialPort, MyCommTimeouts)
             If Success = False Then
                Throw New CommException("Unable to retrieve current time-out settings")
             End If
             ' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
             ' WARNING: Make sure to modify the properties according to their supported values.
             MyCommTimeouts.ReadIntervalTimeout = 0
             MyCommTimeouts.ReadTotalTimeoutConstant = 0
             MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
             MyCommTimeouts.WriteTotalTimeoutConstant = 0
             MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
             ' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
             Success = SetCommTimeouts(hSerialPort, MyCommTimeouts)
             If Success = False Then
                Throw New CommException("Unable to reconfigure the time-out settings")
             End If
             ' Write data to COM1.
             Console.WriteLine("Writing the following data to COM1: Test")
             Success = WriteFile(hSerialPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
             If Success = False Then
                Throw New CommException("Unable to write to COM1")
             End If
             ' Read data from COM1.
             Success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, IntPtr.Zero)
             If Success = False Then
                Throw New CommException("Unable to read from COM1")
             End If
          Catch ex As Exception
             Console.WriteLine(Ex.Message)
          Finally
             ' Release the handle to COM1.
             Success = CloseHandle(hSerialPort)
             If Success = False Then
                Console.WriteLine("Unable to release handle to COM1")
             End If
          End Try

          Try
             ' Parallel port.
             Console.WriteLine("Accessing the LPT1 parallel port")
             ' Obtain a handle to the LPT1 parallel port.
             hParallelPort = CreateFile("LPT1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
             ' Verify that the obtained handle is valid.
             If hParallelPort.ToInt32 = -1 Then
                Throw New CommException("Unable to obtain a handle to the LPT1 port")
             End If
             ' Retrieve the current control settings.
             Success = GetCommState(hParallelPort, MyDCB)
             If Success = False Then
                Throw New CommException("Unable to retrieve the current control settings")
             End If
             ' Modify the properties of the retrieved DCB structure as appropriate.
             ' WARNING: Make sure to modify the properties according to their supported values.
             MyDCB.BaudRate = 9600
             MyDCB.ByteSize = 8
             MyDCB.Parity = NOPARITY
             MyDCB.StopBits = ONESTOPBIT
             ' Reconfigure LPT1 based on the properties of the modified DCB structure.
             Success = SetCommState(hParallelPort, MyDCB)
             If Success = False Then
                Throw New CommException("Unable to reconfigure LPT1")
             End If
             ' Retrieve the current time-out settings.
             Success = GetCommTimeouts(hParallelPort, MyCommTimeouts)
             If Success = False Then
                Throw New CommException("Unable to retrieve current time-out settings")
             End If
             ' Modify the properties of the retrieved COMMTIMEOUTS structure as appropriate.
             ' WARNING: Make sure to modify the properties according to their supported values.
             MyCommTimeouts.ReadIntervalTimeout = 0
             MyCommTimeouts.ReadTotalTimeoutConstant = 0
             MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
             MyCommTimeouts.WriteTotalTimeoutConstant = 0
             MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
             ' Reconfigure the time-out settings, based on the properties of the modified COMMTIMEOUTS structure.
             Success = SetCommTimeouts(hParallelPort, MyCommTimeouts)
             If Success = False Then
                Throw New CommException("Unable to reconfigure the time-out settings")
             End If
             ' Write data to LPT1.
               ' Note: You cannot read data from a parallel port by calling the ReadFile function.

             Console.WriteLine("Writing the following data to LPT1: Test")
             Success = WriteFile(hParallelPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)
             If Success = False Then
                Throw New CommException("Unable to write to LPT1")
             End If
          Catch ex As Exception
             Console.WriteLine(Ex.Message)
          Finally
             ' Release the handle to LPT1.
             Success = CloseHandle(hParallelPort)
             If Success = False Then
                Console.WriteLine("Unable to release handle to LPT1")
             End If
          End Try

          Console.WriteLine("Press ENTER to quit")
          Console.ReadLine()

   
« Last Edit: April 30, 2015, 01:28:24 am by SuzyC »
 

Offline neslekkim

  • Super Contributor
  • ***
  • Posts: 1305
  • Country: no
Re: Help! I Can't Find My Entry Point!
« Reply #48 on: April 30, 2015, 05:20:42 am »
You still mix vb6 with vb.net.. Don't use MSComm in .net, use System.IO.Ports:
http://www.ontrak.net/serialport.htm
 

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Re: Help! I Can't Find My Entry Point!
« Reply #49 on: April 30, 2015, 12:53:30 pm »
neslekkim...Thanks Again!

The original downloaded winio.zip came with the C code and binaries and sources required to create the Winio.exe application and Winio.dll.
I immediately noticed upon examining the C source files with Wordpad that they had .net calling conventions, were not compatible with VB6.
I have Visual Studio 2010 and so I used this to try to compile the C code. Upon first opening with VS2010, they needed to be updated to be used by the C IDE in Visual Studio 2010.. and this was successful without error. However, I could not get the output files to debug and I attached to my posts here the revised Wino.zip with the converted source files.

Update: I copied the Winio32.dll and Winio32.sys files to the Release directory and it works perfect in C# IDE..but it ain't no VBasic code yet.

My confusion arises from Vis Studo 2010 showing that it has support for C++ 2010, C# 2010 and Visual F# and the converted code does not expose the type of code generated..though definitely I clearly understand now that the code produced is not Vis Studio VBasic 2010, my choice to work with if I could convert the code.
« Last Edit: April 30, 2015, 01:46:49 pm by SuzyC »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf