ASCII for numbers greater than 255

Discussion in 'Programmer's Corner' started by kj4g09, Nov 13, 2012.

  1. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    I am wondering how many binary bits used to represent number greater than 255, such as 256, is it represented by 100000000 or something like 0..0100000000(more than 9 bits, say 16 bits?);

    I am using C# to send a number in the range of 0-4095 (12 bits) by serial port to 12-bit DAC. If the number (>255) send out is in the form of 16 bits then the extra 4 bits will shift out the first 4 bits in the register of DAC.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    There are many different types of serial ports, UART, SPI, I2C, USB, ethernet, etc.
    You really need to specify what kind of port you are using.

    For character oriented interfaces, you can transmit two bytes in binary to give 16 bits.
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The normal thing is to send a 16 bit word as two bytes. Then join them to a 16 bit word in the other end.
     
  4. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    its RS-232.
    I guess it will send 2 bytes through the serial port if the data is greater the 255.
     
  5. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    ASCII is the coding used to represent characters in the computer. You could convert your numbers into ASCII digits and send the data that way, but from what's been said in this thread, it seems as if the title is seriously inaccurate.

    If you send one byte for numbers < 256 and switch to two bytes for >= 256, is there any possibility that the receiving end will get confused about what's being sent? For instance, could the two bytes for a large number be interpreted as two successive numbers in the 0-255 range?

    You do understand that a serial port sends 8-bit characters only, right? You can't send 8 bits and then 4. Is it possible that the DAC actually uses an SPI port, basically a shift register?
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Not quite. It depends on who or what is sending the data.
    You still have to spell out clearly what you are doing.

    I can only guess that you are opening a PC COM port using C#.
    Then you are writing to the port. What statement are you using to write to the port?
    Are you sending binary or ASCII?
    ASCII can only send one byte at a time. You have to specifically deconstruct your data in order to send out two ASCII characters.

    There are 10 types of people in the world, those who understand binary and those who don't.
     
  7. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    yes I knew that it can only send 8 bits at a time. I was hoping if I send 256 through serial port, the actual signal send out could be 2 separated bytes like (00000001+00000000). But now I figured out it doesn't work in that way.

    Sorry about the title if it confused you.
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  9. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    You cannot send one byte and later change and decide to send two bytes.
    You have to make up your mind and always send two bytes.

    Then the problem arises.
    What if the receiver misses one byte and now every pair of byte is out of sync?
     
  10. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    I created a Windows Form Application in C#. I want to enter integer numbers in textBox and then click the buttom to send out that number. Also the code will open the USB COMM port with an USB to Serial adapter connect to it. The transmit Data Pin of that adapter is connected to the SDI(serial data in) of Serial to Parallel Shift Register of the DAC.
    the code for writing the port is:
    SerialPort1.Write(textBox1.Text);

    I tried to trigger the signal of the transmit data pin of the adapter using Oscilloscope.
    When I sent 255 through the port, the output waveform looks like "A"+"B"+"B"(I am guessing A represents 2 and B represent 5, but its not in binary form);

    What I want to do is to send a binary number out the port in the range of (0-4095);
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Then you'll need to convert the textBox text into an integer number... It hink the code would be Int32.Parse(textBoxt.Text);, though I could be mistaken.. it's something like that...

    If you tell the port to send text, it will send the ASCII numbers corresponding to those characters...
     
    Last edited: Nov 13, 2012
  12. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Uh oh. I'm seeing confirmation that the DAC has SPI, not a serial port, which of course is typical. If it were a serial port, the input pin would most likely be RX. Is there also a clock input?

    But prove me wrong. What is the part number and where can we look it up?
     
  13. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    SPI is serial... What you mean is a UART... I've never seen a DAC with a UART connection. You'll need to convert the UART data to SPI. a microcontroller can do this easily....
     
  14. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    Sorry, I just realize I made a silly mistake.
    The value out of the port is in the ASCII form, and the pin is active low which means 5(0101) is measured as (1010) plus a start&stop signals.

    All I need to do is to figure out a way to write characters and punctuates to the port, so that the output binary signal can be seem as a integer number in the range of 0-4095.
     
  15. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Well, all right, but SPI is "clocked serial" as opposed to a "serial port". I suppose mentioning a UART removes any ambiguity. But that's what I meant--a DAC with a UART sounds unlikely, and the terminology sounds like SPI.
     
  16. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    Thanks for that code.
    I will have a go.
     
  17. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    Yes, it has a clock input. Its DAC7614
     
  18. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    So this is a (almost) SPI interface.... you need to communicate with a clocked serial interface...
     
  19. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Yes, true, sorry.. I apparently misread the first time around, though it was a bit confusing making that distinction...;)
     
  20. kj4g09

    Thread Starter New Member

    Dec 4, 2011
    29
    0
    Yeah, I am going to use a microcontroller to setup and clock the DAC.
    But I have to get the right SDI out of the PC COMM Port first.
     
Loading...