Serial I/O

genejames

Member
Joined
Oct 1, 2006
Messages
7
Programming Experience
10+
Where should I post this?

I have a simple project that controls devices via serial I/O. I'm new to VB, using VB Express. I design hardware and normally write assembler, C or BASIC for tiny processors and VB5 is sure full of surprises for someone with my background!
So I used this construction (and tried many variations that didn't work either):

VB.NET:
'send DVK100 start keying commands (select cam1)
Using KeyerCom As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort(KeyerPortName, 38400, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
KeyerCom.DtrEnable = True
KeyerCom.Write(Chr(&H0) & Chr(&HFE) & Chr(&H40) & Chr(&H67) & Chr(&H31) & Chr(&H5F) & Chr(&H5D) & Chr(&HFF))
End Using

I have seen this cited as an example on numerous VB help sites. It's even in the VB help files. It's simple and clean and I may need to read as well so it's appealing.
Result (from a serial monitor): the H00 is not sent, the HFE becomes H3F followed by H00, the rest are correct, but each byte is followed by H00
My conclusion is it's converting to 16-bit UNICODE. I have spent hours trying to find how to disable such conversion to no avail. I'm thinking that I want it to be UTF8, but that's just a guess at this point.
Using MSComm looks to be too advanced for me at this point. No VB programmer I've contacted has a clue about serial I/O and they won't take a contract to code it correctly for me.
Thanks in advance.
Gene James
 
Last edited by a moderator:
Im new here but your project sounds interesting.
Just a few off the wall comments that probably won't help

1. I'm sure you remeber the parity, stop bits, data bits ie 8N1
2. The Letter 'U' is a square wave
3. preemptive mode
4. some serial monitors have data settings

Just hoping something might...
 
These are verified correct

Thanks Zekeman,
1- Yes, 8N1 is correct. The serial device responds to 1 byte commands that are less than /3f where the byte isn't mangled by VB and the added NUL is ignored by the device.
2- aesthetics?
3- Don't know abt preemptive mode but assuming not an issue due to (1) above
4- The serial monitor is verified as it shows the right strings from other programs.

Have the feeling that I'm looking for a environmental setting that tells VB that strings are single byte, not double byte.
-GJ
 
You can change the SerialPort.Encoding property, default is ASCII. A .Net character is two bytes (Char data type). The length of a string converted to byte array may vary according to the encoding and character value - here a single character may only use one byte.
 
Thanks John,
It would seem to me that if default is ASCII, I would not be getting 2 bytes out for each character. Does that seem right to you? Is there some global setting WinXP that might cause VB to acquire a differnt default.
Still, I'd like to try to set it explicitly and I have not been able to find how to change the SerialPort.Encoding property, though I did see a GetEncoding property.
I also note that all the examples I have see are concerned with ASCII text. In this case, the correct character IS sent, followed by a NUL. If one is just looking at the text, say on a terminal, NULs may not displayand all will look well. OTOH, I dount that I'd be the first to find a VB bug.
-G
 
No, system doesn't override classes default settings. SerialPort class doesn't have a GetEncoding property, see the members list in documentation.
With ASCII encoding you are getting one byte per character in a string (but miss char values>127 to '?'s 63), check the byte array length:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] b() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Byte[/COLOR][/SIZE][SIZE=2] = System.Text.Encoding.ASCII.GetBytes([/SIZE][SIZE=2][COLOR=#800000]"code"[/COLOR][/SIZE][SIZE=2])[/SIZE]
Set the Encoding property of SerialPort instance:
VB.NET:
[SIZE=2]sport.Encoding = System.Text.Encoding.UTF8[/SIZE]
 
Thanks.
I've read up on Unicode and am somewhat more enlightened now.
So we know that chars>7F are translated to multi-bytes. The extra /00 following every byte still seems wrong.
Absent a "NoEncoding" or "Raw" property and given that the write method will not accept byte data, am I to conclude that I simply cannot send 8-bit ASCII ? Seems like that would be a design oversight.
 
Hi John,
Almost working. I must not have expressed myself well, because the main error was sending char data instead of bytes:

[FONT=Courier New, Courier] [/FONT][FONT=Courier New, Courier]Dim[/FONT][FONT=Courier New, Courier] TestData() [/FONT][FONT=Courier New, Courier]As[/FONT][FONT=Courier New, Courier] [/FONT][FONT=Courier New, Courier]Byte[/FONT][FONT=Courier New, Courier] = {&HFE, &H11, &H11, &HFF}
[/FONT][FONT=Courier New, Courier]Using[/FONT][FONT=Courier New, Courier] KeyerCom [/FONT][FONT=Courier New, Courier]As[/FONT][FONT=Courier New, Courier] IO.Ports.SerialPort = _
[/FONT][FONT=Courier New, Courier]My[/FONT][FONT=Courier New, Courier].Computer.Ports.OpenSerialPort(KeyerPortName, 38400, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
[/FONT][FONT=Courier New, Courier]KeyerCom.Write(TestData, 0, 4)
[/FONT]
This sends the bytes without translation, but it still adds a NUL (&H00) after every byte sent.
So the stream actually sent is: FE 00 11 00 11 00 FF 00

Any ideas?
 
Again Im just new here but are you sure about preemptive mode.
It probably wouldn't work at all if this was out of wack but..

I think have used this product before with success:
See the fifth paragraph down.

www.vsres.com/activeport/
 
Hi John,
Almost working. I must not have expressed myself well, because the main error was sending char data instead of bytes:

[FONT=Courier New, Courier] [/FONT][FONT=Courier New, Courier]Dim[/FONT][FONT=Courier New, Courier] TestData() [/FONT][FONT=Courier New, Courier]As[/FONT][FONT=Courier New, Courier] [/FONT][FONT=Courier New, Courier]Byte[/FONT][FONT=Courier New, Courier] = {&HFE, &H11, &H11, &HFF}
[/FONT][FONT=Courier New, Courier]Using[/FONT][FONT=Courier New, Courier] KeyerCom [/FONT][FONT=Courier New, Courier]As[/FONT][FONT=Courier New, Courier] IO.Ports.SerialPort = _
[/FONT][FONT=Courier New, Courier]My[/FONT][FONT=Courier New, Courier].Computer.Ports.OpenSerialPort(KeyerPortName, 38400, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)
[/FONT][FONT=Courier New, Courier]KeyerCom.Write(TestData, 0, 4)
[/FONT]
This sends the bytes without translation, but it still adds a NUL (&H00) after every byte sent.
So the stream actually sent is: FE 00 11 00 11 00 FF 00

Any ideas?
 
The last code posted is actualy fine. My serial monitor is not!

My monitor (COMMSNIFFER 1.0.32) WAS tested and working fine last week ... on an XP box. I was monitoring, however, with a WinME machine and there the COMMSNIFFER shows that extra NUL. Thinking I was crazy, I retested COMMSNIFFER on the XP box and it works fine. And so does my code.

Tks to all for help.
 
Back
Top