#COM = 3 ;Serial port to use #TXstop = 99;end of transmission for PC/BS communication ;---------------------------[ Initial setup ]-------------------------------------------------------- OpenConsole() ; Open a window to display the program status EnableGraphicalConsole(1) ; Open graphics mode for the ConsoleLocate command below ClearConsole() ConsoleTitle ("M.Hampshire - COM" + Str(#COM) + " test program") ; Set the window title PrintN("Attempting to open COM" + Str(#com)) i=1 While OpenSerialPort(3, "COM" + Str(#COM), 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)=0 ConsoleLocate(0,1) PrintN ("COM"+Str(#COM)+" Not available. Retry #" + Str(i)) Delay(2500) i=i+1 Wend PrintN ("COM"+Str(#COM)+" acquired") PrintN("") ;---------------------------[ Main program loop ]---------------------------------------------------- MAIN: ;---------------------------[ Input servo position ]------------------------------------------------- If OpenConsole() Print("Servo position? ") SVA = Val(Input()) EndIf ;---------------------------[ Send token indicating servo position to follow ]----------------------- While WriteSerialPortString(#COM, "SVA") = 0 ; Indicate servo position A ConsoleLocate(0,4) PrintN("Servo token send error") Wend PrintN("Sent SVA token") i=SVA Gosub writenum2com ; Send numerical data Delay(1000) ;---------------------------[ Wait for serial data ]------------------------------------------------- checkconfirmation: While AvailableSerialPortInput(#COM)=0 ; If no Data, then check again ConsoleLocate(0, 13) PrintN("Waiting for any data and confirmation") Wend PrintN("") PrintN("Data available") PrintN("") Gosub readcom If text$="SVA OK" PrintN("Confirmation receieved") Else PrintN("Failed confirmation!!!!") EndIf Delay(2000) ClearConsole() Goto MAIN ;---------------------------[ Exit To system ]------------------------------------------------------- StopDrawing() CloseConsole() End ;===========================[ Subroutines ]========================================================== ;---------------------------------------------------------------------------------------------------- ;---------------------------[ Break number into digits and send over COM ]--------------------------- ;---------------------------[ inputs number in "i" and sends i's digits @ 9600baud ]----------------- ;---------------------------------------------------------------------------------------------------- writenum2com: PrintN("Intending to send data: " + Str(i)) For x=4 To 0 Step -1 pos=Int(i/Pow(10,x)) Print(Str(pos) + " ") Gosub senddig i=i-(pos*Pow(10,x)) Delay(10) ; Pause because the LCD update is so slow Next x pos=#TXstop ; Send non-numeric byte to indicate last digit Print("Stop byte: ") senddig: dig=pos+48 If WriteSerialPortData(#COM, @dig, 1) = 0 PrintN("Digit #1 send error") EndIf PrintN("Sent:" + Str(dig-48)) Return ;---------------------------------------------------------------------------------------------------- ;---------------------------[ Read SIX bytes from the COM port ]------------------------------------- ;---------------------------------------------------------------------------------------------------- readcom: Print("Searching for stop byte: ") checkagain: ReadSerialPortData(#COM, @buff, 1) Print(Chr(buff)) If buff <> (#TXstop+48) And AvailableSerialPortInput(#COM)>0 ; Read from the serial port Until reach the stop char (clear buffer) Goto checkagain EndIf PrintN("") PrintN("Stop byte found") PrintN("") text$="" While AvailableSerialPortInput(#COM)>0 ; Pull all data off the serial port ReadSerialPortData(#COM, @buff, 1) ; One byte at a time text$=text$+Chr(buff) ; and save it in text$ Wend Return ; IDE Options = PureBasic 4.51 (Windows - x86) ; CursorPosition = 66 ; FirstLine = 40 ; EnableXP ; Executable = ..\Users\Matt.IZUMI\Desktop\Serial test with BS2.exe ; DisableDebugger