View previous topic :: View next topic |
Author |
Message |
ninebarmaximus
Joined: 27 Feb 2006 Posts: 20
|
visual basic and pic |
Posted: Mon Apr 24, 2006 12:31 pm |
|
|
i wrote a simple function to see if my visual basic program would interact with the pic. I am sending hex characters to the pic using vb, although vb makes me convert them to strings before i can send them using mscomm1 i know this isnt a vb forum but its kinda integral to my problem and i figured that its more likely that someone here would know vb then someone on a vb forum would know ccs.
well anyway i send some hex to the pic and the pic should send it directly back to me but i get jibberish, does anyone know why this could be happening, will show both vb code and pic code
VB Code: | ReDim commandbyte(9)
commandbyte(3) = (Text1.Text)
commandbyte(4) = (Text5.Text)
commandbyte(5) = (Text6.Text)
commandbyte(6) = (Text7.Text)
commandbyte(7) = (Text8.Text)
commandbyte(2) = 5
For i = 3 To (3 + commandbyte(2))
commandbyte(i) = Hex(HexByte(commandbyte(i)))//hexbyte converts strings to their corresponding hex ie. a2 -> hA2
Next i
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
MSComm1.PortOpen = True
'*****************************************************************************
commandbyte(1) = "w"
For i = 1 To (commandbyte(2) + 3)
MSComm1.Output = Str(Asc(commandbyte(i)))
Next i
'*****************************************************************************
start = Timer
Do
DoEvents
Loop Until (Timer >= start + 0.5 Or MSComm1.InBufferCount >= commandbyte(2))
'*****************************************************************************
instring = MSComm1.Input
MSComm1.PortOpen = False |
Code: |
PIC CODE
#include <16F74.h>
#device adc=8
#fuses NOWDT,XT, NOPUT, NOPROTECT, BROWNOUT
#use delay(clock=3645000)
#use rs232(baud=9600, parity=n, bits=8, xmit=pin_a2, rcv=pin_a4,ERRORS, stream=PC)//PC
#use rs232(baud=9600, parity=n, bits=8, xmit=pin_c6, rcv=pin_c7,ERRORS, stream=GSM)//GSM
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in=0;
BYTE next_out=0;
char command[10];
int commandc;
#define bkbhit (next_in!=next_out)
long timeout;
char answer;
#int_rda
void serial_isr()
{
int t;
buffer[next_in]=getc(GSM);
t=next_in;
next_in=(next_in+1)%BUFFER_SIZE;
if (next_in==next_out)
next_in=t; //buffer full!!
}
BYTE bgetc()
{
BYTE c;
while (!bkbhit);
c=buffer[next_out];
next_out=(next_out+1)%BUFFER_SIZE;
return(c);
}
void main()
{
enable_interrupts(global);
InitialContacts();
start:
timeout=0;
testtimer=0;
test=0xFF;
while(!kbhit(PC)&&(++timeout<1000))
delay_us(10);
if(kbhit(PC))
{
answer=getch(PC);
if (answer=='w')
{
commandc=getch(PC);
fprintf(PC,commandc);
for (i=0;i<=commandc;i++)
fprintf(PC,"%X",command[i]);
goto start;
}
else
{
goto start;
}
}
else
{
goto start;
}
}
|
in a previous post i wanted to know how to use interrups for 2 rs232's but realised that i should always know how much information im sending so the first byte received is the mode ie. w
the second byte is the amount of bytes to be recieved commandc
and the rest is the information that is sent by VB
i know this is quite a long post but i hope someone could be able to tell me why im not getting nice hex on the vb side
cheers |
|
|
zogbog
Joined: 02 Jun 2004 Posts: 16
|
|
Posted: Mon Apr 24, 2006 12:39 pm |
|
|
I dont know how to do this but i agree that it could be a useful topic as it is something that i am also working towards. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Mon Apr 24, 2006 2:46 pm |
|
|
Quote: | Code: | fprintf(PC,"%X",command[i]); |
|
The length of your output hex string might be varying. For example 0x0A might appear as just A and not 0A.
Code: | fprintf(PC,"%2X",command[i]); |
Also, could you pst your expected output and actual output? |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Mon Apr 24, 2006 2:55 pm |
|
|
In VB, use
MsComm1.output = chr$(&haa) 'send 0xaa to serial port
Just to test to see if you get 0xaa back.
Also, if "instring" is a string of reflected hex characters, it may look like jibberish when VB tries to display it as ascii text. You need to look at each character that comes in.
What is being typed into text1.text etc? |
|
|
ninebarmaximus
Joined: 27 Feb 2006 Posts: 20
|
|
Posted: Tue Apr 25, 2006 2:49 am |
|
|
What is being sent at the moment is 00 C0 A2 02 0D ill try these ideas and ill get back to ye |
|
|
ninebarmaximus
Joined: 27 Feb 2006 Posts: 20
|
|
Posted: Tue Apr 25, 2006 5:00 am |
|
|
right i changed code slightly to see what vb is acutally doing and to be honest i dont really know what its doing, using the following code im sending w 1 FF, that is a letter a number and a hex number and the reponce i recieve is w 70 46
The following is the vb and the pic code
VB CODE
Code: | Private Sub CharTEST_Click()
Dim counter As Integer
ReDim commandbyte(5)
commandbyte(1) = "w"
commandbyte(2) = 1
commandbyte(3) = &HFF
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
MSComm1.PortOpen = True
MSComm1.Output = commandbyte(1)
MSComm1.Output = Chr$(commandbyte(2))
MSComm1.Output = Hex(commandbyte(3))
start = Timer
Do
DoEvents
Loop Until (Timer >= start + 0.5 Or MSComm1.InBufferCount >= 10)
'*****************************************************************************
counter = MSComm1.InBufferCount
instring = MSComm1.Input
MSComm1.PortOpen = False
Text3.Text = ""
Text4.Text = instring
End Sub |
PIC CODE
Code: | if (answer=='w')
{
fprintf(PC,"w");
commandc=getch(PC);
command[1]=getch(PC);
fprintf(PC,"%U",commandc);
fprintf(PC,"%2X",command[1]);
goto start; |
|
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Tue Apr 25, 2006 7:14 am |
|
|
On the PIC side, try
Code: |
fprintf(PC,"w%c%c",commandc,command[1]);
|
On VB side why use the HEX(... just use:
Code: |
MSComm1.Output = chr$(commandbyte(3))
|
|
|
|
ninebarmaximus
Joined: 27 Feb 2006 Posts: 20
|
|
Posted: Tue Apr 25, 2006 9:22 am |
|
|
to be perfectly honest i dont see how that is much different to what i was doing but thank you very much it is now at least doing what i want to do |
|
|
slo Guest
|
|
Posted: Tue Apr 25, 2006 1:45 pm |
|
|
Why not just loop the VB application back on itself by connecting pins 2 & 3 together on the PC serial port. This will tell you if it's the VB part or the PIC that's not doing what you expected.
Steve. |
|
|
|