|
-
Feb 7th, 2011, 05:21 AM
#1
Thread Starter
Junior Member
help with MSCOMM output Method
i have problem with using mscomm1.output method
the output string contains ascii & hex values i cann't convert hex values which are between 0 to 32 ascii( as they are non printing characters
can anyone help me?
-
Feb 7th, 2011, 08:00 AM
#2
Re: help with MSCOMM output Method
.Output isn't implemented as a Method, it's a Property.
When you say "hex values" you probably mean "binary values" instead? Or perhaps you're just talking about non-printing characters?
As far as I can tell you can use .Output for any String characters in the 0-127 range without incident, even though the documentation suggests you use Byte arrays instead. True binary data with bytes ranging from 0-255 needs to be sent using Byte arrays to avoid possible conversion errors though.
But maybe none of that is important here. You seem to be asking about display or perhaps entry of non-printable characters.
I think we need a more elaborate definition of the problem, perhaps with a sample of the code you're having trouble with.
-
Feb 7th, 2011, 11:37 PM
#3
Thread Starter
Junior Member
Re: help with MSCOMM output Method
For communication between GUI & Controller there are different frames. These are as follows:
1. Setting Frame
2. Connect Frame.
3. Start Frame.
4. Get Real Time Data Request Frame.
5. Set Time Frame.
6. Ready Frame.
7. Stop Frame.
8. Data Frame.
Note: All Command bytes are in 'Hex'.
Except Command bytes all other bytes are in 'ASCII'.
1. Setting Frame: This Frame is used for setting of Parameters(channel,time interval) .It's size is
7 bytes. Controller respond this frame with format It's structure is shown below:
START
[1]
COMMAND
[1]
CHANNEL SELECT
[1]
TIME INTERVAL
[2]
ERROR CODE
[1]
STOP
[1]
START : It is used for indicating start of Setting frame. This is 1 byte of data. “#” is the start byte.
COMMAND: This is 1 byte of data. 0X03 is the command for Setting frame.
CHANNEL SELECTION: This is 1 byte of data used for setting which channels are to be used.
X WM RADIATION TEMP CURRENT2 CURRENT1 VOLT 2 VOLT 1
Fig. 2 Channel Selection format.
e.g. For selecting 2 voltage,1 current ,temperature & radiation channel , code is 0x37 .
0 : Channel Absent.
1: Channel Present.
0 0 1 1 0 1 1 1
TIME INTERVAL: This is 2 byte data used for setting the data logging interval. This interval is
selected from drop down list from GUI .GUI converts time in seconds & convert it into hex format
in 2 byte format MS Byte first.
e.g. For 1 min code is 0x003C , for 1 hr code is 0x0E10
ERROR CODE: This is 1 byte of data for error handling. Error code is calculated by 'Ex-OR”ing
the bytes. First byte will Ex-OR with second & the result will again Ex-OR with third byte
& so-on. Error code calculation should not include START, STOP bytes.
STOP: This is 1 byte of data for indicating end of Setting Frame. “@” is the stop byte.
START
[1]
COMMAND
[1]
ERROR CODE
[1]
STOP
[1]
The frame shown above is of 4 byte length. This is response of Setting Frame sent by GUI to
controller. GUI compares the received error code with transmitted one. If match occurs the
frame sent successfully otherwise retransmit it.
2. Connect Frame: This frame is used as Request for connecting the GUI to Controller. It is of 3
bytes in length. Response of this frame is Echo frame on successful connection.
It's structure is shown below:
START
[1]
COMMAND
[1]
STOP
[1]
Fig.4 : Connect Frame From GUI to Controller
START
[1]
COMMAND
[1]
STOP
[1]
START : It is 1 byte of data used for indicating start of Connect frame.”#” is the start byte.
COMMAND: This is 1 byte of data. 0X02 is the command for Connect frame.
STOP: This is 1 byte of data for indicating end of Connect frame. “@” is the stop byte.
3.Start Frame: This frame is used for Starting the communication between GUI & Controller.
It is of 3 bytes in length. Response of this frame is Echo frame on successful connection.
It's structure shown below:
START
[1]
COMMAND
[1]
STOP
[1]
Fig.6: Start Frame from GUI to Controller
START
[1]
COMMAND
[1]
STOP
[1]
Fig.7: Start Frame from Controller to GUI.
START : It is 1 byte of data used for indicating start of Start frame. “#” is the start byte.
COMMAND: This is 1 byte of data. 0X05 is the command for Start frame.
STOP: This is 1 byte of data for indicating end of Start frame. “@” is the stop byte.
4. Get Real Time data Request Frame: This frame is used for getting the real time data from
controller. It is of 3 bytes in length. In response to this frame it gives the data frame. It's
structure shown below:
START
[1]
COMMAND
[1]
STOP
[1]
Fig.8: Get Real Time data Frame from GUI to Controller
START : It is 1 byte of data used for indicating start of Get Real Time data frame. “#” is the
start byte.
COMMAND: This is 1 byte of data. 0X06 is the command for Get Real Time data frame.
STOP: This is 1 byte of data for indicating end of Get Real Time data frame. “@” is the stop
byte.
5. Set Time Frame: This frame is used for sending the time & date for RTC(Initial setting).
This frame is of 21 bytes. It's structure is shown below:
START
[1]
COMMAND
[1]
TIME & DATE
[17]
ERROR CODE
[1]
STOP
[1]
Fig.9: Set Time Frame from GUI to Controller
START: It is 1 byte of data used for indicating start of Set Time frame. “# “ is the start byte.
COMMAND: This is 1 byte of data. 0X04 is the command for Set Time frame.
TIME & DATE: This is 17 byte of data. All time setting bytes are ASCII.Its format shown below:
HH HH : MM MM : SS SS DD DD / MM MM / YY YY
Fig.10: Time & Data Format
GUI should get timing information from PC. Time is in 24 Hr. Format.
ERROR CODE: This is 1 byte of data for error handling. Error code is calculated by 'Ex-OR”ing
the bytes. First byte will Ex-OR with second & the result will again Ex-OR with third byte
& so-on. Error code calculation should not include START, STOP bytes.
STOP: This is 1 byte of data for indicating end of Set Time Frame. “@” is the stop byte.
START
[1]
COMMAND
[1]
ERROR CODE
[1]
STOP
[1]
Fig.11: Response of Set Time Frame(Controller to GUI)
Upon receiving the Set time Frame controller response back the frame shown above. GUI
compare the Error Code received with transmitted one. If match occur then it means
frame received successfully at controller end otherwise it is require to re-transmit the frame.
6. Ready Frame: This frame is used for checking the readiness of both controller & GUI. It is
bidirectional frame. It is of 3 bytes in length. This frame should be transmitted before starting
communication.
START
[1]
COMMAND
[1]
STOP
[1]
Fig.12: Ready Frame
START : It is 1 byte of data used for indicating start of Ready frame. “#” is the start byte.
COMMAND: This is 1 byte of data. 0X01 is the command for Ready frame.
STOP: This is 1 byte of data for indicating end of Ready frame. “@” is the stop byte.
7.STOP Frame: This frame is used for terminating the communication between controller &
GUI. It is of 3 bytes in length.
START
[1]
COMMAND
[1]
STOP
[1]
Fig.13: Stop Frame
START
[1]
COMMAND
[1]
STOP
[1]
Fig.14: Stop Frame (Response)
START : It is 1 byte of data used for indicating start of Ready frame. “#” is the start byte.
COMMAND: This is 1 byte of data. 0X07 is the command for Ready frame.
STOP: This is 1 byte of data for indicating end of Ready frame. “@” is the stop byte.
8.DATA Frame: This frame is used for sending the data to GUI from Controller. It is of 60 bytes
in length. All bytes are ASCII.
START
[1]
CH1
[6]
CH2
[6]
CH3
[6]
CH4
[6]
CH5
[6]
CH6
[6]
CH7
[5]
TIME
[8]
DATE
[8]
ERROR
CODE
[1]
STOP
[1]
Fig.15: Data Frame
START: It is 1 byte of data used for indicating start of Data frame. “#” is the start byte.
CH1-CH4 & CH6: It include the various channels data such as VOLT1,VOLT2,
CURR1,CURR2, ANALOG . It is of 6 bytes each.
e.g : If CH1 channel has 251.32V then it's format is shown below:
2 5 1 . 3 2
Fig.16: Data format.
CH5: It shows data from temperature channel. Its format shown below:
+ 2 0 . 3 4
Fig.17 Data format for Temperature channel.
It means that Temperature channel has +20.34° C temperature.
CH7: It shows water meter readings.
1 2 0 6 5 6
Fig.18 Data format for Water meter channel.
TIME: It shows the Time in HR:MM: SS Format.
H H : MM MM : SS SS
Fig.19 Data format for Time .
DATE: It shows the Date in DD/MM/ YY Format.
D D / MM MM / YY YY
Fig.20 Data format for Date .
ERROR CODE: This is 1 byte of data for error handling. Error code is calculated by 'Ex-OR”ing
the bytes. First byte will Ex-OR with second & the result will again Ex-OR with third byte
& so-on. Error code calculation should not include START, STOP bytes.
STOP: This is 1 byte of data for indicating end of Set Time Frame. “@” is the stop byte.
COMMANDS SEQUENCE FROM GUI TO CONTROLLER & RESPONSES:
SR.NO COMMAND RESPONSE
1 READY [0x01] ECHO
2 CONNECT [0x02] ECHO
3 SETTING [0x03] ERROR ECHO
4 SET TIME [0x04] ERROR ECHO
5 START [0x05] ECHO
6 GET DATA [0x06] DATA FRAME
7 STOP [0x07] ECHO
After START Command controller starts sending DATA FRAME as per the parameters set in
SETTING command.
Error Handling & Retransmission:
Time Out Period : It is time period for which GUI waits for response from controller. It is 500ms.
Retransmission Period: If Time out period is over GUI retransmit the frame after each 1 sec.
Interval.
No. of Retransmission : GUI Retransmit One frame max. 3 times After that it gives Error
indication .
the vb code that i use to send data is as folows
frmTester.MSComm1.Output = "#3337000A@"
-
Feb 8th, 2011, 12:43 PM
#4
Re: help with MSCOMM output Method
That's an impressive amount of detail but it also manages to confuse the issue somewhat.
VB6 String types do not contain ASCII data. They contain UTF-16LE Unicode characters (16 bits each).
In order to help deal with the transition to Unicode, VB6 uses a mix of approaches instead of having two types of String variables and literals.
A String variable is always Unicode (unless you forcibly store ANSI, not ASCII, into it).
A String literal in source code (quoted text) is always ANSI encoded using your current locale/codepage settings. When it gets compiled it gets translated back to Unicode according to the current codepage.
As part of this VB6 "bridging" of the ANSI past and Unicode present, many VB6 components and structures (Declare-based API calls, native I/O, etc.) also will perform translation between a Unicode String variable's contents and ANSI. The MSComm control does this for example.
So when you need fine control over things you must either stick to using Byte arrays (not Strings) or else use Strings that only contain characters in the "safe" range of 0 to 127 (decimal).
Ok, so we have this example:
Code:
frmTester.MSComm1.Output = "#3337000A@"
What this will do is translate the ANSI character string to Unicode for use at runtime, based on the codepage in effect when the program is compiled.
Then at runtime this Unicode String gets translated to ANSI according to the codepage in effect at runtime (which might be different) because that's how the interface to the .Output property is defined when a String value is assigned to it.
The value sent will be 10 octets (roughly the same as saying Byte, but not quite) with the value (in hex):
Code:
23 33 33 33 37 30 30 30 41 40
If this is what you want, it is safe since all of those characters are in the "safe" (ASCII) range, which is the same in nearly all ANSI codepages.
But perhaps that wasn't what you were after.
If you were instead to code this as:
Code:
frmTester.MSComm1.Output = "#" & Chr$(&H33) & Chr$(&H37) & Chr$(&H00) & Chr$(&H0A) & "@"
... then what gets sent is:
... again, working because all of the characters are in the safe range (under 128 decimal).
You can either use the bulky style of coding shown above, or you might also use Byte arrays (which won't get translated to/from ANSI and Unicode).
I have also posted a pair of Classes you can include in your program that might make this a little easier to deal with. See Escer, Hexer Classes and EscBuddy Tool.
Using these you have two options, one String based and the other Byte array based:
Code:
frmTester.MSComm1.Output = Escer.UnEscape("#\x33\x37\x00\x0A@")
frmTester.MSComm1.Output = Hexer.HexToBinary("233337000A40", hfmtHexRaw)
The first creates a String and assigns it to the .Output property. It is safe because the hex values there are 7-bit (below 128 decimal).
The second translates a String of hex digits in "raw" format into a Byte array which is assigned to the .Output property. It can use 8-bit values safely because it doesn't have to store them into a String, however the entire thing has to be expressed as hex digit pairs.
I don't know if this helps or not. Escer/Hexer might just add confusion on top of the confusion you might have about the relationships among ASCII, ANSI, Unicode, and binary data in a VB6 program.
I hope I haven't confused myself trying to write this!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|