May 09, 2024, 12:17:25 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


The Commexample doesn't work

Started by Techno, April 26, 2009, 02:43:30 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Dear support,

I woiuld like the character "A" but my microcontroller doesn't receive the character.
Can someone post here an example for example commwrite???
I would liek prefer an gui example that used commread and commwrire?

This example doesn't work here



'Emergence BASIC example program for using COM ports
'this exmaple opens COM1,changes the BAUD rate and writes a string of data to it.
'compile as a CONSOLE target

SETID "OPENEXISTING",3
SETID "GENERICREAD",0x80000000
SETID "GENERICWRITE",0x40000000
SETID "NOPARITY",0
SETID "ODDPARITY",1
SETID "EVENPARITY",2
SETID "MARKPARITY",3
SETID "SPACEPARITY",4
SETID "ONESTOPBIT",0
SETID "TWOSTOPBITS",2

type DCB
    DEF DCBlength:int
    DEF BaudRate:int
DEF flags:int
    DEF wReserved:word
    DEF XonLim:word
    DEF XoffLim:word
    DEF ByteSize:char
    DEF Parity:char
    DEF StopBits:char
    DEF XonChar:char
    DEF XoffChar:char
    DEF ErrorChar:char
    DEF EofChar:char
    DEF EvtChar:char
    DEF wReserved1:word
ENDTYPE

DECLARE "Kernel32",CreateFileA(name:string,access:int,share:int,security:int,create:int,flags:int,handle:int),int
DECLARE "Kernel32",GetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",SetCommState(handle:int,lpdcb:DCB),int
DECLARE "Kernel32",CloseHandle(handle:int),int
DECLARE "Kernel32",WriteFile(handle:int,buffer:string,count:int,written:pointer,overlapped:int),int
DECLARE "Kernel32",ReadFile(handle:int,buffer:string,count:int,bytesread:pointer,overlapped:int),int

def dcb1:DCB
def hcom,success,written:int
def data1:string
dcb1.DCBlength = 26

hcom = CreateFileA("COM1",@GENERICREAD | @GENERICWRITE,0,0,@OPENEXISTING,0,0)
OPENCONSOLE
IF hcom <> -1
IF GetCommState(hcom,dcb1)
PRINT "Com port open, Current baud = ",dcb1.BaudRate
dcb1.BaudRate = 9600
dcb1.ByteSize = 8
dcb1.StopBits = @ONESTOPBIT
dcb1.Parity = @NOPARITY
IF SetCommState(hcom,dcb1) = 0
PRINT "Error setting com parameters"
ELSE
PRINT "Baud rate changed to ",dcb1.BaudRate
ENDIF
data1 = 0x41
IF WriteFile(hcom,data1,len(data1),written,0) <> 0
PRINT "Wrote ",written,"bytes to the port"
ENDIF
ENDIF
success = CloseHandle(hcom)
ELSE
PRINT "Could not open com port"
ENDIF

PRINT "Press any key to close"
DO
UNTIL INKEY$ <> ""
CLOSECONSOLE
END

REM the flags value in the DCB are individual bit switches defined as follows
REM LSB
    'fBinary:1           binary mode, no EOF check
    'fParity:1           enable parity checking
    'fOutxCtsFlow:1      CTS output flow control
    'fOutxDsrFlow:1      DSR output flow control
    'fDtrControl:2       DTR flow control type
    'fDsrSensitivity:1   DSR sensitivity
    'fTXContinueOnXoff:1 XOFF continues Tx
    'fOutX: 1            XON/XOFF out flow control
    'fInX: 1             XON/XOFF in flow control
    'fErrorChar: 1       enable error replacement
    'fNull: 1            enable null stripping
    'fRtsControl:2       RTS flow control
    'fAbortOnError:1     abort reads/writes on error
    'fDummy2:17          reserved
REM MSB



I waiting for an answer that I can test the communication with ebasic and the microcontroller
Kind regards

Egil

The variable data1 is defined as STRING.

Have you tried to change the line data1 = 0x41 to data1 = "A"
Support Amateur Radio  -  Have a ham  for dinner!

Techno

Quote from: Egil on April 26, 2009, 04:54:27 AM
The variable data1 is defined as STRING.

Have you tried to change the line data1 = 0x41 to data1 = "A"

Egil,

It works if I changed it to data1 = "A"
Can you give me an example in GUI application?

Egil

Well, since I up till now only have done console programming, I feel I am not the right person to help you with the GUI programming.
Have you tried to do it on your own?
Support Amateur Radio  -  Have a ham  for dinner!