Sending Ctrl Characters/non-text characters via serial..

General HouseBot discussion. Any issues that don't fit into any of the other topics belong here.
Post Reply
MediaStorm
Member
Posts: 30
Joined: Sat Jun 28, 2003 1:58 pm

Sending Ctrl Characters/non-text characters via serial..

Post by MediaStorm »

How in the heck do I send something like this via a serial command:



Chr(0) + "VOL2B" + Chr(1)



(2B is actually a hex value that it is replaced via %x%)



I'm going nuts trying to figure out how to get the damn ctrl characters sent without them getting converted to ASCII.



The returned variable shows as 0VOL2B1 (wrong)



The sent data from the trace is 30 56 4f 4c 32 42 31 which = 0VOL2B1 as well...(also wrong)



I need to specifically send a combination of hex and ascii data exactly as:



&H0 (or Int[0]) VOL2B (VOL is ASCII - 2B is HEX) &H1 (or Int[1])



I've got several knots on my forehead shaped like the front of my desk from trying to get this working.. :wink:





Receiving and parsing is a piece of cake.. (Incoming start/stop characters are CHR(3) and CHR(4) -- Outgoing is CHR(0) and CHR(1))



This works perfectly for incoming data:

Code: Select all

Dim szDataIn, szMsgData
szDataIn = GetPropertyValue("Kenwood_VR5700.Received Data")
szDataIn = Replace(szDataIn,Chr(3),"")
szDataIn = Replace(szDataIn,Chr(4),"")

If Left(Ucase(szDataIn),3) = "VOL" Then
	szMsgData = CLng("&H" & Right(szDataIn,2))
	SetPropertyValue "Kenwood_VR5700.Volume", szMsgData
End If
kbosscawen
Member
Posts: 71
Joined: Thu Jul 03, 2003 8:53 am
Location: Raleigh, NC
Contact:

Post by kbosscawen »

At the risk of showing my ignorance, is it possible that VB is typecasting the hex characters into ASCII because you're combining them with a string to assign to a variable? I did some testing....



The following code returns the results that you experienced (ie, 0VOL2B1 is displayed in the message box):

Code: Select all

test= chr(0) + "VOL2B" + Chr(1)
MsgBox test
...but the following code displays nothing (null) in the message box

Code: Select all

test= "VOL2B"
MsgBox chr(0) + test + Chr(1)


I'm thinking that since the second snippet isn't assigning chr(0)...chr(1) to a text variable, the chr(x)s don't get typecast into strings and get sent to the function as hex instead.



I dunno - Just a thought....
ScottBot
Site Admin
Posts: 2790
Joined: Thu Feb 13, 2003 6:46 pm
Location: Georgia (USA)
Contact:

Post by ScottBot »

I will really celebrate the day when consumer electronics manufacturers adopt and implement a common control protocol. It's bad enough that they all do things differently, but some make some really bad designs (in my opinion). For sending simple control commands, I don't understand why anyone would design a protocol that uses non-ascii data. :x



Step off soap box....



The Chr(0) makes this difficult. You can't really mix hex and ascii data in a command string, so you will need to define the entire command string as Hex and convert any ascii data into hex values.



So something like 00564F4C%x%01 might work.



Scott
MediaStorm
Member
Posts: 30
Joined: Sat Jun 28, 2003 1:58 pm

Post by MediaStorm »

kbosscawen wrote:At the risk of showing my ignorance, is it possible that VB is typecasting the hex characters into ASCII because you're combining them with a string to assign to a variable? I did some testing....

The following code returns the results that you experienced (ie, 0VOL2B1 is displayed in the message box):

Code: Select all

test= chr(0) + "VOL2B" + Chr(1)
MsgBox test
...but the following code displays nothing (null) in the message box

Code: Select all

test= "VOL2B"
MsgBox chr(0) + test + Chr(1)
I'm thinking that since the second snippet isn't assigning chr(0)...chr(1) to a text variable, the chr(x)s don't get typecast into strings and get sent to the function as hex instead.

I dunno - Just a thought....
You can do it as:

Code: Select all

Const ForAppending = 8
Const ToCreateIfNotExist = True
Const TristateFalse_Ascii= 0

Const Filename="c:\myfile.txt"
Dim myLine
myLine="VOL2B"

Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.OpenTextFile(Filename, ForAppending, ToCreateIfNotExist, TristateFalse_Ascii)
f.WriteLine Chr(0) + myLine + Chr(1)
f.Close
Set f=Nothing
Set fso=Nothing
wscript.echo "done"


Open the test file output (myfile.txt) and look at it.. You should see what appears to be a space and a funky character at the end.. Look at it with a hex editor and you'll see that it does indeed write the correct character values.. It's easier to visualize with other Chr values like 3 and 4..



MsgBox has trouble with certain characters but writing to a file ususally will bring it out in the light.



This is actually a very simple serial protocol.. If you want to have some big fun, try tackling the RS-232 commands for the Kenwood DVD changers... That one took a couple of weeks in Delphi and I wouldn't even consider it using VB due to extremely tight timing issues in the handshaking and ack/nak.



I've been doing low level RS-232 interfacing for much longer than I care to admit. :wink:
Post Reply