Liberty BASIC Community Forum
« kernell #32 problem »

Welcome Guest. Please Login or Register.
Dec 12th, 2017, 08:06am


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


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


member is offline

Avatar




PM


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

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

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5585
xx Re: kernell #32 problem
« Reply #1 on: Mar 21st, 2017, 03:44am »

I will look at your code when I get a little time. Can you try this, it runs for me but I can't see whether it is setting the high and low break that you need.


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
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

'now close the port because we need to open
'with Liberty BASIC
calldll #kernel32, "CloseHandle", _
hFileHandle as ulong, _
result as long

'hFileHandle now contains the #handle, that identifies the port
'we will use this handle within our Liberty code

'Now open the com port in Liberty
open lpFileName$;":10400,n,8,1,ds0,cs0,rs" for random as #com

'ports take time to establish, give it two seconds before using it
timer 2000, [setuptime]
wait

[setuptime]
timer 0

print "setting break"
CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETBREAK as long,_
result as long

'delay 25ms
CallDLL #kernel32, "Sleep",25 As ULong,ret As Void


print "clearing break"
CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _CLRDTR as long,_
result as long

'delay 25ms
CallDLL #kernel32, "Sleep",25 As ULong,ret As Void

'now start normal communication either reading the input buffer
'or sending commands to #com

Print "Closing com port"
close #com
end

 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5585
xx Re: kernell #32 problem
« Reply #2 on: Mar 21st, 2017, 03:57am »

OK, my understanding is that SetCommState is all about establishing or changing the port parameters. So stop bits baud rate and handshaking lines. But it does not set or reset the Tx line which is what your car ecu is monitoring.

For that you need EscapeComFunction and SetBreak/ClrBreak. I am opening the port at 10400 baud and holding the line to 0 or 1 for 25ms then allowing normal transmission at 10400 baud.


User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5585
xx Re: kernell #32 problem
« Reply #3 on: Mar 21st, 2017, 04:12am »

I am not sure where this code is taking you but you were missing a return value in the API call and you had closed the port ahead of trying to talk to it.


Code:
'open com port
lpFileName$ = "Com3"
dwCreationDistribution = _OPEN_EXISTING
hTemplateFile = _NULL
dwFlagsAndAttributes=_FILE_FLAG_OVERLAPPED

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


'get ready to find its parameters
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)

'get dcb
CALLDLL #kernel32, "GetCommState",hFileHandle as ulong,dcbSerialParams as struct,status as short


'set the values for Baud rate, Byte size, Number of start/Stop bits to something else.

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

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

wait

 


Also Windows constants need preceded with an _ in Liberty so _NOPARITY _ONESTOPBIT or just send the decimal value of the constant 0,0 in this case
« Last Edit: Mar 21st, 2017, 06:18am by Rod » User IP Logged

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