D wiseman

I am trying to send normal characters (example A thru Z: ascii 1-126) and control characters (non printable characters above ascii 126) through a serial port to a special industrial printer.

The control codes are needed to turn the printer on/off, allow bold text, etc¡­

I have no problems sending normal characters like A through Z using ascii character encoding.

But when I send anything above ascii 126, it sends the ¡° ¡± question mark character which is what ascii character encoding is supposed to do (as per the documentation).

I have two PC¡¯s connected thru a null-modem serial cable, one is running the VB code, the other is running a terminal emulator so I can see the character and ascii code sent.

Here is what I get using ASCII encodeing

Hex code received Character RECEIVED

41 53 3F 3F AS

Note hex 41 = dec 65

Hex 53 = dec 83

Hex A1 =161

HEX DE = 222

When I use Unicode encoding, I get the following:

Hex code received Character RECEIVED

41 00 53 00 A1 00 DE 00 A.S.*,*.

Where * = the proper control character (can¡¯t show it cause unprintable)

So, the Unicode encoding looks good but it sends two bytes per character which is what the documentation said it should.

Problem is that my printer only handles one byte characters and gets confused with the extra hex 00 characters.

This is what I need the output to look like:

Hex code received Character RECEIVED

41 53 A1 DE AS**

Where * = the proper control character (can¡¯t show it cause unprintable)

I think the problem lies in the encoding. I tried every possibility (ie ASCII, UTF8, UTF32...)and it will not give me what I want. Maybe there is another routine other than this built in serialport function

Here is my code on an XP machine SP2, running Visual Basic 2005 on visual studio 2005

I do not suggest running the code unless you have 2 PC¡¯s connected with a null modem cable between the serial ports.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim strPortName As String

Dim portName As String = "COM1"

Dim baudRate As Integer = 38400

Dim byte1 As Byte

Using ComPort As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")

ComPort.BaudRate = 38400

ComPort.Handshake = Handshake.RequestToSend

ComPort.Parity = Parity.None

ComPort.ReadTimeout = 5000 'read timeout in milliseconds

ComPort.Encoding = System.Text.Encoding.Unicode ¡®**** CHARACTER ENCODING

strPortName = ComPort.PortName

If ComPort.IsOpen = True Then

If ComPort.CtsHolding = True Then 'check clear to send line

¡®*************** HERE IS WHERE THE CHARACTERS ARE SENT

ComPort.Write(Chr(65)) '(ascci 65 or letter A)

ComPort.Write(Chr(83)) '(ascci 83 or letter S)

ComPort.Write(Chr(161)) '(ascii 161 controll character)

ComPort.Write(Chr(222)) '(ascii 222 controll character)

End If

End If

ComPort.Close()

End Using

End Sub



Re: Visual Basic Language VB2005 Sending ascii characters larger than ascii 126 using SerialPort.Write command

nobugz

Your problem is indeed encoding. There is no encoding for character codes > 127 in ASCII, they thus produce a question mark. Instead of writing characters, you should write bytes. Easy enough in your snippet, just write ComPort.Write(161).

Oh, and don't use CtsHolding, set the Handshake property to RequestToSend. Oh, and don't call ComPort.Close() in your button Click event, call it in your FormClosing event. Open the port in your Load event.





Re: Visual Basic Language VB2005 Sending ascii characters larger than ascii 126 using SerialPort.Write command

Nerio Zamora

I was the same problem and found a good solution,

Encoding = System.Text.Encoding.Default

Nerio