EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: jonbasauri on August 11, 2020, 12:52:03 am

Title: Creating a Program for a function generator
Post by: jonbasauri on August 11, 2020, 12:52:03 am
Hello guys, so i just bought a function generator and i want to connect the ports from my program. The folder of the driver came with a program in vb6, so that makes me dont need to create the full program.
Even if im not a pro in visual basic, i translated almost everything into vb.net, but when i run the program it gives me the following error when runs Writefile.
System.AccessViolationException

i have searched in lot of websites and forums and tried almost everything but i dont know how to fix it. when every data arrives to "writefile" is like this:

WriteFile(hComm, bytebuf_t(0), wLen, gLen, 0&)

hcomm: a large number, always something like 42221250801565695
bytebuf_t(0): 115
wlen: 14
glen:0
0&: i dont really get this.

also, the writefile command is this.

Public Declare Function WriteFile Lib "kernel32" (ByVal hCommDev As Long, lpBuffer As Byte,
        ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long

Help please...
Title: Re: Creating a Program for a function generator
Post by: ledtester on August 11, 2020, 12:55:50 am
This page describes all of the parameters:

https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
Title: Re: Creating a Program for a function generator
Post by: jonbasauri on August 11, 2020, 01:32:55 am
Yes i already read about that, but since doesnt appear the error i got, i dont really know how to fix it. i searched for that error too and its like some the exception that is thrown when there is an attempt to read or write protected memory.
Title: Re: Creating a Program for a function generator
Post by: PlainName on August 12, 2020, 12:57:39 am
I know nothing about any of this, but that hasn't stopped me before :)

Quote
hcomm: a large number, always something like 42221250801565695

That's -1 isn't it? Usually something like that says "Oops, not a valid thing". Based on that I'd be looking at whatever opened hcomm to check that it's actually opening whatever it is properly.

Edit: got that wrong. But I'd still be looking at the open to check it's working.
Title: Re: Creating a Program for a function generator
Post by: ledtester on August 12, 2020, 01:30:42 am
I know nothing about any of this, but that hasn't stopped me before :)

Quote
hcomm: a large number, always something like 42221250801565695

That's -1 isn't it? Usually something like that says "Oops, not a valid thing". Based on that I'd be looking at whatever opened hcomm to check that it's actually opening whatever it is properly.

Edit: got that wrong. But I'd still be looking at the open to check it's working.

hcomm is a pointer to a data structure so it is essentially a 64-bit address which is why it it's so large. The only value I would be concerned about is the value 0 which usually indicates the open failed.
Title: Re: Creating a Program for a function generator
Post by: ledtester on August 12, 2020, 01:32:36 am
Hello guys, so i just bought a function generator and i want to connect the ports from my program.
...

Which function generator did you get? It's likely there is alternative software to talk to it.
Title: Re: Creating a Program for a function generator
Post by: PlainName on August 12, 2020, 01:57:45 am
Quote
The only value I would be concerned about is the value 0 which usually indicates the open failed

That would be the clincher, yes, but a -1 (which, I stress, this isn't) might indicate assignment from a stream rather than a pointer to the stream, if you follow. That might produce the state exception as opposed to something trapping a NULL reference.
Title: Re: Creating a Program for a function generator
Post by: ledtester on August 12, 2020, 02:26:20 am
Quote
WriteFile(hComm, bytebuf_t(0), wLen, gLen, 0&)

What is bytebuf_t(0)? Does it refer to address 0? If so, I'm sure there's your problem.

From this page:

https://stackoverflow.com/questions/37710294/unhandled-exception-system-accessviolationexception-attempted-to-read-or-writ

it's clear that System.AccessViolationException refers to attempting to access memory you shouldn't be.
Title: Re: Creating a Program for a function generator
Post by: Renate on August 29, 2020, 09:16:10 pm
Have you solved this yet?
The 2nd and 4th parameters are pointers.
That is why you are getting illegal access.
If you were writing this in C the errors would be immediately apparent.
In VB it takes your (incorrect) declaration.
I'm happy to say, I've forgotten how to work with pointers in VB.
Note that the returned bytes processed argument (#4) is required, you can't use NULL.
Title: Re: Creating a Program for a function generator
Post by: Tony_G on August 29, 2020, 09:30:41 pm
Are you trying to access it via a com port in VB.NET?

It wasn't clear to me that this is the case. If you are then you shouldn't try to do it the way it was done in VB6. I did a little sample that shows using a com port to talk to a Maxbotics MB1040 sensor over serial - https://github.com/TGoodhew/GPIBUtils/tree/master/MB1040SerialDemo

Let me know if you're trying to use a custom driver for Windows as you'll need to do something else for VB.NET.

TonyG