RS232 question...

ianr900

Member
Joined
Jul 13, 2006
Messages
18
Location
UK
Programming Experience
3-5
I am trying to transmit some control codes via the serial port using the RS232 class, and cant find the way to do it. I am able to transmit ascii characters and control chars up to 127 decimal, but not above even though my port is set to 8 data bits! The control char I would like to send is 254 decimal.

In VB6 you could set the mode to ascii or binary. Is there an equivelant in VB .NET?

Here is my port setup code:


VB.NET:
visual basic code:


With moRS232  
.Port = Port // Uses COM1
.BaudRate = 2400 // 2400 baud rate 
.DataBit = 8 // 8 data bits                                                   
.StopBit = Rs232.DataStopBit.StopBit_1 // 1 Stop bit 
.Parity = Rs232.DataParity.Parity_Even // No Parity 
.Timeout = 10

Here is the code that outputs the data:

VB.NET:
visual basic code:


moRS232.Write(Chr(254))// this line doen't work!
moRS232.Write(Chr(70))// transmits ascii "F"
moRS232.Write(Chr(3))// transmits decimal 3
moRS232.Write(Chr(0))// transmits decimal zero

Any ideas? Thanks.
 
From the SerialPort.Write Method (String) documentation:
By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater then 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.
 
set the Encoding property of SerialPort instance:
VB.NET:
[SIZE=2]sport.Encoding = System.Text.Encoding.UTF8
[/SIZE]
 
hmmm... i have the same problem,
but i cannot solve it by changing encoding type from ASCII to UTF8,
but by using UNICODE, i get it done, but then,
for each byte transmitted, 2 bytes will be received,
where the upper byte will be zero.
just need to eliminate that '0', and i'm done! YAY! :D
 
Back
Top