Recent

Author Topic: Please help with the CH341DLL  (Read 5294 times)

sabouras

  • New Member
  • *
  • Posts: 18
Please help with the CH341DLL
« on: January 02, 2023, 09:37:52 pm »
Dear All,
I am new in the pascal and i would like to guide me how to include the ch341dll into my program.
Until know i found from the Chinese manufacture some examples which have in pascal and i use it but when i start to execute the program the lazarus show's me a message that the execution has been stopped without any other information.
I am attaching the zip file which provide the manufacture which contains the pascal examples and my code.
Thank you in advance for your time.
Best regard to all.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Please help with the CH341DLL
« Reply #1 on: January 02, 2023, 09:47:00 pm »
What OS?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

sabouras

  • New Member
  • *
  • Posts: 18
Re: Please help with the CH341DLL
« Reply #2 on: January 03, 2023, 07:59:36 am »
I am using windows 10.

af0815

  • Hero Member
  • *****
  • Posts: 1409
Re: Please help with the CH341DLL
« Reply #3 on: January 03, 2023, 08:55:50 am »
I think this will be this device USB Bridge Controller CH341 http://www.wch-ic.com/products/CH341.html so an additive question is, what is behind the Bridge ?
regards
Andreas

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Please help with the CH341DLL
« Reply #4 on: January 03, 2023, 09:23:08 am »
Basically, if set up as a serial device it will- subject to having the right device driver installed- behave like a standard serial device on Windows and Linux.

It's also possible to set it up as a parallel device (printer or GPIO), see e.g. https://hackaday.com/2018/02/21/linux-adds-ch341-gpio/

Anything more than that will depend (as af0815 says) on understanding the protocol required by whatever it's attached to. As an example, it's used in a popular range of desktop signal generator.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Please help with the CH341DLL
« Reply #5 on: January 03, 2023, 09:57:30 am »
Quote
I am new in the pascal and i would like to guide me how to include the ch341dll into my program.
You have a nice set of example code that is written in/for Delphi.

Quote
Until know i found from the Chinese manufacture some examples which have in pascal and i use it but when i start to execute the program the lazarus show's me a message that the execution has been stopped without any other information.
And how have you 'converted' those Delphi examples to Lazarus ? (rethorical)

I doubt the "Stopped without any further information" unless you have done something out of the ordinary (which is not a problem, but guessing what that might be is an impossible job).

Nobody else is able to reproduce unless having the exact same hardware... a chance of 1 in ... what shall we say 100.000 ?

Quote
I am attaching the zip file which provide the manufacture which contains the pascal examples
Yes, I have seen them. The examples seem nice.

Quote
and my code.
You have no code (for Lazarus) other then a button that does nothing when you press it.

Quote
Thank you in advance for your time.
It is a matter of copy-paste... !?

So, for sure I must be misunderstanding something.

The basic idea is that, depending on what hardware you have connected to that USB device, you need to use the corresponding accompanied Delphi code (translate that to Lazarus = copy-paste). Ergo what code applies for your situation depends on what hardware you have connected. You did not mention that.

The basic commands (API calls) should be performed first like opening (CH341OpenDevice) and closing (CH341CloseDevice) the device.

You should start with that and see if that works for you /first/.

If those commands are not working for you when you run your program then I would advise to download one of the online available tools that communicate with that specific device so you are able to do some basic testing.

The package as you have does not officially support windows 10, let alone 64-bit (all provided files/drivers are 32 bit). So more information on your used OS and which version and bitness of Lazarus you use is information that is required before someone is even able to help you out.

You can do much more then you think... and that is helping us so that we can help you  :)
Today is tomorrow's yesterday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Please help with the CH341DLL
« Reply #6 on: January 03, 2023, 10:03:34 am »
Nobody else is able to reproduce unless having the exact same hardware... a chance of 1 in ... what shall we say 100.000 ?

I've got the hardware. But I'm not messing around with an undocumented DLL etc. for an OS I don't use.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

sabouras

  • New Member
  • *
  • Posts: 18
Re: Please help with the CH341DLL
« Reply #7 on: January 03, 2023, 01:00:43 pm »
I think this will be this device USB Bridge Controller CH341 http://www.wch-ic.com/products/CH341.html so an additive question is, what is behind the Bridge ?

You are right. This is a usb to uart and have another function uart to I2C

sabouras

  • New Member
  • *
  • Posts: 18
Re: Please help with the CH341DLL
« Reply #8 on: January 03, 2023, 01:09:01 pm »
This is the error which i receive every time i try to call any function.
As for the hardware is the computer as master which send data to the CH314T and that IC convert it to I2C and send them to PCA9685.
I have install the driver from the official site.Also i copy the .dll file to the project folder just in case which could not found it.

balazsszekely

  • Guest
Re: Please help with the CH341DLL
« Reply #9 on: January 03, 2023, 01:19:49 pm »
@sabouras
The dll is 64 bit. Is your Lazarus/FPC also 64 bit?

PS: Windows defender is very aggressive with the created exe/dll.

sabouras

  • New Member
  • *
  • Posts: 18
Re: Please help with the CH341DLL
« Reply #10 on: January 03, 2023, 04:51:06 pm »
@sabouras
The dll is 64 bit. Is your Lazarus/FPC also 64 bit?

PS: Windows defender is very aggressive with the created exe/dll.

Yes it is 64bit. I am attaching the version

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Please help with the CH341DLL
« Reply #11 on: January 03, 2023, 10:51:24 pm »
The dll is 64 bit. Is your Lazarus/FPC also 64 bit?
Are you sure about that GetMem ?

My toolbox says it is a 32 bit dll (and the documentation suggests it be running on non 64 bit windows versions).

I am speaking of the package as provided by TS. I am aware that there are other drivers floating around online.
Today is tomorrow's yesterday.

balazsszekely

  • Guest
Re: Please help with the CH341DLL
« Reply #12 on: January 04, 2023, 06:35:00 am »
@TRon
Quote
Are you sure about that GetMem ?

My toolbox says it is a 32 bit dll (and the documentation suggests it be running on non 64 bit windows versions).

I am speaking of the package as provided by TS. I am aware that there are other drivers floating around online.

Yes, you're right, it's a 32 bit dll. I made an elementary mistake, here is my method:
Code: Pascal  [Select][+][-]
  1. uses JwaWindows;
  2.  
  3. function GetPEType(const APath: WideString): Byte;
  4. const
  5.   PE_UNKNOWN = 0;
  6.   PE_32BIT   = 2;
  7.   PE_64BIT   = 3;
  8. var
  9.   hFile, hFileMap: THandle;
  10.   PMapView: Pointer;
  11.   PIDH: PImageDosHeader;
  12.   PINTH: PImageNtHeaders;
  13.   Base: Pointer;
  14. begin
  15.   Result := PE_UNKNOWN;
  16.  
  17.   hFile := CreateFileW(PWideChar(APath), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  18.   if hFile = INVALID_HANDLE_VALUE then
  19.   begin
  20.     CloseHandle(hFile);
  21.     Exit;
  22.   end;
  23.  
  24.   hFileMap  := CreateFileMapping(hFile, nil, PAGE_READONLY, 0, 0, nil);
  25.   if hFileMap = 0 then
  26.   begin
  27.     CloseHandle(hFile);
  28.     CloseHandle(hFileMap);
  29.     Exit;
  30.   end;
  31.  
  32.   PMapView := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
  33.   if PMapView = nil then
  34.   begin
  35.     CloseHandle(hFile);
  36.     CloseHandle(hFileMap);
  37.     Exit;
  38.   end;
  39.  
  40.   PIDH := PImageDosHeader(PMapView);
  41.   if PIDH^.e_magic <> IMAGE_DOS_SIGNATURE then
  42.   begin
  43.     CloseHandle(hFile);
  44.     CloseHandle(hFileMap);
  45.     UnmapViewOfFile(PMapView);
  46.     Exit;
  47.   end;
  48.  
  49.   Base := PIDH;
  50.   PINTH := PIMAGENTHEADERS(Base + LongWord(PIDH^.e_lfanew));
  51.   if PINTH^.Signature = IMAGE_NT_SIGNATURE then
  52.   begin
  53.     case PINTH^.OptionalHeader.Magic of
  54.       $10b: Result := PE_32BIT;
  55.       $20b: Result := PE_64BIT
  56.     end;
  57.   end;
  58.  
  59.   CloseHandle(hFile);
  60.   CloseHandle(hFileMap);
  61.   UnmapViewOfFile(PMapView);
  62. end;  
  63.  
  64.  
  65. procedure TForm1.Button1Click(Sender: TObject);
  66. var
  67.   ImageType: Byte;
  68. begin
  69.   ImageType := GetPEType(WideString('c:\windows\notepad.exe'));
  70.   case ImageType of
  71.     0: ShowMessage('unknown PE');    
  72.     2: ShowMessage('32 bit PE');
  73.     3: ShowMessage('64 bit PE');
  74.   end;
  75. end;

I forgot to replace the path.  :-[  Instead of "c:\windows\notepad.exe" we need the path to CH341DLL.DLL.
 
The point is the bitness of the exe and the dll is different. This is at least one of OP's problem.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
Re: Please help with the CH341DLL
« Reply #13 on: January 04, 2023, 11:27:46 am »
Hello, I the DLL is 32 bits, you must compile a 32 bits exe.
Also be sure that de dll file is in the same folder where do you have de exe.

/BlueIcaro

Dzandaa

  • Hero Member
  • *****
  • Posts: 544
  • From C# to Lazarus
Re: Please help with the CH341DLL
« Reply #14 on: January 04, 2023, 12:08:14 pm »
Hello Sabouras,

Can you describe the board you try to communicate with?

Can you send then link to the product you use from China.

I used a lot of ESP8266, ESP32 and modules from China.

The CH341 is effectively and USB to Serial chip.

Do you communicate from your Windows 10 to an external board with USB?

I also see in you ZIP mention to PCA9685 which is a 16 Channels Servo Driver

Is this the board you try to communicate with?

More info would be useful.

B->
Regards,
Dzandaa

 

TinyPortal © 2005-2018