1

I'm having an issue with transmitting serial data using C#.

The below code is a simple example that transmits the byte value 0x00 which comes out inverted 0xFF;

namespace Serialwrite
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SerialPort SP = new SerialPort("COM7", 9600, Parity.None, 8, StopBits.One);
            SP.Open();
            SP.Write(new byte[] { 0x00 }, 0, 1);
            Thread.Sleep(1000);
            SP.Close();
        }
    }
}

The problem is that the 8 high bits appear before the start bit as shown in the 1st image. When I was expecting the waveform to look like the 2nd image.

I'm not sure whyt this is the case.

enter image description here

enter image description here

enter image description here

Mike
  • 4,041
  • 6
  • 20
  • 37
Kenny Barber
  • 245
  • 2
  • 11
  • 1
    this is an interesting question, but I suspect most people just use `Write` and `Read`, without knowledge of what that means in terms of voltages etc; the question I'd have is: does a connected device using that device's read/receive/whatever method receive the `0x00` ? or the `0xFF`? For all I know, the voltage chart shown is entirely normal and expected and doesn't indicate an error – Marc Gravell Feb 16 '19 at 11:26
  • Marc, I should of mentioned that the TX value goes through a max233 and an inverter to the RX pin of a PIC. The value is output to 8 LED's which none are lit. – Kenny Barber Feb 16 '19 at 11:32
  • 1
    I'm not sure what I'm looking at in the first image. Pehaps you're capturing the time when the port is opened (level goes high), until the start bit and data bits kick in (level goes low), and we don't see the stop bits because they're off the right of the screen? Change the data you're sending to a recognisable changing pattern, like 0xAF, and show an image which contains the whole waveform? – canton7 Feb 16 '19 at 12:03
  • Hi canton7, I have added the waveform of 0xAF. in binary form this is: 01010000 and the led's are showing 00101000 so its shifted right by 1. Any ideas? – Kenny Barber Feb 16 '19 at 12:44
  • *"I was expecting the waveform to look like the 2nd image."* -- You're supposed to be the one to tell us what the waveform represents, not us. Since the scope's display indicates `Vpp=19.0V`, the waveform you are looking at is RS-232, and not TTL. The logic levels for RS-232 are inverted. And regardless of logic levels, the bit order of the data from a UART is low-order bit first (i.e. earlier in time or the left-hand side of the timeline). Your third capture looks like (going left to right) a start bit, four 1s, a 0, a 1, a 0, and a 1 (plus a stop bit and idle line), or a byte of 0xAF. – sawdust Feb 16 '19 at 21:50
  • Questions on how to read a 'scope trace are off-topic for SO. Vote to close. – sawdust Feb 16 '19 at 21:56
  • This sounds like an interfacing probem rather than a software problem. RS232 interface chips invert, in the sense that if you put a TTL “1” of 5V (which is the idle state of a UART output) into an RS232 driver it outputs -12V, and vice versa. If your connection between the UART rx/tx doesn’t have two of these inverters i.e. between the UART serial output pin and the UART serial input pin then you will need to invert at the receiving end, if that’s possible. – DisappointedByUnaccountableMod Feb 17 '19 at 22:39

0 Answers0