Parsing Serial Input String
maldridge
Posts: 37
I am retrofitting an old SSR board to use a Basic stamp for my Christmas lights display. Previously I had used a parallel port to drive the board directly, but this year I would like to run the display with my mac mini using fusion. I hit a roadblock when trying to parse the data that is incoming from the serial link.
The program I am using outputs one byte for the intensity of each channel. I need to read this byte and then assign it to the proper channel. I can use a serial header if necessary but I would prefer to avoid it. My link operates at 9600,8,n,1.
At this point, I don't think that the stamp can operate fast enough to do foward phase dimming, but correct me if I'm wrong there.
Below is my code:
The program I am using outputs one byte for the intensity of each channel. I need to read this byte and then assign it to the proper channel. I can use a serial header if necessary but I would prefer to avoid it. My link operates at 9600,8,n,1.
At this point, I don't think that the stamp can operate fast enough to do foward phase dimming, but correct me if I'm wrong there.
Below is my code:
' {$STAMP BS2} ' {$PBASIC 2.5} '--------------------------------------------constants------------------------------------------- baud CON 84 link CON 16 chan VAR Byte(8) chanid VAR Byte '--------------------------------------program initialization------------------------------------ DO FOR chanid = 1 TO 8 SERIN link, baud, [chan(chanid)] NEXT FOR chanid=1 TO 8 IF chan(chanid)>0 THEN HIGH chan(chanid) NEXT LOOP
Comments
Jeff T.
Unfortunately the program I am using cannot have the data requested. Instead it is sent at roughly 100ms intervals. I can easily add in the header and terminator to the transmitting plugin, but how should it be added to the pbasic code on the stamp?
The slash means wait for X\number of bytes. Check out the SERIN command in PBasic help for syntax.
I did some googling and then some reading with a fine toothed come of the syntax book and I think that the "/8" is used to capture exactly one channel's worth of data to the variable. Now I just need the program to hold at the start of the loop until the next header arrives. In the BS1, there was a wait command. Is there something similar to this for the BS2?
It's not real clear how the protocol works, but I think you get one byte value per channel (or Id) in a 8 byte stream? The index of the byte in memory is the ID or channel. If that's the case, the problem you'll face is syncing up the start of 8 bytes. If you can, send a start byte then you know the next 8 bytes will drop in the right slots.
To clear up any confusion, the data stream is as follows:
It should be noted that only the channel intensity is transmitted. It is a value between 0-255, but anything above 00 is being ignored and the relay is just set to 'on.'
If anyone out there knows C, there is a program which accomplishes a similar function here. It should be noted that there are no spare channels in my design and I do not use a sync/beat channel.
See SERIN in the help file and post your source code.
The specific help file entry I do not understand is the section 5 entry of the basic stamp command reference pertaining to SERIN. There are two lines at the top listing argument syntax. One specifies for the first generation stamp and the other specifies for the second generation stamp. What I fail to see is how the arguments from the first line are integrated into the second line.
As requested here is my source code:
The STAMP does not have buffers so you might have to go to 2400 baud.
Replace your loop(s) with commands like this.
I don't quite understand about the commands you referenced, but at 9600 buad, the stamp should still have almost a tenth of a second left over to act on the data received before needing to process more data.
http://www.parallax.com/dl/docs/prod/stamps/web-BSM-v2.2.pdf
Good luck
I thought that maybe the data wasn't arriving when I calculated, so then I tried it without the timout:
But still no luck. I have LEDs connected to P0-P2 for the first three channels, but try as I might, I can't get the program to work. I have also tried sending a string which should definitely work from hyper terminal (it to failed).