Anyone figure out serial port communication yet?
I can't figure out the best way to go about it. :(
I can't figure out the best way to go about it. :(

Module pub.zcomm ModuleInfo "Version: 1.00" ModuleInfo "Author: Matt Griffith (Zenith)" ModuleInfo "License: Public Domain" ModuleInfo "Modserver: BRL" Import brl.standardIO Import brl.system Rem bbdoc: Zenith's Serial(And possibly? Parallel) Port Library. :) electrodev{NOSPAM}gmail.com I give out this code as public domain, FREE AS HELL. Give me credits if you want... ;) NOTE: THIS HAS ONLY BEEN TESTED UNDER WINDOWS XP WITH LOOPBACK HARDWARE WHERE RX IS BUFFERED INTO TX. EndRem Type Tcomm Field timeout,stream,id$ ' Open up a stream using serial communication ' Or possibily parallel if you type in "LPT" instead of COM as type? ' timeout is in milliseconds.. ' port refers to which ports, IE: COM1 COM2, LPT1.. and so forth Method Open() Self.stream = OpenStream(Self.id+":") End Method ' Clean up the stream, each time you recieve data it is added to the ' stream, this can slowly grow into a huge size, however with this ' function you can reset the stream and reconnect right away, cleaning ' away all the old data. (Just make sure you have read it all before then) Method Clean() CloseStream(Self.stream) Self.stream = OpenStream(Self.id+":") End Method ' Send data to the port, using a simple string - no delim characters or anything ' just send a string and it will send it until it reaches the end. Method Send(send$) Local i For i=0 To Len(send)-1 WriteByte(Self.stream,send[i]) Next End Method ' Wait for data to be recieved on the stream. ' It will timeout if it doesn't get data until then. Method Wait() Local time = MilliSecs() While StreamPos(Self.stream)=0 If MilliSecs()-time>Self.timeout ' we'll give it a 1 second time out Return 0 EndIf Wend Return StreamSize(Self.stream) End Method ' Once you have checked if data is ready to be read with CommWait() ' You can then read it off, this will output a string. ' A string is probally a very bad way of getting data back and forth ' But I figured it would just be temporary. ' I'm not even sure if it will send bytes that are 0-32?! :( Method Recv$() Local data$,i SeekStream(Self.stream,0) For i=0 To StreamSize(Self.stream)-1 data = data+Chr(ReadByte(Self.stream)) Next Return data End Method End Type
Rem ****EXAMPLE PROGRAM**** Zenith's Serial(And possibly? Parallel) Port Library. :) electrodev{NOSPAM}gmail.com I give out this code as public domain, FREE AS HELL. Give me credits if you want... ;) NOTE: THIS HAS ONLY BEEN TESTED UNDER WINDOWS XP WITH LOOPBACK HARDWARE WHERE RX IS BUFFERED INTO TX. EndRem Framework brl.standardio Import pub.zcomm Strict Local c:Tcomm = New Tcomm c.id = "COM1" c.timeout = 1000 c.Open() If c.stream Print "STREAM CONNECTED on "+c.id Else Print "STREAM FAILED TO CONNECT on "+c.id Input() End EndIf Repeat Local recv$,send$ = Input("PC> "),bytes Print "@Sending "+Len(send)+" byte(s)" ' Send the data c.Send(send) ' Wait for data to be recieved bytes = c.Wait() Print "@Recieving "+bytes+" byte(s)" If bytes ' Ok, we have to data to be read in, now recieve it. recv$ = c.Recv() Print c.id+"> '" +recv+"'" c.Clean() Else Print c.id+" has timed out." EndIf FlushMem Forever Print "CLOSED STREAM.." Input()