Liberty BASIC Community Forum
« kernell #32 problem »

Welcome Guest. Please Login or Register.
Oct 23rd, 2017, 02:49am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: kernell #32 problem  (Read 261 times)
monini64
New Member
Image


member is offline

Avatar




PM


Posts: 8
xx kernell #32 problem
« Thread started on: Mar 21st, 2017, 02:03am »

i have this code, but when i try use SetCommState in last line liberty basic stop work
Code:
 
'Open the port briefly using an API call to determine the handle given by Windows
'We will use this handle later in Liberty, its the only way to get it.
'substitute your own port number
lpFileName$ = "Com3"
dwCreationDistribution = _OPEN_EXISTING
hTemplateFile = _NULL
dwFlagsAndAttributes=_FILE_FLAG_OVERLAPPED
'in c
'HANDLE WINAPI CreateFile(
'  _In_     LPCTSTR               lpFileName,
'  _In_     DWORD                 dwDesiredAccess,
'  _In_     DWORD                 dwShareMode,
'  _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
'  _In_     DWORD                 dwCreationDisposition,
'  _In_     DWORD                 dwFlagsAndAttributes,
'  _In_opt_ HANDLE                hTemplateFile
');

calldll #kernel32, "CreateFileA", _
lpFileName$ as ptr, _
dwDesiredAccess as ulong, _
dwShareMode as ulong, _
lpSecurityAttributes as ulong, _
dwCreationDistribution as ulong, _
dwFlagsAndAttributes as ulong, _
hTemplateFile as ulong, _
hFileHandle as ulong

'in c
'BOOL WINAPI CloseHandle(
'  _In_ HANDLE hObject
');

calldll #kernel32, "CloseHandle", _
hFileHandle as ulong, _
result as long

'hFileHandle now contains
'the #handle, a number, that identifies the port
'*********************************************************************************************************
'BOOL WINAPI GetCommState(
'  _In_    HANDLE hFile,
'  _Inout_ LPDCB  lpDCB
');

'BOOL WINAPI SetCommState(
'  _In_ HANDLE hFile,
' _In_ LPDCB  lpDCB
');

'If the function succeeds, the return value is nonzero.
'If the function fails, the return value is zero. To get extended error information, call GetLastError.
'*********************************************************************************************************


'typedef struct _dcbSerialParams {
'  DWORD DCBlength;
'  DWORD BaudRate;
'  DWORD fBitFields
'************************************  fBitFields bits configuration *************************************
' DWORD fBinary  :1;
' DWORD fParity  :1;
' DWORD fOutxCtsFlow  :1;
' DWORD fOutxDsrFlow  :1;
' DWORD fDtrControl  :2;
' DWORD fDsrSensitivity  :1;
' DWORD fTXContinueOnXoff  :1;
' DWORD fOutX  :1;
' DWORD fInX  :1;
' DWORD fErrorChar  :1;
' DWORD fNull  :1;
' DWORD fRtsControl  :2;
' DWORD fAbortOnError  :1;
' DWORD fDummy2  :17;
'**************************************  fBitFields have 32 bites      **************************************
'  WORD  wReserved;
'  WORD  XonLim;
'  WORD  XoffLim;
'  BYTE  ByteSize;
'  BYTE  Parity;
'  BYTE  StopBits;
'  char  XonChar;
'  char  XoffChar;
'  char  ErrorChar;
'  char  EofChar;
'  char  EvtChar;
'  WORD  wReserved1;
'} DCB, *LPDCB;
'**************************************  dcbSerialParams have 28 bytes  **************************************

struct dcbSerialParams, _
  DCBlength As uLong, _
  BaudRate As uLong, _
  fBitFields As uLong, _
  wReserved As short, _
  XonLim As short, _
  XoffLim As short, _
  ByteSize As char[1], _
  Parity As char[1], _
  StopBits As char[1], _
  XonChar As char[1], _
  XoffChar As char[1], _
  ErrorChar As char[1], _
  EofChar As char[1], _
  EvtChar As char[1], _
  wReserved1 As short

dcbSerialParams.DCBlength=len(dcbSerialParams.struct)

'Now open the com port in Liberty and use the hFileHandle value in API calls
CALLDLL #kernel32, "GetCommState",hFileHandle as ulong,dcbSerialParams as struct,status as short
'and set the values for Baud rate, Byte size, Number of start/Stop bits etc.

dcbSerialParams.BaudRate.struct = 600'  // Setting BaudRate = 9600
dcbSerialParams.ByteSize.struct = 8'         // Setting ByteSize = 8
dcbSerialParams.StopBits.struct = ONESTOPBIT'// Setting StopBits = 1
dcbSerialParams.Parity.struct   = NOPARITY'  // Setting Parity = None

CALLDLL #kernel32, "SetCommState",hFileHandle as ulong,dcbSerialParams as struct


 
User IP Logged

Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1115
xx Re: kernell #32 problem
« Reply #1 on: Apr 6th, 2017, 9:26pm »

Hello,
I apologize that it has taken so long for a reply to your question.

It appears that you have just left out the return to the SetCommState API call.

Here is a link to the MSDN page for the function; on it you can see that the return value is stated as BOOL; in LB you can use a Long value for this and it works perfectly fine.

Here is what your SetCommState function call should look like (You can change the return variables name to anything you want):

Code:
CallDLL #kernel32, "SetCommState", hFileHandle      As ulong, _
                                   dcbSerialParams  As struct, _
                                   SetCommState     As long 


Note that I did not check that the structure you are sending is correct.

I hope this works for you; let us know if you have any further questions.


{:0)

Brandon
User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls