week9

Serial 1

Serial communication

 

code 1

dim inputBuffer(1 To 13) As Byte '4-byte output buffer.
dim outputBuffer(1 To 10) As Byte '1-byte output buffer.
dim thisByte as byte
dim inByte as byte
dim gotaByte as boolean
dim potVar as integer

sub main ()
call delay(0.5) ' start program with a half-second delay
' define which pins COM3 will be:
call defineCom3(11,12,bx1000_1000)

' set aside memory for input and output:
call openQueue(inputBuffer, 13)
call openQueue(outputBuffer, 10)

' open COM3:
call openCom(3, 9600, inputBuffer, outputBuffer)

do

' read the switch, convert it to a readable ASCII value:

'thisByte = getPin(13) + 48

'thisByte = cByte(getADC(14)\4)
potVar = getADC(14)
debug.print "potVar = " ; cstr(potVar)


' send it out the serial port:
call putQueue(OutputBuffer, thisByte, 1)
loop

end sub

 

code 2

dim outputBuffer(1 To 45) as byte
dim inputBuffer(1 To 13) as byte
dim potVar as integer


Sub main()
do
call delay(0.5) ' start program with a half-second delay

dim myString as String

call openQueue(outputBuffer, 45)
call openQueue(inputBuffer, 13)
call defineCom3(11,12,bx1000_1000)
call openCom(3, 9600, inputBuffer, outputBuffer)
potVar = getADC(13)
debug.print "potVar = " ; cstr(potVar)

myString = "hello world!" & "the switch state: " & chr(cstr(potVar)\4)
call putQueueStr(outputBuffer, myString)

' Append carriage return and line feed
call putQueueStr(outputBuffer, chr(13) & chr(10))
loop

End Sub

We divided cstr(potVar) by 4 : "chr(cstr(potVar)\4)", because we got potentiometer value from 0-1023, so if we want to use ASCII code which has data ranged 0-255, we need to adjust our Pot values to those ASCII values. 'chr(13) & chr(10)' means "push 'enter' key--chr(13)" and "make 'New Line' --chr(10)" as following ASCII code.

code 3

dim outputBuffer(1 To 45) as byte
dim inputBuffer(1 To 13) as byte
dim potVar as integer


Sub main()
do
call delay(0.5) ' start program with a half-second delay

dim myString As String

call openQueue(outputBuffer, 45)
call openQueue(inputBuffer, 13)
call defineCom3(11,12,bx1000_1000)
call openCom(3, 9600, inputBuffer, outputBuffer)
potVar = getADC(13)
debug.print "potVar = " ; cstr(potVar)
if (potVar>0) and (potVar<300) then
myString="low"
end if

if (potVar>300) and (potVar<500) then
myString="middle"
end if

if (potVar>500) then
myString="high"
end if

'myString = "hello world!" & "the switch state: " & chr(cstr(potVar)\4)
call putQueueStr(outputBuffer, myString)

' Append carriage return and line feed
call putQueueStr(outputBuffer, chr(13) & chr(10))
loop

End Sub

 

 

 

 

 

Journal Index