i am trying to send a string of characters (ascii codes 0 to 255) over the serial port using the SerialPort control in C# (vs 2005 pro)
the characters arrive nice and sweet ... up to 7F, the characters with ascii code 80 and higher (hex) are sent as 3F all the time (as soon as the 8th bit is used 3F will be sent)
does know anyone what could cause this problem, and maybe how it can be solved or done otherwise ?
i am using the code below :
string strMsg = ""; for (int intI = 0; intI <256; intI++) { strMsg += Convert.ToChar(intI); } comKAR.Write(strMsg); </pre>
Assunto: Re: C# serial port sends only 3F above 7F ?
Hrqls: I'm not really sure, as I never worked directly with Serial Port. But parity errors normally occur if a) there occured an error during sending and one bit was changed or b) maybe the parity bit was corrupted by your program? Maybe your connection is set to a bytelength of 7 bit and you overwrite the parity bit? Again: I don't really have a clue, but this was what came to my mind ;) Good luck in finding the solution :)
Assunto: Re: C# serial port sends only 3F above 7F ?
Mr. Shumway: yes thats what i thought as well .. but it only occurs with characters with an ascii value of 128 (80) or higher .. and with all of them .. so it cant be an accidental loss of data
the databits are set to 8 in my program i dont know how to check the databits of the serial port on the ipaq though .. it doesnt have a real serial port .. it uses a special cable to the normal connection on the ipaq (which normally ends in an usb connector :))
maybe the cable cant handle 8 databits ? but that would be weird as its serial data and not parallel so the data itself shouldnt matter
that leaves the driver (which is a default driver as i didnt have to install anything) or the serialport control in c# (or the functions i use to control the serialport) ?
Assunto: Re: C# serial port sends only 3F above 7F ?
Hrqls: Can you try converting those 8 bits you send/receive to 7 bits? I googled a bit and found a Base64 Encoder/Decoder, but maybe the .NET framework already has something similar ???
Assunto: Re: C# serial port sends only 3F above 7F ?
hexkid: thanks
that would be nice .. but i think the receiving side has to receive decode the 7 bits back to 8 bits again and i have no control over the receiver side
will have a look though, it might be a solution :)
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.
Hrqls: the encoding classes wont help me .. i need to send 1 byte which has to hold characters from ascii value 0 to ascii value 255 .. the only encoding class which sends only 1 byte is the ascii encoding which sends the 63 (3F) when i try to send a character with ascii value of 128 or higher
but! (yeah! ;)) i can also send the string or character array as a byte array ... which then bypasses the encoding class (or simply doesnt need it)
this works! ;)
so for the loop to send all characters with ascii value from 0 to 255 i now use :
byte[] bytArray; bytArray = new byte[256]; for (int intI = 0; intI <256; intI++) { bytArray[intI] = (byte)intI; } comKAR.Write(bytArray, 0, 256);
which works flawlessly
thanks for the help and ideas .. it set me on the right track and made it work :)