Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Receiving byte with value of 0x1A

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2008, 02:00 AM
Basic4ppc Veteran
 
Join Date: May 2008
Location: Newcastle Upon Tyne - England
Posts: 271
Awards Showcase
Beta Tester 
Total Awards: 1
Default Receiving byte with value of 0x1A

I'm writing an app to receive data via the serial port and display the byte values. I've stripped out the project for the attachment. In the 'On Com' event, you'll see that at first I was using Port.InputString - however if the byte was greater than 0x7F it was changed to 0x3F ('?'), presumably by Serial2.dll.

I changed to using Port.InputArray and now receive the full range of bytes from 0x00 to 0xFF, but now, if opto compiled, I get the error attached if 0x1A is sent. If legacy or IDE compiled, sending a single 0x1A byte fires 'On Com' twice and opto gives the error. I've included an app that may be compiled for a device and connected with an RS232 cable but I appreciate not many will have that facility.

What does Port.TimeOut actually do?

Finally, if I use CTSHandshaking=True, do I still have to assert RTSEnable?
Attached Files
File Type: zip Receive.zip (11.7 KB, 19 views)
Reply With Quote
  #2 (permalink)  
Old 10-17-2008, 08:33 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by Zenerdiode View Post
however if the byte was greater than 0x7F it was changed to 0x3F ('?'), presumably by Serial2.dll.
Yes, that is expected behaviour. InputString is based on a .NET Stream. Streams support encodings, Unicode, ASCII or code pages. The default is ASCII whose valid characters are 0x00 to 0x7F. This can't be changed in Basic4ppc except by using the Door library.

Quote:
I changed to using Port.InputArray and now receive the full range of bytes from 0x00 to 0xFF, but now, if opto compiled, I get the error attached if 0x1A is sent. If legacy or IDE compiled, sending a single 0x1A byte fires 'On Com' twice and opto gives the error.
I can't explain what is happening except to remark that it is probably not coincidental that 0x1A is the Ctl-Z character which is used for XonXoff handshaking if it were enabled.

Quote:
What does Port.TimeOut actually do?
When bytes are read or written to the port if the read or write doesn't succeed within the timeout then the call returns. In Serial2 the read process is detached from this by having a buffer in which incoming bytes are assembled and the read is done on this buffer so a timeout on read can't occur. The Output methods do write directly and so might timeout if the handshake method in use is preventing data being sent. The default timeout is set to 100mS when you open the port.

Quote:
Finally, if I use CTSHandshaking=True, do I still have to assert RTSEnable?
No, it is an error to do so and you should get an exception thrown if you try when either CTSHandshaking or (strangely!) XonXoffHandshaking are true. When you enable CTSHandshaking the system manages the state of the RTS pin.
Reply With Quote
  #3 (permalink)  
Old 10-17-2008, 10:28 AM
Basic4ppc Veteran
 
Join Date: May 2008
Location: Newcastle Upon Tyne - England
Posts: 271
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Many thanks Andrew

0x1A is the control character 'SUB' or substitute (with 0x11 and 0x13 being XON and XOFF respectively)

A work around I've discovered is to trap the error and do nothing with it - as the sub still returns the value 1A to the calling sub. Its not my preferred solution.

Code:
Sub Port_OnCom
    ErrorLabel(BadHex)
    Buffer()=Port.InputArray
    
For i = 0 To ArrayLen(Buffer())-1
        ReceivedString=ReceivedString&
Chr(Buffer(i))
    
Next
    
Return
    BadHex:
    
Return
End Sub
This only works for Optimised compilation, the OnCom still gets fired twice on legacy or IDE. That doesn't really concern me as the app will be compiled with optimisation.
Reply With Quote
  #4 (permalink)  
Old 10-17-2008, 10:45 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by Zenerdiode View Post
0x1A is the control character 'SUB' or substitute (with 0x11 and 0x13 being XON and XOFF respectively)
engage brain before writing down quick assumptions! Ctl-Z rang a bell as special character for something so I blithely wrote down a rubbish assumption It is of course the old DOS end-of-file character for text files. I'm sure I have had RS232 comms issues with it in the past - problem is I can't remember what they were
Reply With Quote
  #5 (permalink)  
Old 10-17-2008, 12:49 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

I connected one of my laptops to my desktop by RS232 and got the same results as you. Here's what I think is happening based on this Serial Communication with Visual Basic .NET

The .NET SerialPort class apparently harks back to the DOS days and treats 0x1A as a special case and creates an additional OnCom event when one arrives to notify the fact. This extra event can be identified in VB.NET or C# code by an event argument but you don't get this with the Serial2 library. This event is additional to the one for the character and probably explains why you get two events in legacy/IDE.

I suspect that the error in an optimised app is caused by SerialPort.InputArray returning a null reference for this extra event. Why this doesn't error in the IDE I have no idea but the hypothesis fits the facts. A workaround would appear to use SerialPort.InputString but we need to get the full range of characters which it does not by default. This can be accomplished by assigning an Encoding object to the SerialPort using the Door library as in the attached library
Attached Files
File Type: sbp ReceivesOKnow.sbp (2.3 KB, 16 views)
Reply With Quote
  #6 (permalink)  
Old 10-17-2008, 06:17 PM
Basic4ppc Veteran
 
Join Date: May 2008
Location: Newcastle Upon Tyne - England
Posts: 271
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Almost, although byte values between 0x80 and 0x9F are returning peculiar values. For example, send 0x80 and it is interpreted as 0x20AC or 8364. I can't work out the relationship to 128. Is it a codepage thing?

Quote:
Originally Posted by agraham View Post
Ctl-Z rang a bell ...
So does Ctl-G
(I'll erm, I'll get my coat...)
Reply With Quote
  #7 (permalink)  
Old 10-17-2008, 06:39 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by Zenerdiode View Post
Is it a codepage thing?
Yes dammit I'll have another think tomorrow - my turn to cook tonight
Reply With Quote
  #8 (permalink)  
Old 10-19-2008, 09:15 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Let's forget the String idea, Unicode gets in the way whatever code page you use

I have single stepped the Serial library code in VS2005 and what is happening is almost exactly as I said. Two events are raised for 0x1A, when you do InputArray for the first event you get the character and empty the input buffer, the second event also does InputArray but as the buffer is empty a null reference is returned. The IDE, being interpreted, seems to trap this null and leaves Buffer() with its previous contents, hence the apparent duplication of the character(s) previously read, whereas an optimised compiled app just tries the assignment and causes an exception.

Attached is a version of the Serial library that does not raise the extra EOF event. I'll raise the issue as a bug and hopefully Erel will make an official update of Serial2.
Attached Files
File Type: zip SerialEx.zip (4.0 KB, 19 views)
Reply With Quote
  #9 (permalink)  
Old 10-19-2008, 06:12 PM
Basic4ppc Veteran
 
Join Date: May 2008
Location: Newcastle Upon Tyne - England
Posts: 271
Awards Showcase
Beta Tester 
Total Awards: 1
Default



Sincere thanks for your work on this. I've updated my project to include your SerialEx. I hope Erel has a chance to update Serial2 - perhaps even allowing us to switch on the enumeration - just in case someone somewhere needs the use of 0x1A EOF.

The article you directed me to made for interesting reading. I would have good use for the events listed in the SerialPort Events paragraph. I'll tag a request to your bug report requesting the feasibility.

Christopher
Reply With Quote
  #10 (permalink)  
Old 11-19-2008, 02:41 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

My "official" release of an expanded SerialEx with help and source for merging now released here http://www.basic4ppc.com/forum/addit...html#post18998
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
put byte value into a string cotralis Questions (Windows Mobile) 5 09-13-2008 03:30 AM
how to display chinese characters (High-ASCII, double-byte) using sqlite3 dennychuang Questions (Windows Mobile) 5 05-22-2008 04:39 PM
JPEG to byte array without writing to file brathbone Questions (Windows Mobile) 2 03-01-2008 05:59 AM
Outlook library now supports - sending and receiving SMS messages. Erel Announcements 3 09-10-2007 09:49 PM


All times are GMT. The time now is 02:49 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0