Creating a Program for a function generator - Page 1
Forum section: electronics / general

author: jonbasauri
author_rank: Newbie
timestamp: August 11, 2020, 12:52:03 am
content: 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.AccessViolationExceptioni 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 42221250801565695bytebuf_t(0): 115wlen: 14glen:00&: 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 LongHelp please...

author: ledtester
author_rank: Super Contributor
timestamp: August 11, 2020, 12:55:50 am
content: This page describes all of the parameters:https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile

author: jonbasauri
author_rank: Newbie
timestamp: August 11, 2020, 01:32:55 am
content: 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.

author: PlainName
author_rank: Super Contributor
timestamp: August 12, 2020, 12:57:39 am
content: I know nothing about any of this, but that hasn't stopped me beforeQuoteThat'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.

author: ledtester
author_rank: Super Contributor
timestamp: August 12, 2020, 01:30:42 am
content: Quote from: dunkemhigh on August 12, 2020, 12:57:39 amhcomm 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.

author: ledtester
author_rank: Super Contributor
timestamp: August 12, 2020, 01:32:36 am
content: Quote from: jonbasauri on August 11, 2020, 12:52:03 amWhich function generator did you get? It's likely there is alternative software to talk to it.

author: PlainName
author_rank: Super Contributor
timestamp: August 12, 2020, 01:57:45 am
content: QuoteThat 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.

author: ledtester
author_rank: Super Contributor
timestamp: August 12, 2020, 02:26:20 am
content: QuoteWhat 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-writit's clear that System.AccessViolationException refers to attempting to access memory you shouldn't be.

author: Renate
author_rank: Super Contributor
timestamp: August 29, 2020, 09:16:10 pm
content: 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.

author: Tony_G
author_rank: Super Contributor
timestamp: August 29, 2020, 09:30:41 pm
content: 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/MB1040SerialDemoLet 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