Purebasic Serial Port Example

Purebasic Serial Port Example Average ratng: 4,4/5 7260 votes

Good evening. I'm running a BS2px with a Parallax USB-Serial converter on my Serial port-delete Win7 machine. The converter has worked great up to this point.

Purebasic serial port example

I can program through, debug screen through and do all functions necessary with the BS. When I do an ID in the BS editor I get: COM3 Basic Stamp 2px24, Version v1.0, Loopback NO, Echo YES Now my question, that I've been unable to find an answer for: I'm writing code for RX/TX communications between the BS2 and PC.

Purebasic Serial Port Example

The current project is simple. PC: Sends 'SVA' a token to indicate servo A, followed by a 16bit number e.g. Decimal 01000 BS2: Receives the token and 16bit number (decimal 01000) and sends a confirmation 'SVA OK' PC: Pulls the following data off the serial port: 'SVA01000^SVA OK' The '^' is a non-numeric stop character to let the BS know the number has finished sending.

SERIN 16,16780,WAIT('SVA'),DEC servo1 Am I seeing the Echo YES function in this and that's why all transmitted data is being read back into my PC? Is there any way to turn Echo OFF?? Is determined by how the connector is wired? Does the Stamp still program after turning Echo OFF? I've been all over the manuals, the converter settings, Google and the forums. I'm not seeing the answer.

Perhaps I'm asking the wrong question. Thank you once again. Hi, which program are you using on the PC side for your TX/RX comms. Not only are the characters ehoed back but they will remain in the serial port buffer until purposefully removed or the port is closed. Knowing how many characters are transmitted tells you how many are echoed back and how many to remove.

It is also worth mentioning that the characters echoed back take time to arrive at the PC which is dependent on baud rate and packet size, it might take 1 or 2 milliseconds for each byte to arrive back at the PC at a baud of 9600. This may mean pausing the PC program briefly to ensure the transmission lines are clear and ensure the echo has completely arrived at the buffer for deletion. My PC program is currently written in Pure Basic to work out my ideas. It will be written in C as soon as my issues are all worked out. At the moment, I'm learning.

One thing I don't understand is, I used Tera Term (open source terminal program) to verify the BS program and the communications. Tera Term does not show the echoed characters. But my PC/Basic program does. 'Not only are the characters ehoed back but they will remain in the serial port buffer until purposefully removed or the port is closed' Removed?? As in read off the port/buffer by my PC program right? I don't know of any other purges, unless that is my inexperience showing. I'll see about filtering the echoed characters tonight.

That is unless I can come up with another alternative, like finding a problem. It sounds like the echo is there to stay though unless I use P0-15 for communications. Hi Spiral72, while your on basic check this link As with anything there are drawbacks but it has a lot going for it, for a GUI it works really well I have used VB with the Bs2px and the serial exchange is pretty fast. The applications themselves are by no means slow and unless you are into something extremely complex or graphic intensive its not going to appear much slower than a C app. These same routines are also suited for the Propeller with the smallest of modification.

You could be up and running with VB Express in a very short period of time. Although the syntax is different C# mainly uses the same methods and objects as VB so that transition would not be too difficult. Clearing the serial port buffer is as you say a case of reading the characters, VB has additional commands that also 'discard' the buffer.

Thank you all. I send a token, then a 16-bit servo position from the PC. Then I have to send a non-numeric byte to indicate 'No more digits will follow'. I already said that I know. So what I did was on the PC, send the token, position and stop byte.

Purebasic serial port example

Then I READ from the serial port until the stop byte is found. Effectively clearing the port except for the BS's confirmation message.

And it works. Now the PC accepts a typed servo position, sends it to the BS, the BS moves the servo in position, then waits for the next position. Man that was painful, but I learned a lot.

Now to hot glue a PING on the servo and add some more code: I have never used DEBUGIN or DEBUG before. It looks like I need to look it up. Don't tell me I did this the hard way!

SerialPortError SerialPortError Syntax Result = SerialPortError(#SerialPort) Description Returns the error on the serial port when, or failed. Parameters #SerialPort The serial port to use. Return value Can be a combination of the following values: #PBSerialPortRxOver: An input buffer overflow has occurred. There is either no room in the input buffer or a character was received after the end-of-file (EOF) #PBSerialPortOverRun: A character-buffer overrun has occurred. The next character is lost. #PBSerialPortRxParity: The hardware detected a parity error. #PBSerialPortFrame: The hardware detected a framing error.

#PBSerialPortBreak: The hardware detected a break condition. #PBSerialPortTxFull: The application tried to transmit a character but the output buffer was full. #PBSerialPortIOE: An I/O error occurred during communications with the device. #PBSerialPortWaitingCTS: Specifies whether transmission is waiting for the CTS (clear-to-send) signal to be sent. #PBSerialPortWaitingDSR: Specifies whether transmission is waiting for the DSR (data-set-ready) signal to be sent. #PBSerialPortWaitingRLSD: Specifies whether transmission is waiting for the RLSD (receive-line-signal-detect) signal to be sent.

#PBSerialPortXoffReceived: Specifies whether transmission is waiting because the XOFF character was received. #PBSerialPortXoffSent: Specifies whether transmission is waiting because the XOFF character was transmitted. Transmission halts when the XOFF character is transmitted to a system that takes the next character as XON, regardless of the actual character. #PBSerialPortEOFSent: Specifies whether the end-of-file (EOF) character has been received.

See Also, Supported OS All.