PDA

View Full Version : Bi-Direction serial communication PC > PIC *16F84A via RWS-434 TWS-434



RYTECH
- 20th December 2005, 06:39
I've been trying to obtain rf wireless communication to my pic projects for awhile now... recently I managed to get my pc's serial port to transmit out a TWS transmitter module to the RWS reciever at N2400 baud (yay) i can "trigger" my reciever address "if" statement and get to the underlying functions (i'm hoping these parts of the program can talk to the other pic's and update variables on those which in turn provide the desired functionality. This is a rc robot/car

the recadd byte is next byte out after the "254" qualifier

I then have a variable "bitvalue" ' basically the 0-255 I would like to send to the different PIC's which control various features (motor direction, HPWM motor speed control, various lights, possibly a rototating "platter" for my camera to sit on)

but enough about the PC > PIC transmission... my problem is the opposite, the PIC > PC transmission and recipt.

The problem is my vb program gets garbled data

it looks like this: ûo—ÿû[¸
qH


maybe somebody could post a schematic for connecting both a RWS /TWS to both pic and pc so i know i have my connections right. (especially resistor values)

Thanks

RYTECH
- 20th December 2005, 19:45
I thought i'd update my post with more information as i am still unable to resolve my receiver difficulties and really require some intervention.

My PC side is like this for hardware:

PC SERIAL PORT TX > PIN2 TWS TRANSMITTER MODULE (works) Sends output of serial port from Visual Basic program using Com 1 2400,n,8,1
PC SERIAL PORT GND > GND

PC SERIAL PORT RX < RWS-434 Data out (have tried both pin 2 "Digital" output, tried pin 3 "Linear" output with 47k resistor to ground, "Linear" is what is used successfully on the remote pic side.) doesnt work or strange charecters received.

The Pic side looks like this:

pin 1 (PortA.2 "serpin" in my pic's software program) to RWS-434 modules "Linear" data output with 47k resistor to ground. (Works) Pic receives data qualifier and procceds through program as expected. The pic side RWS-434 has pin 1 > GND, pin 2 > SOMETIMES TO SERIAL PORT RX, pin 3 > SOMETIMES TO SERIAL PORT RX , pin 4 > VCC +5 (4.9), pin 5 > VCC +5 , pin 6 > GND , pin 7 > GND , pin 8 > ANT.

pin 2 (PortA.3 "serpout" in my pic's software program) to TWS-434A "data in" pin 2.

The 4 antennas (I'd eventually like to use just the 1 on each side) have been swapped in one at a time to the working PC > PIC TWS > RWS part of the circuit so they can likely be ruled out as the problem.

The project is currently breadboarded (3 breadboards actually) with the PC side device (meaning the TWS+RWS+7805C) on a smaller breadboard.

The pic side layout currently is having the Pic and teh tws on a small breadboard with the working RWS on its own breadboard. ( I scrapped a couple of working TX > RX TWS / RWS w/ holtek encoders that i had going to start development immediately of the serial RF modem type communication)

The PIC program is as follows: (sorry for the mamoth post but i thought it be good to post the complete picture)

More of my questions and comments after the code:

(my post is too long i'll re-post my code)


As you can see i'm hoping to eventually Parse the returned data.

But first to get the data over RF from the pic side.

Any ideas on what is my problem... maybe we could move this post to a more appropriate section... i didnt realize i was in the PIC wishlist (oops, sorry)

RYTECH
- 20th December 2005, 19:46
'************************************************* ***************
'* Name : SERIAL RF *
'* Author : Ryan Barrett *
'* Notice : Copyright (c) 2005 Rytech Computing *
'* : All Rights Reserved *
'* Date : NOV 2005 *
'* Version : 1.0 *
'* Notes : *
'* : PC to PIC and vice-versa! *
'************************************************* ***************

INCLUDE "bs2defs.bas"
RECADD VAR b3 '8 bit latch number
BITVALUE VAR byte '8 bit latch value
MCONTROL1 var b5 'MOTORCONTROL BYTE - FORWARD BACK LEFT RIGHT
MSPEED1 var b6 'MOTORSPEED BYTE FOR HPWM
LIGHTS1 var b7 'BYTE CONTROLLING LED LIGHTS
LVAL4 var b8
'CVAL1 var b9
'CVAL2 var b10
'CVAL3 VAR b11
'CVAL4 VAR b12


'i var byte
serpin VAR porta.2 'serial input pin
serpout var porta.3 'serial output pin
INITIAL:
trisa = %00000100
'trisa = %00010000
trisb = %00000000
MCONTROL1 = 0
MSPEED1 = 0
LIGHTS1 = 0
LVAL4 = 0
PORTB = 0
'flash leD
HIGH portA.1
PAUSE 700
LOW portA.1
PAUSE 800
'flash leD
HIGH portA.1
PAUSE 1000
LOW portA.1
PAUSE 800
'flash leD
HIGH portA.1
PAUSE 1000
LOW portA.1
PAUSE 800
HIGH portA.1
PAUSE 1000
LOW portA.1
GoTO loop
loop:
BITVALUE = 0

SERIN serpin,N2400,[254],RECADD,BITVALUE' serial data in on portA.1
serout serpout,N2400,[220,#MCONTROL1]
IF RECADD = 1 THEN outr1 ' if request is for RECADD#1 then goto RECADD#1 routine
IF RECADD = 2 THEN outr2 ' if request is for RECADD#2 then goto RECADD#2 routine
IF RECADD = 3 THEN outr3 ' if request is for RECADD#3 then goto RECADD#3 routine
IF RECADD = 4 THEN outr4 ' if request is for RECADD#4 then goto RECADD#4 routine
IF RECADD = 5 THEN outr5 ' if request is for RECADD#5 then goto RECADD#5 routine
IF RECADD = 6 THEN outr6 ' if request is for RECADD#6 then goto RECADD#6 routine
GOTO loop

outr1:
HIGH portA.1
Pause 1000
MCONTROL1 = BITVALUE
'INSERT OUT SERIAL TO OTHER PIC CONTROLLING MOTOR DIRECTIONS
LOW portA.1: GOTO loop
outr2:
HIGH portA.1
Pause 1000
MSPEED1 = BITVALUE
'INSERT OUT SERIAL TO OTHER PIC CONTROLLING MOTOR HPWM
LOW portA.1: GOTO loop
outr3:
HIGH portA.1
Pause 1000
LIGHTS1 = BITVALUE
'INSERT OUT SERIAL TO LED PIC CONTROLLING LEDS
LOW portA.1: GOTO loop
outr4:
HIGH portA.1
Pause 1000
LVAL4 = BITVALUE
LOW portA.1: GOTO loop
GOTO loop
outr5:
HIGH portA.1
PAUSE 1000
LOW portA.1
Pause 1000
HIGH portA.1
Pause 1000
LOW portA.1
Pause 1000
HIGH portA.1
Pause 1000
LOW portA.1
Pause 1000
HIGH portA.1
Pause 1000
LOW portA.1
Pause 1000
HIGH portA.1
Pause 1000
LOW portA.1
Pause 1000
HIGH portA.1
Pause 1000
LOW portA.1: GOTO loop
outr6:
HIGH portA.1
Pause 700
LOW portA.1
Pause 700
HIGH portA.1
Pause 700
LOW portA.1
Pause 700
HIGH portA.1
Pause 700
LOW portA.1
Pause 700
HIGH portA.1
Pause 700
LOW portA.1
Pause 700
HIGH portA.1
Pause 700
LOW portA.1
Pause 700
HIGH portA.1
Pause 700
LOW portA.1
Pause 700
HIGH portA.1
Pause 700
LOW portA.1: GOTO loop
GOTO loop






The Visual Basic Program is very immature and looks something like this:


'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''
' PC > PIC SERIAL RF
' Ryan Barrett
' Rytech Computing
' DEC 2005
'
'
'
'
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public bit1value As Integer
Public bit2value As Integer
Public bit3value As Integer
Public bit4value As Integer
Public allbitout As Integer
Public allbitout8 As Integer
Public recadd As Integer
Dim oldlogtext As String
Dim serinput As String

Private Sub AddressList_Click()
recadd = AddressList.ItemData(AddressList.Text)
'MsgBox (recadd)
'Call TransmitIt
End Sub

Private Function TransmitIt2()
'For I = 1 To 3 ' Loop 4 times
MSComm1.Output = Chr$(254) & Chr$(recadd) & Chr$(allbitout8)
'Pause 235 ' Pause 250mS
oldlogtext = SERIALINLOG.Text
serinput = MSComm1.Input
SERIALINLOG.Text = "Incoming Data: " + serinput + vbCrLf + oldlogtext
'PARSESTRINGNOW
'Next I
End Function
Private Function PARSESTRINGNOW()
Dim Atext As String
Dim Btext As String
Dim Ctext As String
Dim Dtext As String
Dim Etext As String
Dim Ftext As String
Dim Gtext As String
Dim Htext As String
Dim Itext As String
Dim Jtext As String
Dim count As Long
If serinput = vbNullString Then
Else
count = Len(serinput)
Atext = Mid$(serinput, InStr(serinput, "A") + 1, InStr(serinput, "B") - InStr(serinput, "A") - 1)
Btext = Mid$(serinput, InStr(serinput, "B") + 1, InStr(serinput, "C") - InStr(serinput, "B") - 1)
Ctext = Mid$(serinput, InStr(serinput, "C") + 1, InStr(serinput, "D") - InStr(serinput, "C") - 1)
Dtext = Mid$(serinput, InStr(serinput, "D") + 1, InStr(serinput, "E") - InStr(serinput, "D") - 1)
Etext = Mid$(serinput, InStr(serinput, "E") + 1, InStr(serinput, "F") - InStr(serinput, "E") - 1)
Ftext = Mid$(serinput, InStr(serinput, "F") + 1, count - (InStr(serinput, "F") - 1))
'MsgBox (Atext + " " + Btext + " " + Ctext + " " + Dtext + " " + Etext + " " + Ftext)
oldlogtext = SERIALINLOG.Text
SERIALINLOG.Text = "parsed data string: " + Atext + " " + Btext + " " + Ctext + " " + Dtext + " " + Etext + " " + Ftext + vbCrLf + oldlogtext
End If
End Function
Public Sub bit1_Click()
Call AddBits(1)
End Sub
Private Sub bit2_Click()
Call AddBits(2)
End Sub
Private Sub bit3_Click()
Call AddBits(3)
End Sub
Private Sub bit4_Click()
Call AddBits(4)
End Sub


Private Sub BYTEOUT1_Click()
allbitout8 = 129
TransmitIt2
End Sub

Private Sub BYTEOUT2_Click()
allbitout8 = 3
TransmitIt2
End Sub
Private Sub BYTEOUT3_Click()
allbitout8 = 7
TransmitIt2
End Sub
Private Sub BYTEOUT4_Click()
allbitout8 = 15
TransmitIt2
End Sub
Private Sub BYTEOUT5_Click()
allbitout8 = 11
TransmitIt2
End Sub

Private Sub Command1_Click()
allbitout8 = 255
TransmitIt2
End Sub


Private Sub Command2_Click()
MSComm1.Output = Chr$(254) & Chr$(1)
Pause 235 ' Pause 250mS

End Sub

Private Sub Command3_Click()
MSComm1.Output = Chr$(254) & Chr$(2)
Pause 235 ' Pause 250mS

End Sub

Private Sub Command4_Click()
Pause 300
MSComm1.Output = Chr$(254) & Chr$(1) & Chr$(0)
SERIALINLOG.Text = MSComm1.Input
Pause 250
SERIALINLOG.Text = MSComm1.Input
Pause 250
SERIALINLOG.Text = MSComm1.Input

End Sub

Private Sub Form_Initialize()
bit1value = 0
bit2value = 0
bit3value = 0
bit4value = 0
allbitout = 0
recadd = 0
allbitout8 = 0
End Sub
Private Function AddBits(whichbit As Integer)
If whichbit = 1 Then
If bit1.Value = 0 Then
bit1value = 0
Else
bit1value = 1
End If
'MsgBox ("bit1")
End If
If whichbit = 2 Then
If bit2.Value = 0 Then
bit2value = 0
Else
bit2value = 2
End If
'MsgBox ("bit2")
End If
If whichbit = 3 Then
If bit3.Value = 0 Then
bit3value = 0
Else
bit3value = 4
End If
'MsgBox ("bit3")
End If
If whichbit = 4 Then
If bit4.Value = 0 Then
bit4value = 0
Else
bit4value = 8
End If
'MsgBox ("bit4")
End If

allbitout = bit1value + bit2value + bit3value + bit4value
'MsgBox (allbitout)
'fourbitbyte.Text = allbitout
Call TransmitIt
End Function
Private Sub Form_Load()
MSComm1.Settings = "2400,N,8,1"
MSComm1.CommPort = 1
MSComm1.PortOpen = True
End Sub
Public Sub Pause(milli As Long)
Sleep (milli)
End Sub

Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End
End Sub

RYTECH
- 20th December 2005, 21:21
added this to the vb program to poll the serial port input gave it a 235ms interval

Private Sub Timer1_Timer()
oldlogtext = SERIALINLOG.Text

serinput = MSComm1.Input
SERIALINLOG.Text = "Incoming Data: " + serinput + vbCrLf + oldlogtext

End Sub

RYTECH
- 20th December 2005, 21:23
Hooked up my Pic's recieve directly to the RX to confirm that the 2 programs resulted in seeing the data in the log window.. works resonably we'll when directly connected..

still not at all through the PIC>PC TWS>RWS. the best i get is when i use the digital output the data is all strange charecters. this happens regardless of weither the pic side is on or off.

Dave
- 21st December 2005, 12:05
RYTECH, Have you looked at the output from the receiver with no RF being generated? You probably have all kinds of switching going on at the output of the receiver as these types of receivers use a data slicer for comparing the signal strength of the incomming signal. These types of receivers need to have a Sync or Wakeup data stream sent before the actual data to condition the data slicer integrator. During times of no RF reception the integrator is allowed to discharge and is made more sensitive to background noise. If the PC side of the system is allowed to receive this output signal then it will interpret it as random data by the uart. That is why you need to send a stream of 1's and 0's to condition the receiver and then send a preamble pattern then look for the fixed pattern before accepting the received data from the RS232 port. I hope this all makes sense......

Dave Purola,
N8NTA

skimask
- 23rd December 2005, 03:46
Do a Google search on

skimask
- 23rd December 2005, 03:53
Do a Google search on Manchester encoding before you go any farther.

I'm using the same modules you are using and they work great for me, both TX'ing and RX'ing at 9600 baud over 200-300 feet at 5v on the TX and RX using roughly 11 inch wire antennas.

Send about 5ms worth of 1's and 0's before you do anything with the transmitted data...a $55AA will do to sync up the data slicer on the RX, more if you can spare the time. Then immediately after that send your data.
This should be manchester encoded, which also happens to double your data payload...2400 baud becomes 1200 baud of actual data sent. 0's become '01' and 1's become '10'. I just use a lookup table to do both the encoding and decoding conversion...simple enough and avoids any complex logic at both ends. And don't wait between characters. With those modules, the data slicer begins to discharge itself after a couple of milliseconds. If you pause the data stream, the data slicer will lose it's mind and you'll have to send more sync-bytes. Check out www.laipac.com for a bit more in-depth information on those type modules.

More advise from me that may work for you (or not, who knows), switch over to 4800 baud, maybe 9600 if it ends up working (I've had 19,200 baud working intermittently at 200 feet). With the 4800 or 9600 baud rate, you can send more sync-bytes ($55AA) to help set up the receiver.

JDG

RYTECH
- 27th December 2005, 02:15
Im satisfied for now with the N1200, it seems to get my data over fast enough, however i'm trying to make my outgoing to PC from intermediate receiver PIC provide a "fixed length string" when it transmits its data (decimal) values to the PC. But this is quite hard cause i cant seem to figure out how to count the lenth of my outgoing string, which varies slightly with larger byte values. I want to make my input string a fixed length in vb i think it make parsing alot simpler as sometimes the entire "stream" is not received. sometimes a string will be chopped up or the beginning is with the end of another transmission etc... i think it will simplify this "push" and "poll" method i seem to be working.

actually im not even polling now its using vb comm controls event received "OnComm" for the input data.

psymansays
- 11th July 2008, 20:19
Hi!

I think you need to run you PC-transmitting-to-PIC pair at a different frequency than your PIC-transmitting-to-PC pair, or you can only do half-duplex communication without interference. That could explain some of your garbled received data.

skimask
- 11th July 2008, 20:23
Hi!
I think you need to run you PC-transmitting-to-PIC pair at a different frequency than your PIC-transmitting-to-PC pair, or you can only do half-duplex communication without interference. That could explain some of your garbled received data.
Nothing like bumping up a post that's over 2 years old...

RYTECH
- 12th July 2008, 02:36
Skimask ur right it is quite old, I regularily refer to archived or old posts for help... I welcome and encourage people to post to old threads... u never know when new information might make the difference for someone...