# Converting 8 bits into a byte and transmitting via serial port in C#

#### hunterage2000

Joined May 2, 2010
487
Hi all,

I'm trying to find out how to Convert 8 bits into a byte and transmitting via serial port in C#.

I think I'm fine with transmitting via serial port but I don't know how to convert 8 bits to a byte.

In C I would use a bit field structure but can't find anything on it in C#.

Anyone know how this is done please?

Thanks you.

#### Picbuster

Joined Dec 2, 2013
983
Do you have 8 bit all over the place and want them in one byte?
If so its simple:
bit_(letter) is one of your bits
bit 7 bit 6 bit 5 bit 4 bit 3 bit2 bit1 bit0
The_Byte= bit_h*128 + bit_g*64 + bit_f*32 + bit_e*16 + bit_d*8 + bit_c*4 + bit_b *2 + bit_a
or use the shift function << I stead of multiplication ( same thing in fact)

Picbuster

#### jpanhalt

Joined Jan 18, 2008
8,332
Or, move the bit wherever it is to STATUS,C and rotate into the byte under construction (Buc). For example, if the MSB of your Buc is bit 3 of RegA, then:
Code:
bsf     STATUS,C
btfss   RegA,3
bcf     STATUS,C
rlf     Buc,f
Of course, if you told us more about where those "bits" were, we could give better answers. For example use of XOR to construct the Buc.

#### MrChips

Joined Oct 2, 2009
19,750
I am going to hazard a guess that the eight bits are all in one place, for example, eight input pins on an input-output port. This is a common misunderstanding by new comers to the world of microcomputers.

In such a case, there is no conversion required. Eight bits read in are already combined into a byte. One can transmit a byte directly via the serial port. One can also choose to format the data in any representation, for example, as a string as readable text.

#### hunterage2000

Joined May 2, 2010
487
Ok so I have a Windows form that has 8 checkboxes that if checked is 1 or unchecked 0

The selected bits either open or close a relay.

The idea is to tick what you want to open and close and press a SEND button that converts the 8 bits to a byte then transmits the byte by serial port to a SIPO Shift register. The parellel outputs will turn on the required relays.

I will try what you said Picbuster, nice one.

#### jpanhalt

Joined Jan 18, 2008
8,332
How are you reading that form? That is, are you trying to convert an image into a digital bit? If it is read as byte, that is no different than reading any register or port.

#### hunterage2000

Joined May 2, 2010
487
The idea is to control relays from a PC GUI using:

DTR pin to send 8 bits to the DS pin of a SIPO Shift register.
RTS pin will be used for the Storage Register clock to hold the required 8 bits at the parallel outputs
TXD pin will be used as the Shift Register clock, 16 cycles will be made to shift all 8 bits through before RTS is made HIGH

There is 8 relays so I thought the best way would be to have 8 checkboxes and a Send button.

The 8 checkbox states are polled until the Send button is pressed.

Each 8 states are stored in an array that will be sent individually by the DTR pin to the DS pin of the Shift register.

Not sure of any other way at the moment but all suggestions are welcome.

Thank you.

#### MrChips

Joined Oct 2, 2009
19,750
I think I follow what you are attempting to do. There is no need to convert the eight bits into binary.

You are going to create a GUI that has eight check boxes.
Collect the status of the check boxes into an array such as Relay_Output, where i is from 0 to 7.
Now bit-bang the output to the DTR pin as in this pseudo-code:

initialize i = 0
loop:
DTR = Relay_Output
toggle SR clock
i = i + 1
repeat loop while i < 7

#### hunterage2000

Joined May 2, 2010
487
Yeah that's what I was thinking, the next thing to do is create a clock on the txd pin. I guess you have to have a startbit when transmitting so I will send 7bit's to have a clock.

The attached image shows the timing diagram for the NPIC6C595. I'm assuming that a LOW to HIGH transition on STCP will put the 8 bits on the Parallel port. But not sure if STCP has to be held HIGH or can go LOW to HIGH to LOW.

I'll look into this.

#### Attachments

• 29.3 KB Views: 3

#### MrSoftware

Joined Oct 29, 2013
1,613
Maybe I'm late here, but if you want to cram all of your checkboxes into a single byte, just shift like this:

Code:
        static void Main(string[] args)
{
// Emulate check boxes, 0 = not checked, 1 = checked
int check0 = 0;
int check1 = 0;
int check2 = 0;
int check3 = 0;
int check4 = 1;
int check5 = 1;
int check6 = 1;
int check7 = 1;

byte AllCheckboxesInOneByte = (byte)(check0 | check1 << 1 | check2 << 2 | check3 << 3 | check4 << 4 | check5 << 5 | check6 << 6 | check7 << 7);

Console.WriteLine("These checkboxes are checked:  HEX: {0:X}", AllCheckboxesInOneByte);
Console.WriteLine("These checkboxes are checked:  BIN: " + Convert.ToString(AllCheckboxesInOneByte, 2));
}