1
\$\begingroup\$

I am doing a project in which I am getting data from some switches and push buttons on the pins of 8051 uC. Now I have to transmit this data on the port pins serially to the PC. At the PC end, I am receiving the data in C# .Net application using the serial port class. Now the issue is that I want that the data received in C# application is in the form of bit string instead of ASCII.

Can someone guide me how it can be achieved ?

\$\endgroup\$
7
  • \$\begingroup\$ Please start by giving us a schematic of the connection between the 8051 and the PC. I'm assuming this is RS232 but that might be wrong. Then, give us a few examples of what you want to send and how you want it to be interpreted. e.g. if your 8051 sends "1" do you want it to appear as "1" or "00000001" or "00110000" or ? Not enough information right now. \$\endgroup\$
    – akohlsmith
    Feb 10, 2013 at 14:22
  • \$\begingroup\$ The thing is that there is obviously some combinations of 1's and 0's on the port. When i take this combination of 8 bits data from the port and transmit it to the PC i get some random symbols. Instead i want it to be exactly the same combination bit string i.e. combination 8 bits as it was on the port of 8051 . \$\endgroup\$ Feb 11, 2013 at 1:32
  • \$\begingroup\$ And yes it is RS232 connection. And i am using standard serial communication connections between 8051 and PC. \$\endgroup\$ Feb 11, 2013 at 1:43
  • \$\begingroup\$ You are getting random symbols means you are getting correct data but you are reading it as ASCII! \$\endgroup\$
    – Swanand
    Feb 11, 2013 at 4:25
  • 2
    \$\begingroup\$ It looks like this issue is more of a software issue than a hardware one - if the symbols just need to be re-cast into binary using the c# application, this question would be better suited to stackoverflow. \$\endgroup\$
    – stanri
    Feb 11, 2013 at 5:17

2 Answers 2

2
\$\begingroup\$

It's not clear what you are asking since ASCII is a bit stream.

In any case, especially when you have your own application on the PC, it makes sense to send data in binary over the serial line. ASCII is for humans, but you have two machines communicating with each other. The PC can display the data in any form suitable for the users, but there is no need for that to be anything like the format of the data sent from the microcontroller.

Since the most limited end of the communications line is the microcontroller, use the easiest format for it. That will be just sending raw bytes. I usually use packets that start with a opcode byte, followed by whatever data is defined for that opcode. It is simple to send and receive in a little micro, with no need for bulky and slow ASCII to binary and binary to ASCII conversion routines.

On the PC side you have essentially infinite compute power relative to the speed of the serial line, so it can occomodate any format. However, raw binary is about as easy as it gets there too.

About the only thing to watch for is to not make implicit assumptions about the byte order the host machine uses for multi-byte values. Define whatever is easiest for the microcontroller, then work with that on the PC end. For example, let's say the micro stores multi-byte data in least to most significant byte order. Don't do something stupid like define a union in C and write the received bytes into byte field overlays of wider values. That makes your host program machine-dependent. Instead, do the shifting and all will be OK. For example, to assemble a 16 bit quantity in a wider integer, write the first byte into it directly, then OR the second byte into it after that byte was shifted left 8 bits. That will always work regardless of the host machine byte order.

As for converting binary values to ASCII, there are various facilities for that in any language. That is a pure language problem and out of scope here, and besides is trivial anyway.

\$\endgroup\$
5
  • \$\begingroup\$ One more thing I would like to add is, OP is using C# which has lot of in-built classes to convert from Binary to Hex to ASCII or whatever! \$\endgroup\$
    – Swanand
    Feb 10, 2013 at 16:53
  • \$\begingroup\$ @Swan: I expect any language would have such facilities readily available. \$\endgroup\$ Feb 10, 2013 at 17:06
  • \$\begingroup\$ @OlinLathrop The thing is that there is obviously some combinations of 1's and 0's on the port. When i take this combination of 8 bits data from the port and transmit it to the PC i get some random symbols. Instead i want it to be exactly the same combination bit string i.e. combination 8 bits as it was on the port of 8051 . \$\endgroup\$ Feb 11, 2013 at 1:48
  • 1
    \$\begingroup\$ @RehanAbbasi, it looks like you need to read a bit up on what ascii is. \$\endgroup\$
    – stanri
    Feb 11, 2013 at 5:23
  • \$\begingroup\$ @Rehan: It sounds like you are expecting to write binary data to the line at one end but have it appear as ASCII characters at the other? It doesn't work that way. Pick a format for transmission, which I recommend to be binary. Then make sure both ends adhere to that. You apparently have some ASCII conversion going on in there one way or the other. This can be a problem when using "black box" libraries. Think of the serial line as transporting bytes, then work you from there yourself. \$\endgroup\$ Feb 11, 2013 at 13:07
2
\$\begingroup\$

What you say sounds like you're actually getting the exact bits that arrive on the port (except for start/stop bits, etc), but you have somehow fooled yourself into thinking that the contents of your variables are "symbols". That doesn't make sense -- .NET SerialPort class will give you just raw bits, in batches of 8 bits for each C# byte or 16 bits for each C#'s char. Whether or not these bits encode symbols or something else is for you to decide.

What you choose to do with those bits after you've received them is your choice. You can choose to treat the 8 bits that sit in your byte variable as if they encode the number of an ASCII character and display that character, but nobody says you have to do that. Bits are bits; there's nothing special about those 8 bits that says that they need to be interpreted as an ASCII codepoint. You can do whatever you want with it -- byte (or, for that matter, char) is an arithmetic type, and all of the usual shifts and logical operations for manipulating bit string are at your immediate disposal.

You didn't expect the SerialPort to deliver bits to you in the form of the ASCII codes for the characters "0" and "1", did you? That would be wasteful, and for most practical purposes utterly pointless.

\$\endgroup\$
1
  • \$\begingroup\$ And if you somehow already have the data as a string on the C# side, you may find something like System.Text.Encoding.ASCII.GetBytes() helpful. Do make sure to read the documentation on that, however; it might not do exactly what you expect. \$\endgroup\$
    – user
    Feb 11, 2013 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.